이중 이동 평균 및 슈퍼 트렌드 전략

저자:차오장, 날짜: 2023-09-28 15:12:50
태그:

전반적인 설명

이 전략은 21일 및 55일 이동 평균의 교차를 기반으로 거래 신호를 생성하고, 거짓 신호를 필터링하기 위해 슈퍼 트렌드 지표를 사용합니다. 트렌드를 따르는 전략입니다.

전략 논리

코드는 먼저 21일 EMA (EMA1) 와 55일 EMA (EMA2) 를 정의합니다. EMA1가 EMA2를 넘을 때 구매 신호가 생성됩니다. EMA1가 EMA2를 넘을 때 판매 신호가 생성됩니다.

거짓 신호를 필터하기 위해 슈퍼트렌드 지표가 추가됩니다. 슈퍼트렌드는 ATR 및 최근 높은-저 낮은 가격에 기초하여 트렌드 방향을 계산합니다. 코드에서 슈퍼트렌드 라인의 위쪽은 상승 추세이고 아래쪽은 하락 추세입니다.

따라서 구매 신호는 상승 추세 중 EMA1가 EMA2를 넘을 때만 생성됩니다. 판매 신호는 하락 추세 중 EMA1가 EMA2를 넘을 때만 생성됩니다. 슈퍼 트렌드는 트렌드 전환 중에 잘못된 신호를 필터링합니다.

또한, 200일 및 233일 이동 평균은 장기 트렌드를 결정하기 위해 추가됩니다. 장기 및 단기 트렌드가 일치 할 때 신호가 생성됩니다.

장점

  1. 이중 이동 평균과 슈퍼 트렌드는 트렌드를 효과적으로 식별하고 잘못된 신호를 필터 할 수 있습니다.

  2. 조정 가능한 이동 평균 매개 변수는 다른 시장 조건에 전략을 조정할 수 있습니다.

  3. 장기 이동 평균은 서로 충돌하는 트렌드로부터의 위험을 방지합니다.

  4. 알고리즘 트레이딩을 위한 명확한 규칙.

  5. 시각적인 구매/판매 신호는 거래 결정을 명확하게 합니다.

위험성

  1. 이동 평균은 전환점 주위에서 잘못된 신호를 생성 할 수 있습니다. 잠재적 인 전환점을 식별해야합니다.

  2. 부적절한 매개 변수 설정은 트렌드를 놓치거나 과도한 잘못된 신호를 유발할 수 있습니다. 매개 변수를 다른 시장에 맞게 조정해야합니다.

  3. 높은 거래 빈도는 더 높은 거래 비용을 초래합니다. 비용을 모니터링해야합니다.

  4. 슈퍼트렌드 매개 변수는 필터링 효과와 지연을 균형을 맞추기 위해 최적화되어야 합니다.

  5. 장기 평균은 신호를 생성하는 데 지연할 수 있습니다. 트렌드 변화의 타이밍은 중요합니다.

개선

  1. 최적의 매개 변수를 찾기 위해 다른 이동 평균 조합을 테스트합니다.

  2. 수퍼트렌드 매개 변수를 최적화하여 필터링과 지연을 균형 잡습니다.

  3. 부피와 같은 다른 지표를 추가하여 신호를 더 검증합니다.

  4. 잠재적인 전환점을 파악하기 위해 감정 분석과 뉴스 분석을 포함합니다.

  5. 매개 변수를 동적으로 최적화하기 위해 기계 학습을 사용하세요.

결론

이 전략은 트렌드를 식별하고 잘못된 신호를 필터하는 데 있어서 이중 이동 평균과 슈퍼 트렌드의 강점을 결합한다. 매개 변수 최적화와 추가 검증을 통해 지속적으로 개선될 수 있다. 특정 위험에도 불구하고 위험 통제 기술을 통해 관리될 수 있다. 이 전략은 알고리즘 거래에 적합하다.


/*backtest
start: 2022-09-21 00:00:00
end: 2023-09-27 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bhavikmota

//@version=4
strategy("EMA & Supertrend", overlay = true)

//length = input(9, minval=1)
//ema1 = ema(close, length)
//ema2 = ema(ema1, length)
//ema3 = ema(ema2, length)

//shortest = ema(close, 20)
//short = ema(close, 50)
//longer = ema(close, 100)
//longest = ema(close, 200)


//for Ema1
len1 = input(21, minval=1)
//src1 = input(close)
ema1 = ema(close,len1)
plot(ema1, color=color.red, linewidth=1)

//for Ema2
len2 = input(55, minval=1)
//src2 = input(close)
ema2 = ema(close,len2)
plot(ema2, color=color.green, linewidth=1)

//for Ema3
len3 = input(200, minval=1)
//src3 = input(close)
ema3 = ema(close,len3)
plot(ema3, color=color.blue, linewidth=1)

//for Ema4
len4 = input(233, minval=1)
//src4 = input(close)
ema4 = ema(close,len4)
plot(ema4, color=color.black, linewidth=1)


Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond = trend != trend[1]
alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")


//Trading logic

Enterlong = crossover(ema1,ema2) or (close>ema1 and close>ema2 and ema1>ema2) and close>ema4// positive ema crossover
Exitlong = crossunder(close,ema2) // candle closes below supertrend

Entershort = crossunder(ema1,ema2) or (close<ema1 and close<ema2 and ema2<ema1) and close<ema4// negative ema crossover
Exitshort = crossover(close,ema2) // candle closes above supertrend

//Execution Logic - Placing Order

start = timestamp(2008,1,1,0,0)

if time>= start
    strategy.entry("long", strategy.long, when=Enterlong)
    strategy.close("long",when=Exitlong)
//strategy.entry("short",strategy.short,100,when=Entershort)
//strategy.close("short",when=Exitshort)

더 많은