다중 EMA 크로스오버에 기반한 적응형 거래 전략

저자:차오장, 날짜: 2023-09-26 14:41:00
태그:

전반적인 설명

이 전략은 여러 개의 EMA 지표 세트를 사용하여 적응적인 긴 / 짧은 거래를 구현합니다. 시장 장기 및 단기 트렌드를 기반으로 입출을위한 다른 매개 변수와 EMA를 채택합니다. 전략은 자동으로 황소 / 곰 시장을 인식하고 위험을 제어하기 위해 독립적인 스톱 손실을 사용합니다.

전략 논리

이 전략은 주로 EMA 지표의 크로스오버 원리를 활용한다. 빠른 EMA가 느린 EMA를 넘어서면 길고, 그 아래를 넘어가면 짧다. 여러 EMA를 설정하고 시장 트렌드를 기반으로 다른 매개 변수를 선택한다. 구체적으로, 장기 트렌드가 상승세를 판단할 때, 긴 신호를 위해 더 긴 기간 EMA의 세트를 사용한다. 하락 시에는 짧은 기간 EMA의 다른 세트를 사용한다. 출구도 다른 기간 EMA를 채택한다. 스톱 로스는 포지션 방향에 따라 고정 비율의 트레일링 스톱을 사용한다.

이점 분석

  • 여러 개의 적응 가능한 EMA 세트는 다양한 시장에서 유연하게 작동합니다.
  • 황소와 곰을 구별하면 신호가 더 명확해집니다.
  • 독립적인 출입/출입 매개 변수는 정확한 위치를 가능하게 합니다.
  • 일정한 비율의 스톱 로스는 위험을 효과적으로 통제합니다.
  • 전략 논리는 직관적이고 이해하기 쉽고 구현하기 쉽습니다.

위험 과 개선

  • EMA는 잘못된 신호를 생성할 수 있습니다. 매개 변수 조정이 핵심입니다.
  • 고정 스톱 로즈는 큰 변동을 추적하지 못할 수 있습니다.
  • 부피와 같은 필터를 추가해서 견고함을 높여야 합니다.
  • 매개 변수는 기계 학습 알고리즘으로 자동으로 최적화 될 수 있습니다.
  • 고정된 대신 ATR 같은 동적 스톱 손실을 사용하는 것을 고려하십시오.

요약

이 전략은 여러 EMA 크로스오버를 활용하여 적응 효과를 달성하고, EMA의 장점을 유지하며 전략을 보다 유연하게 만듭니다. 적절한 필터와 동적 스톱을 추가하면 매우 실용적인 자동화 거래 시스템이 될 수 있습니다.


/*backtest
start: 2023-08-26 00:00:00
end: 2023-09-07 00:00:00
period: 12h
basePeriod: 15m
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/
// © str1nger
//@version=4

// strategy(title="BTC - 4hr - Long/Short", shorttitle="BTC - 4hr - Long/Short", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=75,commission_type=strategy.commission.percent, commission_value=0.075)//////<---Uses a percentage of starting equity

//DATE RANGE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
startDate = input(title="Start Date", type=input.integer,
     defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer,
     defval=1, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer,
     defval=2020, minval=2000, maxval=2100)
endDate = input(title="End Date", type=input.integer,
     defval=1, minval=1, maxval=31)
endMonth = input(title="End Month", type=input.integer,
     defval=12, minval=1, maxval=12)
endYear = input(title="End Year", type=input.integer,
     defval=2021, minval=2000, maxval=2100)

inDateRange =  true


//EMAs//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG
//11,33,3,40
lof= input(11, title="Long Open - Fast", step=1)
los= input(33, title="Long Open - Slow", step=1)
lcf= input(3, title="Long Close - Fast", step=1)
lcs= input(40, title="Long Close - Slow", step=1)
ema_long_open_fast = ema(close, lof)
ema_long_open_slow = ema(close, los)
ema_long_close_fast= ema(close, lcf)
ema_long_close_slow = ema(close, lcs)
//SHORT
//5,11,4,7
sof= input(5, title="Short Open - Fast", step=1)
sos= input(11, title="Short Open - Slow", step=1)
scf= input(4, title="Short Close - Fast", step=1)
scs= input(7, title="Short Close - Slow", step=1)
ema_short_open_fast = ema(close, sof)
ema_short_open_slow = ema(close, sos)
ema_short_close_fast = ema(close, scf)
ema_short_close_slow = ema(close, scs)


//CONDITIONS///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG
openlong = crossover(ema_long_open_fast, ema_long_open_slow)
closelong = crossover(ema_long_close_slow, ema_long_close_fast)
//1.7%
long_loss_percent = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1.7) * 0.01
long_stop_price = strategy.position_avg_price * (1 - long_loss_percent)
//SHORT
openshort = crossover(ema_short_open_slow, ema_short_open_fast)
closeshort = crossover(ema_short_close_fast, ema_short_close_slow)
//0.4%
short_loss_percent = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=0.4) * 0.01
short_stop_price = strategy.position_avg_price * (1 + short_loss_percent)


//PLOT EMAs////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG
plot(ema_long_open_fast, "Long EMA open lower", linewidth=1, color=color.green)
plot(ema_long_open_slow, "Long EMA close upper", linewidth=1, color=color.green)
plot(ema_long_close_fast, "Long close lower", linewidth=1, color=color.red)
plot(ema_long_close_slow, "Long close upper", linewidth=1, color=color.red)
//SHORT
plot(ema_short_open_fast, "Short open fast", linewidth=1, color=color.green)
plot(ema_short_open_slow, "Short open slow", linewidth=1, color=color.green)
plot(ema_short_close_fast, "Short close fast", linewidth=1, color=color.red)
plot(ema_short_close_slow, "Short close slow", linewidth=1, color=color.red)


//LONG-TERM TRENDS
//LONG 144
long_term_trend_longs= input(144, title="Long-term trend - Longs", step=1)
lttl= ema(close, long_term_trend_longs)
plot(lttl, "Long-term trend - Longs", linewidth=2, color=color.blue)
//SHORT 89
long_term_trend_shorts= input(89, title="Long-term trend - Shorts", step=1)
ltts = ema(close, long_term_trend_shorts)
plot(ltts, "Long-term trend - Shorts", linewidth=2, color=color.blue)


//STRATEGY//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG
if (inDateRange and openlong and (close > lttl))
    strategy.entry("OL", long=true, comment="##insert open long comment here##")
if (inDateRange and closelong)
    strategy.close("OL", comment="##insert close long comment here##")
if strategy.position_size > 0
    strategy.exit("L-SL", stop=long_stop_price, comment="##insert long stop-loss comment here##")
//SHORT  
if (inDateRange and openshort and (close < ltts))
    strategy.entry("OS", long=false, comment="##insert open short comment here##")
if (inDateRange and closeshort)
    strategy.close("OS", comment="##insert close short comment here##")
if strategy.position_size < 0
    strategy.exit("S-SL", stop=short_stop_price, comment="##inster short stop-loss comment here##")




더 많은