動的移動平均を追跡する戦略

作者: リン・ハーンチャオチャン,日付: 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)

もっと