동적 이동 평균 추적 전략

저자:차오장, 날짜: 2023-11-24 16:59:25
태그:

img

전반적인 설명

이 전략의 핵심 아이디어는 트렌드 추적을 위해 동적 이동 평균을 사용하여 스톱 로스를 설정하고 이익을 취하고 긴 / 짧은 신호 판단을 위해 하이킨 아시 촛불 기법을 결합하는 것입니다. ATR 지표는 동적 이동 평균과 스톱 로스 위치를 계산하는 데 사용됩니다.

원칙

이 전략은 먼저 ATR 지표를 계산하고, 입력 가격 소스와 매개 변수를 결합하여 동적 이동 평균을 계산합니다. 가격이 동적 이동 평균 이상/하위를 넘을 때 긴/단기 신호가 생성됩니다. 한편, 스톱 로스 및 수익 포지션은 실시간으로 가격 변화를 추적하도록 설정됩니다.

특히, ATR 지표와 매개 변수 nLoss는 먼저 계산된다. 그 다음 현재 기간의 가격과 이전 기간의 스톱 로스 포지션은 스톱 로스 라인을 업데이트하기 위해 비교된다. 가격이 이전 기간의 스톱 로스 라인을 통과할 때, 긴/단 신호 pos와 대응 색상이 생성된다. 거래 신호가 트리거될 때 화살표가 그려진다. 마지막으로 포지션은 스톱 로스/프로프트 로직을 기반으로 닫는다.

이점 분석

이 전략의 가장 큰 장점은 동적 이동 평균을 사용하여 실시간으로 가격 변화를 추적하는 것입니다. 이것은 전통적인 고정 이동 평균보다 트렌드를 더 잘 파악하고 스톱 손실의 가능성을 줄여줍니다. 또한 ATR 기반 스톱 손실을 결합하면 시장 변동성에 따라 스톱 손실 위치를 유연하게 조정하여 위험을 효과적으로 제어 할 수 있습니다.

위험 과 해결책

이 전략의 주요 위험은 가격이 크게 상승/하락 할 수 있으며, 스톱 로스가 타면 잘못된 신호를 유발할 수 있습니다. 또한 부적절한 조건 설정은 과도하게 빈번한 거래로 이어질 수 있습니다.

솔루션은 이동 평균 기간을 최적화하고, 잘못된 신호의 확률을 낮추기 위해 ATR 및 중지 손실 계수를 조정하는 것을 포함합니다. 과도하게 밀도가 높은 거래를 피하기 위해 추가 필터를 추가 할 수 있습니다.

최적화 방향

이 전략은 다음과 같은 측면에서 최적화 될 수 있습니다.

  1. 최적의 매개 변수 조합을 찾기 위해 이동 평균의 다양한 유형과 기간을 테스트

  2. 스톱 손실 감수성을 균형을 맞추기 위해 ATR 기간 매개 변수를 최적화합니다.

  3. 신호 품질을 향상시키기 위해 추가 필터와 표시기를 추가합니다.

  4. 리스크 보상 비율을 최적화하기 위해 스톱 로스/프로프트 취득 값을 조정합니다

결론

이 전략의 핵심 아이디어는 동적 이동 평균을 사용하여 실시간으로 가격 변화를 추적하고, ATR 지표를 활용하여 동적으로 스톱 로스 포지션을 설정하고, 트렌드를 추적하는 동안 위험을 엄격하게 제어하는 것입니다. 매개 변수 최적화 및 규칙 정리를 통해이 전략을 매우 실용적인 수치 시스템에 조정 할 수 있습니다.


/*backtest
start: 2022-11-23 00:00:00
end: 2023-11-05 05:20:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"BTC_USDT","stocks":0}]
*/

//@version=5
strategy(title='UT Bot v5', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
//CREDITS to HPotter for the orginal code. The guy trying to sell this as his own is a scammer lol. 
//Edited and converted to @version=5 by SeaSide420 for Paperina
// Inputs
AllowBuy = input(defval=true, title='Allow Buy?')
AllowSell = input(defval=false, title='Allow Sell?')
h = input(false, title='Signals from Heikin Ashi Candles')
//revclose = input(defval=true, title='Close when reverse signal?')
Price = input(defval=open, title='Price Source (recommended OPEN to avoid repainting)')
smoothing = input.string(title="Moving Average Type", defval="HMA", options=["SMA", "EMA", "WMA", "HMA"])
MA_Period = input(2, title='This changes the MAPeriod')
a = input.float(1, title='This changes the sensitivity',step=0.1)
c = input(11, title='ATR Period')
TakeProfit = input.int(defval=50000, title='Take Profit ($)', minval=1)
StopLoss = input.int(defval=50000, title='Stop Loss ($)', minval=1)
xATR = ta.atr(c)
nLoss = a * xATR
src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, Price, lookahead=barmerge.lookahead_off) : Price
xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2
pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3
xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
ma_function(src, MA_Period) =>
    switch smoothing
        "SMA" => ta.sma(src, MA_Period)
        "EMA" => ta.ema(src, MA_Period)
        "WMA" => ta.wma(src, MA_Period)
        => ta.hma(src, MA_Period)
thema = ma_function(src, MA_Period)
above = ta.crossover(thema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, thema)
buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below
barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop
plot(thema,title="The M.A.",color=color.green,linewidth=2)
plot(xATRTrailingStop,title="The M.A.",color=color.red,linewidth=2)
plotshape(buy,  title = "Buy",  text = "Buy",  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, size = size.tiny)
plotshape(sell, title = "Sell", text = "Sell", style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, size = size.tiny)
barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)
strategy.close_all(when=strategy.openprofit>TakeProfit,alert_message="Close- TakeProfit", comment = "TP")
strategy.close_all(when=strategy.openprofit<StopLoss-(StopLoss*2),alert_message="Close- StopLoss", comment = "SL")
strategy.close("buy", when =  sell and AllowSell==false , comment = "close buy")
strategy.close("sell", when =  buy and AllowBuy==false, comment = "close sell")
strategy.entry("buy", strategy.long, when = buy and AllowBuy)
strategy.entry("sell", strategy.short, when = sell and AllowSell)

더 많은