빠른 느린 이중 이동 평균 거래 전략

저자:차오장, 날짜: 2023-10-27 16:41:24
태그:

img

전반적인 설명

이중 이동 평균 거래 전략은 빠른 이동 평균과 느린 이동 평균을 계산하고 교차를 관찰함으로써 거래 신호를 생성합니다. 빠른 이동 평균이 느린 이동 평균을 넘을 때 긴 포지션을 취합니다. 빠른 이동 평균이 느린 이동 평균을 넘을 때 짧은 포지션을 취합니다. 이 전략은 트렌드 거래와 카운터 트렌드 거래 모두에 사용할 수 있습니다.

전략 논리

이 전략은 먼저 빠른 이동 평균 maFastLength와 느린 이동 평균 maSlowLength의 길이를 설정합니다. 다음으로 빠른 이동 평균 fastMA와 느린 이동 평균 slowMA를 계산합니다. 빠른 이동 평균은 가격 변화에 더 빠르게 반응하고 현재 트렌드를 판단하는 데 사용됩니다. 느린 이동 평균은 더 느리게 반응하고 트렌드의 방향을 결정하는 데 사용됩니다.

빠른 이동 평균이 느린 이동 평균을 넘을 때, 긴 입력 신호가 생성됩니다. 빠른 이동 평균이 느린 이동 평균을 넘을 때, 기존의 긴 포지션은 killLong))) 신호로 닫습니다.

이 전략은 단지 길게, 단지 짧게 설정할 수 있고, 길고 짧은 거래를 허용할 수 있습니다.

롱 모드에서만 롱 포지션은 goLong ((() 신호에 입력되고 killLong ((() 신호에 종료됩니다.

단편 모드에서는 killLong ((() 신호로 단편 포지션을 입력하고 goLong ((() 신호로 종료합니다.

교환 모드에서, 긴 포지션은 goLong (((에서 입력되고, 닫혀서 killLong (((에서 짧은 것으로 역전됩니다.

이 전략은 또한 스톱 로스, 트레일링 스톱, 메시징 및 기타 선택적 기능을 포함합니다.

장점

  1. 간단하고 쉽게 실행할 수 있습니다.

  2. 길거나 짧거나 둘 다 할 수 있는 유연성

  3. 선택적인 스톱 손실 및 트레일링 스톱 기능

  4. 트레이드를 알리기 위해 사용자 정의 가능한 메시지

  5. 시장의 트렌드 변화에 민감합니다.

  6. 조정 가능한 매개 변수는 다른 시장에 적응합니다.

위험성

  1. 불안정하거나 변화하는 시장에서 과도한 거래를 일으킬 수 있습니다.

  2. 갑작스러운 뉴스 사건에 반응하는 게 느려요

  3. 매개 변수 선택은 전략 성능에 영향을 미칩니다.

  4. 신호를 엄격히 따르고, 사유적인 거래를 피해야 합니다.

  5. 거래 비용은 고려되지 않으면 수익을 침식시킬 수 있습니다.

개선

  1. 잘못된 신호를 피하기 위해 RSI 같은 필터를 추가합니다.

  2. 가장 좋은 설정을 찾기 위해 매개 변수 최적화를 구현합니다.

  3. 이윤을 확보하고 조정하기 위해 동적 정지를 사용하세요.

  4. 트렌드 예측을 돕기 위해 기계 학습을 포함합니다.

  5. 개인 거래 습관에 맞는 메시징을 최적화합니다.

결론

이중 이동 평균 전략은 상대적으로 간단하고 강력한 트렌드를 잡는 데 유용합니다. 그러나 낮은 트렌드 환경에서 윙사브를 피하기 위해 주의가 필요합니다. 미세한 조정 매개 변수 및 보조 지표 또는 개선 사항을 추가하면 견고성과 적응력을 더욱 향상시킬 수 있습니다.


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

//@version=4

strategy("SMA Strategy", shorttitle="SMA Strategy", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// === Inputs ===
// short ma
maFastSource = input(defval=close, title="Fast MA Source")
maFastLength = input(defval=3, title="Fast MA Period", minval=1)

// long ma
maSlowSource = input(defval=close, title="Slow MA Source")
maSlowLength = input(defval=9, title="Slow MA Period", minval=1)

// Trade direction
shorting = input(defval=false, title="Short only?")
longonly = input(defval=true, title="Long only?")
swapping = input(defval=false, title="Swap orders?")
// risk management
useStop = input(defval=false, title="Use Initial Stop Loss?")
slPoints = input(defval=25, title="Initial Stop Loss Points", minval=1)
useTS = input(defval=false, title="Use Trailing Stop?")
tslPoints = input(defval=120, title="Trail Points", minval=1)
useTSO = input(defval=false, title="Use Offset For Trailing Stop?")
tslOffset = input(defval=20, title="Trail Offset Points", minval=1)

// Messages for buy and sell
message_long_entry  = input("Long entry message", title="Long entry message")
message_long_exit   = input("Long exit message", title="Long exit message")
message_short_entry = input("Short entry message", title="Short entry message")
message_short_exit  = input("Short exit message", title="Short exit message")

// Calculate start/end date and time condition
startDate  = input(timestamp("2021-01-01T00:00:00"), type = input.time)
finishDate = input(timestamp("2021-12-31T00:00:00"), type = input.time)
 
time_cond  = true
// === Vars and Series ===
fastMA = sma(maFastSource, maFastLength)
slowMA = sma(maSlowSource, maSlowLength)

plot(fastMA, color=color.blue)
plot(slowMA, color=color.purple)

goLong() =>
    crossover(fastMA, slowMA)
killLong() =>
    crossunder(fastMA, slowMA)
    
// Long only
if longonly
    strategy.entry("Buy", strategy.long, when=goLong() and time_cond, alert_message = message_long_entry)
    strategy.close("Buy", when=killLong() and time_cond, alert_message = message_long_exit)

// Short only
if shorting
    strategy.entry("Sell", strategy.short, when=killLong() and time_cond, alert_message = message_short_entry)
    strategy.close("Sell", when=goLong() and time_cond, alert_message = message_short_exit)
    
// Order Swapping
if swapping
    strategy.entry("Buy", strategy.long, when=goLong() and time_cond, alert_message = message_long_entry)
    strategy.entry("Sell", strategy.short, when=killLong() and time_cond, alert_message = message_short_entry)

if useStop
    strategy.exit("XLS", from_entry="Buy", stop=strategy.position_avg_price / 1.08, alert_message = message_long_exit)
    strategy.exit("XSS", from_entry="Sell", stop=strategy.position_avg_price * 1.08, alert_message = message_short_exit)



더 많은