이중 이동평균 크로스오버 전략으로 운동 거래

저자:차오장, 날짜: 2024-04-01 11:53:14
태그:

img

전반적인 설명

이 전략은 시장 트렌드의 변화를 식별하기 위해 8주기 및 21주기 기하급수적 이동 평균 (EMA) 을 사용한다. 단기 EMA가 아래에서 장기 EMA를 넘을 때 구매 신호가 생성되며, 단기 EMA가 위에서 장기 EMA를 넘을 때 판매 신호가 생성된다. 이 전략은 또한 트렌드 역전의 추가 확인으로 세 개의 연속적인 하위 하위 (HLs) 와 세 개의 연속적인 하위 하위 (LHs) 를 포함한다. 또한, 위험 관리 및 수익을 잠금하기 위해 스톱 로스 및 테이크 노프트 레벨이 설정된다.

전략 원칙

  1. 주요 트렌드 방향을 확인하기 위해 8주기 및 21주기 EMA를 계산합니다.
  2. 3개의 연속적인 높은 최저점 (HL) 과 3개의 연속적인 낮은 최고점 (LH) 을 잠재적인 트렌드 반전의 초기 신호로 식별합니다.
  3. 8주기 EMA가 21주기 EMA를 넘어서 HL 파업이 발생하면 구매 신호를 생성합니다. 8주기 EMA가 21주기 EMA를 넘어서 LH 파업이 발생하면 판매 신호를 생성합니다.
  4. 스톱 로스 레벨을 입시 가격의 5%로 설정하고, 수익을 취하는 레벨을 입시 가격의 16%로 설정하여 리스크를 관리하고 수익을 고정합니다.
  5. 포지션을 닫고 반대 신호가 나타나면 역전 포지션을 열어

전략적 장점

  1. EMA와 가격 행동 패턴 (HLs 및 LHs) 을 결합하여 트렌드를 확인하여 신호 신뢰성을 향상시킵니다.
  2. 명확한 스톱 로스 및 취리 레벨을 설정하여 위험을 관리하고 수익을 확보하는 데 도움이 됩니다.
  3. 여러 시간 프레임과 다른 시장에 적용되며 어느 정도의 다양성을 제공합니다.
  4. 명확한 논리, 이해하기 쉽고 실행하기 쉽습니다.

전략 위험

  1. 불안한 시장에서, 빈번한 교차가 여러 가지 잘못된 신호로 이어지고 손실이 발생할 수 있습니다.
  2. 고정된 스톱 로스 및 토익 레벨은 다른 시장 조건에 잘 적응하지 않을 수 있으며, 잠재적인 기회 비용이나 더 큰 손실로 이어질 수 있습니다.
  3. 이 전략은 역사적인 데이터에 의존하고 갑작스러운 사건이나 근본적인 변화에 잘 적응하지 못할 수 있습니다.

전략 최적화 방향

  1. 변동성 (예를 들어, ATR) 에 기초한 레벨의 조정과 같은 적응적 인 스톱 로스 및 이익 취득 메커니즘을 도입하여 다른 시장 조건에 더 잘 적응합니다.
  2. 부피, 상대적 강도 지수 (RSI) 등과 같은 다른 지표 또는 요소를 포함하여 신호를 더 잘 필터하고 신뢰성을 향상시킵니다.
  3. 매개 변수 (예를 들어, EMA 기간, 스톱 로스 및 영업 수익 비율) 를 최적화하여 특정 시장 또는 도구에 가장 좋은 조합을 찾습니다.
  4. 각 거래의 위험 노출을 제어하기 위해 포지션 사이즈와 같은 위험 관리 조치를 도입하는 것을 고려하십시오.

요약

이 전략은 트렌드 반전을 식별하고 거래 신호를 생성하기 위해 HL 및 LH 가격 패턴과 결합하여 8 기간 및 21 기간 EMA의 크로스오버를 활용합니다. 명확한 스톱 로스 및 트레이크 이윤 규칙은 위험을 관리하고 수익을 잠금하는 데 도움이됩니다. 그러나 전략은 불안정한 시장에서 잘못된 신호를 생성 할 수 있으며 고정 스톱 로스 및 트레이크 이윤 수준은 다른 시장 조건에 잘 적응하지 않을 수 있습니다. 추가 개선을 위해 적응 스톱 로스 및 트레이크 이윤을 도입하고 다른 지표를 통합하고 매개 변수를 최적화하고 위험 관리 조치를 도입하는 것을 고려하십시오. 전반적으로 전략은 추진력과 트렌드를 따르는 거래를위한 틀을 제공하지만 특정 시장과 개별 선호도에 따라 조정 및 최적화를 요구합니다.


/*backtest
start: 2023-03-26 00:00:00
end: 2024-03-31 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy('Trend Following 8&21EMA with strategy tester [ukiuro7]', overlay=true, process_orders_on_close=true, calc_on_every_tick=true, initial_capital = 10000)

//INPUTS
lh3On = true
hl3On = true
emaOn = input(title='105ema / 30min', defval=true) 
assistantOn = input(title='Assistant', defval=true)
textOn = input(title='Text', defval=true)

showRiskReward = input.bool(true, title='Show Risk/Reward Area', group="TP/SL")
stopPerc = input.float(5.0, step=0.1, minval=0.1, title='Stop-Loss %:',group="TP/SL") / 100
tpPerc = input.float(16.0, step=0.1, minval=0.1, title='Take-Profit %:',group="TP/SL") / 100

backtestFilter = input(false, title='Backtest Entries to Date Range',group="Backtest Date Range")
i_startTime = input(defval=timestamp('01 Jan 2022 00:00'), inline="b_1", title='Start',group="Backtest Date Range")
i_endTime = input(defval=timestamp('01 Jan 2029 00:00'), inline="b_1", title='End',group="Backtest Date Range")
inDateRange = true

message_long_entry = input.string(title='Alert Msg: LONG Entry', defval ='', group='Alert Message')
message_short_entry = input.string(title='Alert Msg: SHORT Entry', defval='', group='Alert Message')
message_long_exit = input.string(title='Alert Msg: LONG SL/TP', defval='', group='Alert Message')
message_short_exit = input.string(title='Alert Msg: SHORT SL/TP', defval='', group='Alert Message')  

//CALCS
threeHigherLows() =>
    low[0] >= low[1] and low[1] >= low[2]

threeLowerHighs() =>
    high[2] >= high[1] and high[1] >= high[0]

breakHigher() =>
    padding = timeframe.isintraday ? .02 : .1
    high >= high[1] + padding

breakLower() =>
    padding = timeframe.isintraday ? .02 : .1
    low <= low[1] - padding

lh3 = threeLowerHighs() and lh3On
lh3bh = lh3[1] and breakHigher() and lh3On

hl3 = threeHigherLows() and hl3On
hl3bl = hl3[1] and breakLower() and hl3On

ema8 = ta.ema(close, 8)
ema21 = ta.ema(close, 21)

//VARS
var float longStop = na, var float longTp = na
var float shortStop = na, var float shortTp = na

//CONDS
isUptrend = ema8 >= ema21
isDowntrend = ema8 <= ema21
trendChanging = ta.cross(ema8, ema21)

buySignal = lh3bh and lh3[2] and lh3[3] and isUptrend and timeframe.isintraday
sellSignal = hl3bl and hl3[2] and hl3[3] and isDowntrend and timeframe.isintraday

goingDown = hl3 and isDowntrend and timeframe.isintraday
goingUp = lh3 and isUptrend and timeframe.isintraday

projectXBuy = trendChanging and isUptrend
projectXSell = trendChanging and isDowntrend

longCond = trendChanging and isUptrend and assistantOn
shortCond = trendChanging and isDowntrend and assistantOn

//STRATEGY
if shortCond and strategy.position_size > 0 and barstate.isconfirmed
    strategy.close('Long', comment='CLOSE LONG', alert_message=message_long_exit)

if longCond and strategy.position_size < 0 and barstate.isconfirmed
    strategy.close('Short', comment='CLOSE SHORT', alert_message=message_short_exit) 

if longCond and strategy.position_size <= 0 and barstate.isconfirmed and inDateRange
    longStop := close * (1 - stopPerc)
    longTp := close * (1 + tpPerc)
    strategy.entry('Long', strategy.long, comment='LONG', alert_message=message_long_entry)
    strategy.exit('Long Exit', 'Long', comment_loss="SL LONG", comment_profit = "TP LONG", stop=longStop, limit=longTp, alert_message=message_long_exit)

if shortCond and strategy.position_size >= 0 and barstate.isconfirmed and inDateRange
    shortStop := close * (1 + stopPerc)
    shortTp := close * (1 - tpPerc)
    strategy.entry('Short', strategy.short, comment='SHORT', alert_message=message_short_entry)
    strategy.exit('Short Exit', 'Short', comment_loss="SL SHORT", comment_profit="TP SHORT", stop=shortStop, limit=shortTp, alert_message=message_short_exit)

//PLOTS
plotshape(longCond, style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), size=size.small, text='Buy')
plotshape(shortCond, style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.small, text='Sell')
plotchar(trendChanging and isUptrend and close < open and assistantOn, char='!', location=location.abovebar, color=color.new(color.green, 0), size=size.small)

aa = plot(ema8, linewidth=3, color=color.new(color.green, 0), editable=true)
bb = plot(ema21, linewidth=3, color=color.new(color.red, 0), editable=true)
fill(aa, bb, color=isUptrend ? color.new(color.green,90) : color.new(color.red,90))
buyZone = isUptrend and lh3 and high < ema21 and timeframe.isintraday
sellZone = isDowntrend and hl3 and low > ema21 and timeframe.isintraday

L1 = plot(showRiskReward and strategy.position_size > 0 ? strategy.position_avg_price : na, color=color.new(color.green, 0), linewidth=1, style=plot.style_linebr, title='Long Entry Price')
L2 = plot(showRiskReward and strategy.position_size > 0 ? longTp : na, color=color.new(color.green, 0), linewidth=1, style=plot.style_linebr, title='Long TP Price')
L3 = plot(showRiskReward and strategy.position_size > 0 ? longStop : na, color=color.new(color.red, 0), linewidth=1, style=plot.style_linebr, title='Long Stop Price')

S1 = plot(showRiskReward and strategy.position_size < 0 ? strategy.position_avg_price : na, color=color.new(color.teal, 0), linewidth=1, style=plot.style_linebr, title='Short Entry Price')
S2 = plot(showRiskReward and strategy.position_size < 0 ? shortTp : na, color=color.new(color.teal, 0), linewidth=1, style=plot.style_linebr, title='Short TP Price')
S3 = plot(showRiskReward and strategy.position_size < 0 ? shortStop : na, color=color.new(color.maroon, 0), linewidth=1, style=plot.style_linebr, title='Short Stop Price')

fill(L1, L2, color=color.new(color.green, 90))
fill(L1, L3, color=color.new(color.red, 90))
fill(S1, S2, color=color.new(color.teal, 90))
fill(S1, S3, color=color.new(color.maroon, 90))

bgcolor(inDateRange == false ? color.new(color.red,90) : na, title="Backtest Off-Range") 


더 많은