5분 EMA 크로스오버 전략의 추세

저자:차오장, 날짜: 2023-11-16 15:36:36
태그:

img

전반적인 설명

이 전략은 트렌드를 자동으로 잡기 위해 제한 오더와 트래일링 스톱 로스로 빠른 및 느린 EMA 크로스오버를 기반으로 한 5분 시간 프레임 트렌드 다음 전략이다. 전체 트렌드 방향을 결정하고 특정 엔트리 타이밍을 파악하기 위해 EMA 필터를 사용하여 중장기 트렌드 거래에 적합합니다. 이의 장점은 정확한 트렌드 판단과 효과적인 트렌드 추적입니다. 단점에는 가끔 잘못된 브레이크와 윙사우가 있습니다.

전략 논리

  1. 빠른 EMA와 느린 EMA를 사용하세요. 빠른 EMA가 느린 EMA를 넘을 때 길게 이동하고, 낮은 EMA를 넘을 때 짧게 이동하세요.
  2. EMA 매크로 필터를 적용하고 EMA 위의 장과 EMA 아래의 단을 취해서 잘못된 신호를 피합니다.
  3. 이상적인 진입 가격의 충족을 보장하기 위해 제한 주문으로 거래를 입력
  4. 이윤을 차단하고 손실을 줄이기 위해 진입 후 동적 인 후속 스톱 손실을 적용합니다.

구체적으로:

  1. 선택된 기간에 기초하여 빠른 EMA와 느린 EMA를 계산합니다.
  2. 필터가 활성화되어 있는 경우 가격이 EMA보다 높거나 낮을 때만 긴/단기 포지션을 취득합니다.
  3. 빠른 EMA가 느린 EMA를 넘을 때 길게 이동하고, 하향 크로스오버에서 짧게 이동합니다.
  4. 롱에 한정 구매 주문을 하고 쇼트에 한정 판매 주문을 한다
  5. 진입 후 최대 최대/최저 최저 기준에 기초한 트레일링 스톱 손실을 시작

위의 내용은 이 전략의 기본적인 거래 논리를 다루고 있습니다.

장점

  1. EMA 필터는 전체 트렌드를 결정하고 주요 트렌드에 반하는 거래를 방지합니다.
  2. 제한이 있는 빠른/연속 EMA는 상위/하위 추격을 방지합니다.
  3. 동적 인 후속 정지 효과적 인 수익에 잠금
  4. ~2%의 스톱 로스로 좋은 리스크 제어
  5. 소액 인출, 트렌드를 잘 따라
  6. 단순하고 이해하기 쉬운 논리, 최적화

위험성

  1. 윙사와 거짓 탈출 위험, 함락될 가능성
  2. 잘못된 EMA 기간은 놓친 트렌드를 유발할 수 있습니다.
  3. 스톱 로즈가 너무 넓어서 정상적인 변동성을 초과할 수 있습니다.
  4. 후속 스톱 너무 공격적이어서 조기에 빠져나올 수 있습니다.
  5. 최저 스톱 로스/익스피스 비율, 큰 움직임을 놓치고

해결책:

  1. 가장 좋은 기간을 찾기 위해 EMA 매개 변수를 최적화
  2. 과도한 정지 방지하기 위해 정지를 적당히 넓혀라
  3. 주의 깊게 추적 시작점과 추적 비율을 결정
  4. 최적을 찾기 위해 다른 스톱/테이크 비율을 테스트

최적화 기회

  1. 가장 좋은 조합을 찾기 위해 EMA 기간을 최적화하십시오.
  2. 가중화 이동 평균과 같은 다른 EMA 유형을 시도
  3. 향상된 성과를 위해 MACD와 같은 다른 지표를 테스트하십시오.
  4. 더 긴 시간 프레임에 EMA 필터링 시도
  5. 제한 주문 입시 가격 범위를 최적화
  6. 스톱 로스 및 영업 포인트/비율을 최적화
  7. 더 발전된 추적 방법의 실험
  8. 상황 인식에 대한 트렌드 강도 지표 추가
  9. 더 많은 필터를 고려하여 잘못된 신호를 추가로 피하십시오.

결론

전체적으로 이것은 매우 효과적인 중장기 트렌드 다음 전략이다. 엔트리를 위해 EMA 크로스오버를 사용, 추격을 방지하기 위해 제한 주문을 사용, 그리고 수익을 잠금하기 위해 트레일링 스톱을 사용하는 명확한 논리는 간단하고 견고하다. 적절한 매개 변수 조정으로 더 높은 승률과 수익성을 달성할 수 있다. 부적절한 EMA 기간과 과도한 스톱과 같은 위험은 모니터링되어야 한다. 그러나 일반적으로 이것은 효율적인 수치화 가능한 트렌드 거래 시스템이다.


/*backtest
start: 2022-11-09 00:00:00
end: 2023-11-15 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_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/
// © jordanfray

//@version=5
strategy(title="5 Minute EMA Strategy", overlay=true, max_bars_back=500, default_qty_type=strategy.percent_of_equity, default_qty_value=100,initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.05, backtest_fill_limits_assumption=2)

// Indenting Classs
indent_1 = " "
indent_2 = "  "
indent_3 = "   "
indent_4 = "    "

// Group Titles
group_one_title = "EMA Settings"
group_two_title = "Entry Settings"
group_three_title = "Trade Filters"

// Input Tips

ocean_blue = color.new(#0C6090,0)
sky_blue = color.new(#00A5FF,0)
green = color.new(#2DBD85,0)
red = color.new(#E02A4A,0)
light_blue = color.new(#00A5FF,85)
light_green = color.new(#2DBD85,85)
light_red = color.new(#E02A4A,85)
light_yellow = color.new(#FFF900,85)
white = color.new(#ffffff,0)
light_gray = color.new(#000000,70)
transparent = color.new(#000000,100)

// Strategy Settings - EMA
fast_EMA_length = input.int(defval=20, minval=1, title="Fast Length", group=group_one_title)
fast_EMA_type = input.string(defval="EMA", options = ["EMA", "SMA", "RMA", "WMA"], title=indent_4+"Type", group=group_one_title)
fast_EMA_source = input.source(defval=close, title=indent_4+"Source", group=group_one_title)
fast_EMA = switch fast_EMA_type
    "EMA" => ta.ema(fast_EMA_source, fast_EMA_length)
    "SMA" => ta.sma(fast_EMA_source, fast_EMA_length)
    "RMA" => ta.rma(fast_EMA_source, fast_EMA_length)
    "WMA" => ta.wma(fast_EMA_source, fast_EMA_length)
    => na
plot(fast_EMA, title="Fast EMA", linewidth=1, color=green, editable=true)

slow_EMA_length = input.int(defval=100, minval=1, title="Slow Length", group=group_one_title)
slow_EMA_type = input.string(defval="EMA", options = ["EMA", "SMA", "RMA", "WMA"], title=indent_4+"Type", group=group_one_title)
slow_EMA_source = input.source(defval=close, title=indent_4+"Source", group=group_one_title)
slow_EMA = switch slow_EMA_type
    "EMA" => ta.ema(slow_EMA_source, slow_EMA_length)
    "SMA" => ta.sma(slow_EMA_source, slow_EMA_length)
    "RMA" => ta.rma(slow_EMA_source, slow_EMA_length)
    "WMA" => ta.wma(slow_EMA_source, slow_EMA_length)
    => na
plot(slow_EMA, title="Slow EMA", linewidth=1, color=sky_blue, editable=true)


// EMA Macro Filter
enable_EMA_filter = input.bool(defval=false, title="Use EMA Filter", group=group_three_title)
EMA_filter_timeframe = input.timeframe(defval="", title=indent_4+"Timeframe", group=group_three_title)
EMA_filter_length = input.int(defval=300, minval=1, step=10, title=indent_4+"Length", group=group_three_title)
EMA_filter_source = input.source(defval=hl2, title=indent_4+"Source", group=group_three_title)
ema_filter = ta.ema(EMA_filter_source, EMA_filter_length)
ema_filter_smoothed = request.security(syminfo.tickerid, EMA_filter_timeframe, ema_filter[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_on)
plot(enable_EMA_filter ? ema_filter_smoothed: na, title="EMA Macro Filter", linewidth=2, color=white, editable=true)


// Entry Settings
stop_loss_val = input.float(defval=2.0, title="Stop Loss (%)", step=0.1, group=group_two_title)/100
take_profit_val = input.float(defval=2.0, title="Take Profit (%)", step=0.1, group=group_two_title)/100
long_entry_limit_lookback = input.int(defval=3, title="Long Entry Limit Lookback", minval=1, step=1, group=group_two_title)
short_entry_limit_lookback = input.int(defval=3, title="Short Entry Limit Lookback", minval=1, step=1, group=group_two_title)
limit_order_long_price = ta.lowest(low, long_entry_limit_lookback)
limit_order_short_price = ta.highest(high, short_entry_limit_lookback)
start_trailing_after = input.float(defval=1, title="Start Trailing After (%)", step=0.1, group=group_two_title)/100
trail_behind = input.float(defval=1, title="Trail Behind (%)", step=0.1, group=group_two_title)/100
long_start_trailing_val = strategy.position_avg_price + (strategy.position_avg_price * start_trailing_after)
short_start_trailing_val = strategy.position_avg_price - (strategy.position_avg_price * start_trailing_after)
long_trail_behind_val = close - (strategy.position_avg_price * (trail_behind/100))
short_trail_behind_val = close + (strategy.position_avg_price * (trail_behind/100))
currently_in_a_long_postion = strategy.position_size > 0
currently_in_a_short_postion = strategy.position_size < 0
long_profit_target = strategy.position_avg_price * (1 + take_profit_val)
long_stop_loss = strategy.position_avg_price * (1.0 - stop_loss_val)
short_profit_target = strategy.position_avg_price * (1 - take_profit_val)
short_stop_loss = strategy.position_avg_price * (1 + stop_loss_val)

bars_since_entry = bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1)
plot(bars_since_entry, editable=false, title="Bars Since Entry", color=green)

long_run_up = currently_in_a_long_postion and bars_since_entry > 0 ? ta.highest(high, bars_since_entry) : high
long_trailing_stop = currently_in_a_long_postion and bars_since_entry > 0 and long_run_up > long_start_trailing_val ? long_run_up - (long_run_up * trail_behind) : long_stop_loss
long_run_up_line = plot(long_run_up, style=plot.style_stepline, editable=false, color=currently_in_a_long_postion ? green : transparent)
long_trailing_stop_line = plot(long_trailing_stop, style=plot.style_stepline, editable=false, color=currently_in_a_long_postion ? long_trailing_stop > strategy.position_avg_price ? green : red : transparent)

short_run_up = currently_in_a_short_postion and bars_since_entry > 0 ? ta.lowest(low, bars_since_entry) : low
short_trailing_stop = currently_in_a_short_postion and bars_since_entry > 0 and short_run_up < short_start_trailing_val ? short_run_up + (short_run_up * trail_behind) : short_stop_loss
// short_run_up_line = plot(short_run_up, style=plot.style_stepline, editable=false, color=currently_in_a_short_postion ? green : transparent)
short_trailing_stop_line = plot(short_trailing_stop, style=plot.style_stepline, editable=false, color=currently_in_a_short_postion ? short_trailing_stop < strategy.position_avg_price ? green : red : transparent)


// Trade Conditions
fast_EMA_cross_down_slow_EMA = ta.crossunder(fast_EMA,slow_EMA)
fast_EMA_cross_up_slow_EMA = ta.crossover(fast_EMA,slow_EMA)
plotshape(fast_EMA_cross_down_slow_EMA ? close : na, title="Short Entry Symbol", color=red, style=shape.triangledown, location=location.belowbar)
plotshape(fast_EMA_cross_up_slow_EMA ? close : na, title="Long Entry Symbol", color=green, style=shape.triangleup, location=location.abovebar)
fast_EMA_is_above_slow_EMA = fast_EMA > slow_EMA
fast_EMA_is_below_slow_EMA = fast_EMA < slow_EMA
ema_macro_filter_longs_only = fast_EMA > ema_filter_smoothed and slow_EMA > ema_filter_smoothed
ema_macro_filter_shorts_only = fast_EMA < ema_filter_smoothed and slow_EMA < ema_filter_smoothed

long_position_take_profit = ta.cross(close, long_trailing_stop) or close > long_profit_target
short_position_take_profit = ta.cross(close, short_trailing_stop) or close > short_profit_target

long_conditions_met = enable_EMA_filter ? ema_macro_filter_longs_only and fast_EMA_cross_up_slow_EMA and fast_EMA_is_above_slow_EMA and not currently_in_a_short_postion : fast_EMA_cross_up_slow_EMA and not currently_in_a_short_postion
short_conditions_met = enable_EMA_filter ? ema_macro_filter_shorts_only and fast_EMA_cross_down_slow_EMA and fast_EMA_is_below_slow_EMA and not currently_in_a_long_postion : fast_EMA_cross_down_slow_EMA and fast_EMA_is_below_slow_EMA and not currently_in_a_long_postion

// Long Entry
strategy.entry(id="Long", direction=strategy.long, limit=limit_order_long_price, when=long_conditions_met)
strategy.cancel(id="Cancel Long", when=ta.crossover(fast_EMA,slow_EMA))
strategy.exit(id="Close Long", from_entry="Long", stop=long_trailing_stop, limit=long_profit_target, when=long_position_take_profit)

// Short Entry 
strategy.entry(id="Short", direction=strategy.short, limit=limit_order_short_price, when=short_conditions_met)
strategy.cancel(id="Cancel Short", when=ta.crossunder(fast_EMA,slow_EMA))
strategy.exit(id="Close Short", from_entry="Short", stop=short_trailing_stop, limit=short_profit_target, when=short_position_take_profit)

entry = plot(strategy.position_avg_price, editable=false, title="Entry", style=plot.style_stepline, color=currently_in_a_long_postion or currently_in_a_short_postion ? color.blue : transparent, linewidth=1)
fill(entry,long_trailing_stop_line, editable=false, color=currently_in_a_long_postion ? long_trailing_stop > strategy.position_avg_price ? light_green : light_red : transparent)
fill(entry,short_trailing_stop_line, editable=false, color=currently_in_a_short_postion ? short_trailing_stop < strategy.position_avg_price ? light_green : light_red : transparent)
//ltp = plot(currently_in_a_long_postion ? long_profit_target : na, style=plot.style_stepline, title="Take Profit", color=currently_in_a_long_postion ? green : transparent, linewidth=1)
//lsl = plot(currently_in_a_long_postion ? long_stop_loss : na, style=plot.style_stepline, title="Take Profit", color=currently_in_a_long_postion ? red : transparent, linewidth=1)
//fill(entry,ltp, color= currently_in_a_long_postion ? light_green : light_red)
//fill(entry,lsl, color= currently_in_a_long_postion ? light_red : light_green)

//stp = plot(currently_in_a_short_postion ? short_profit_target : na, style=plot.style_stepline, title="Take Profit", color=currently_in_a_short_postion ? green : transparent, linewidth=1)
//ssl = plot(currently_in_a_short_postion ? short_stop_loss : na, style=plot.style_stepline, title="Take Profit", color=currently_in_a_short_postion ? red : transparent, linewidth=1)
//fill(entry,stp, color= currently_in_a_short_postion ? light_green : light_red)
//fill(entry,ssl, color= currently_in_a_short_postion ? light_red : light_green)


더 많은