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


생성 날짜: 2023-09-28 15:12:50 마지막으로 수정됨: 2023-09-28 15:12:50
복사: 0 클릭수: 928
avatar of ChaoZhang ChaoZhang
1
집중하다
1621
수행원

개요

이 전략은 21일과 55일 이동평균선의 교차를 기반으로 구매 및 판매 신호를 생성하고, 슈퍼 트렌드 지표와 결합하여 가짜 신호를 필터링하는 트렌드 추적 전략이다.

전략 원칙

코드는 먼저 21일선 ((EMA1) 과 55일선 ((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)