다중 시간 규모 SMA 추세 추적 및 동적 손절매 전략

SMA Trend
생성 날짜: 2024-06-03 10:57:05 마지막으로 수정됨: 2024-06-03 10:57:05
복사: 3 클릭수: 553
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

다중 시간 규모 SMA 추세 추적 및 동적 손절매 전략

개요

이 전략은 다양한 시간 스케일의 간단한 이동 평균 (SMA) 을 기반으로 시장 추세를 포착한다. 단기 및 장기 SMA의 상대적 위치를 비교하여 구매 및 판매 신호를 생성한다. 동시에, 이 전략은 트렌드 확인 조건을 사용하여 가짜 신호를 필터링하여 거래 정확성을 향상시킵니다. 또한, 이 전략은 스톱 및 스톱 손실 기능을 설정하여 위험 관리를 수행한다.

전략 원칙

  1. 단기 및 장기 SMA를 계산하여 시장의 추세 방향을 판단한다.
  2. 단기 SMA 상에서 장기 SMA를 밟을 때, 구매 신호를 생성한다. 단기 SMA 아래에서 장기 SMA를 밟을 때, 판매 신호를 생성한다.
  3. 트렌드 확인 조건을 사용하여 가짜 신호를 필터링하고, 주요 트렌드가 다목적일 때만 구매를 실행하고, 주요 트렌드가 공허일 때만 판매를 실행한다.
  4. 스톱 및 스톱 손실 기능을 설정하여 거래 위험을 제어하십시오. 가격이 미리 설정된 스톱 또는 스톱 레벨에 도달하면 포지션을 종료하십시오.
  5. 트렌드 확인 조건에 따라 역동적으로 포지션을 조정한다. 주요 트렌드가 변할 때, 적시에 포지션을 청산하고, 트렌드 반전의 손실을 방지한다.

전략적 이점

  1. 트렌드 추적: 이 전략은 다양한 시간 단위의 SMA를 기반으로 시장의 주요 트렌드를 효과적으로 포착하고 다양한 시장 상태에 적응합니다.
  2. 트렌드 확인: 트렌드 확인 조건을 도입하여 가짜 신호를 필터링하여 거래 신호의 신뢰성을 높이고 유효하지 않은 거래를 줄입니다.
  3. 리스크 관리: 내장된 중지 및 중지 기능으로 거래 위험을 제어하고 투자자의 자금을 보호합니다.
  4. 동적 조정: 트렌드 확인 조건에 따라 동적으로 포지션을 조정하고, 시장 변화에 신속하게 대응하여 트렌드 역전으로 인한 손실을 줄인다.

전략적 위험

  1. 매개 변수 최적화 위험: 이 전략의 성능은 SMA 주기, 정지 손해 평준 매개 변수의 선택에 의존한다. 부적절한 매개 변수 설정은 전략의 효과가 좋지 않을 수 있다.
  2. 흔들리는 시장 위험: 흔들리는 시장 환경에서, 거래 신호의 빈도는 과도한 거래로 이어지고 거래 비용과 위험을 증가시킬 수 있습니다.
  3. 급격한 사건 위험: 급격한 중대한 사건에 직면하여 시장이 급격하게 변동할 수 있으며, 이 전략은 적시에 대응하지 못할 수 있으며, 큰 손실을 초래할 수 있습니다.

전략 최적화 방향

  1. 더 많은 기술 지표를 도입: MACD, RSI 등과 같은 다른 기술 지표와 결합하여 트렌드 판단의 정확성과 안정성을 향상시킵니다.
  2. 최적화 파라미터 선택: 역사 데이터 회수 및 파라미터 최적화를 통해 최적의 SMA 주기를 찾고, 스톱 스톱 손실 수평 파라미터 조합을 사용하여 전략 성능을 향상시킵니다.
  3. 위험 관리 개선: 더 고급 위험 관리 기술을 도입하여 위험 경로를 더욱 제어합니다.
  4. 다른 시장 상태에 적응: 시장의 변동성과 트렌드 강도에 따라 전략 매개 변수를 동적으로 조정하여 전략이 다른 시장 상태에 적응할 수 있도록합니다.

요약하다

이 다중 시간 척도 SMA 트렌드 추적 및 동적 중지 전략은 다른 시간 척도의 SMA를 사용하여 시장 트렌드를 포착하고, 트렌드 확인 조건을 통해 가짜 신호를 필터링하며, 동적 위치 조정 기능을 설정하면서 트렌드 추적 및 위험 관리의 목표를 달성합니다. 이 전략은 장점이 있지만, 여전히 변수 최적화, 흔들리는 시장 및 갑작스러운 이벤트와 같은 위험에 직면합니다.

전략 소스 코드
/*backtest
start: 2024-05-01 00:00:00
end: 2024-05-31 23:59:59
period: 6h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("market slayer v3", overlay=true)

// Input parameters
showConfirmationTrend = input(title='Show Trend', defval=true)
confirmationTrendTimeframe = input.timeframe(title='Main Trend', defval='240')
confirmationTrendValue = input(title='Main Trend Value', defval=2)
showConfirmationBars = input(title='Show Confirmation Bars', defval=true)
topCbarValue = input(title='Top Confirmation Value', defval=60)
short_length = input.int(10, minval=1, title="Short SMA Length")
long_length = input.int(20, minval=1, title="Long SMA Length")
takeProfitEnabled = input(title="Take Profit Enabled", defval=false)
takeProfitValue = input.float(title="Take Profit (points)", defval=20, minval=1)
stopLossEnabled = input(title="Stop Loss Enabled", defval=false)
stopLossValue = input.float(title="Stop Loss (points)", defval=50, minval=1)

// Calculate SMAs
short_sma = ta.sma(close, short_length)
long_sma = ta.sma(close, long_length)

// Generate buy and sell signals based on SMAs
buy_signal = ta.crossover(short_sma, long_sma)
sell_signal = ta.crossunder(short_sma, long_sma)

// Plot SMAs
plot(short_sma, color=color.rgb(24, 170, 11), title="Short SMA")
plot(long_sma, color=color.red, title="Long SMA")

// Confirmation Bars
f_confirmationBarBullish(cbValue) =>
    cBarClose = close
    slowConfirmationBarSmaHigh = ta.sma(high, cbValue)
    slowConfirmationBarSmaLow = ta.sma(low, cbValue)
    slowConfirmationBarHlv = int(na)
    slowConfirmationBarHlv := cBarClose > slowConfirmationBarSmaHigh ? 1 : cBarClose < slowConfirmationBarSmaLow ? -1 : slowConfirmationBarHlv[1]
    slowConfirmationBarSslDown = slowConfirmationBarHlv < 0 ? slowConfirmationBarSmaHigh : slowConfirmationBarSmaLow
    slowConfirmationBarSslUp = slowConfirmationBarHlv < 0 ? slowConfirmationBarSmaLow : slowConfirmationBarSmaHigh
    slowConfirmationBarSslUp > slowConfirmationBarSslDown

fastConfirmationBarBullish = f_confirmationBarBullish(topCbarValue)
fastConfirmationBarBearish = not fastConfirmationBarBullish
fastConfirmationBarClr = fastConfirmationBarBullish ? color.green : color.red

fastConfirmationChangeBullish = fastConfirmationBarBullish and fastConfirmationBarBearish[1]
fastConfirmationChangeBearish = fastConfirmationBarBearish and fastConfirmationBarBullish[1]

confirmationTrendBullish = request.security(syminfo.tickerid, confirmationTrendTimeframe, f_confirmationBarBullish(confirmationTrendValue), lookahead=barmerge.lookahead_on)
confirmationTrendBearish = not confirmationTrendBullish
confirmationTrendClr = confirmationTrendBullish ? color.green : color.red

// Plot trend labels
plotshape(showConfirmationTrend, style=shape.square, location=location.top, color=confirmationTrendClr, title='Trend Confirmation Bars')
plotshape(showConfirmationBars and (fastConfirmationChangeBullish or fastConfirmationChangeBearish), style=shape.triangleup, location=location.top, color=fastConfirmationChangeBullish ? color.green : color.red, title='Fast Confirmation Bars')
plotshape(showConfirmationBars and buy_signal and confirmationTrendBullish, style=shape.triangleup, location=location.top, color=color.green, title='Buy Signal')
plotshape(showConfirmationBars and sell_signal and confirmationTrendBearish, style=shape.triangledown, location=location.top, color=color.red, title='Sell Signal')

// Generate trade signals
buy_condition = buy_signal and confirmationTrendBullish and not (strategy.opentrades > 0)
sell_condition = sell_signal and confirmationTrendBearish and not (strategy.opentrades > 0)

strategy.entry("Buy", strategy.long, when=buy_condition, comment ="BUY CALLS")
strategy.entry("Sell", strategy.short, when=sell_condition, comment ="BUY PUTS")

// Take Profit
if (takeProfitEnabled)
    strategy.exit("Take Profit Buy", from_entry="Buy", profit=takeProfitValue)
    strategy.exit("Take Profit Sell", from_entry="Sell", profit=takeProfitValue)

// Stop Loss
if (stopLossEnabled)
    strategy.exit("Stop Loss Buy", from_entry="Buy", loss=stopLossValue)
    strategy.exit("Stop Loss Sell", from_entry="Sell", loss=stopLossValue)

// Close trades based on trend confirmation bars
if strategy.opentrades > 0
    if strategy.position_size > 0
        if not confirmationTrendBullish
            strategy.close("Buy", comment ="CLOSE CALLS")
    else
        if not confirmationTrendBearish
            strategy.close("Sell", comment ="CLOSE PUTS")

// Define alert conditions as booleans
buy_open_alert = buy_condition
sell_open_alert = sell_condition
buy_closed_alert = strategy.opentrades < 0
sell_closed_alert = strategy.opentrades > 0

// Alerts
alertcondition(buy_open_alert, title='Buy calls', message='Buy calls Opened')
alertcondition(sell_open_alert, title='buy puts', message='buy Puts Opened')
alertcondition(buy_closed_alert, title='exit calls', message='exit calls ')
alertcondition(sell_closed_alert, title='exit puts', message='exit puts Closed')