遅延したRSI取引戦略

作者: リン・ハーンチャオチャン,日付: 2023年10月7日15時38分56秒
タグ:

概要

遅れたRSI取引戦略は,従来のRSI指標を使用して,過剰購入および過剰販売状態を特定し,シグナルが出た後一定期間市場に入場を遅らせ,偽のブレイクアウトによる不必要な損失を回避する.この戦略の主な考えは,RSI指標を使用して過剰購入および過剰販売市場状況を判断し,この判断に基づいてエントリーを遅らせることでより正確なエントリータイミングを達成することです.

戦略の論理

この戦略は,過買い・過売状態を決定するために21期間のRSI指標を使用する.RSI指標がユーザー定義過買いレベル (デフォルト60) を越えると,市場は過買いとみなされる.RSIがユーザー定義過売レベル (デフォルト40) を越えると,市場は過売とみなされる.

過剰購入または過剰販売のシグナルを特定した後,戦略はすぐに市場に参入しません.代わりに,遅延期間を数え始めます.遅延期間 (デフォルト15バー) が満たされると,過剰購入のシグナルに基づいてショート,過剰販売のシグナルに基づいてロングに入ります.

この戦略は,ユーザーが異なるエントリータイミングを達成するために遅延期間を調整することを可能にします.より長い遅延期間は,より多くの偽のブレイクアウトを回避できますが,より良いエントリー機会も逃す可能性があります.ユーザーは特定の製品の特徴に基づいて遅延期間パラメータを調整する必要があります.

また,ストップ・ロスト,テイク・プロフィート,リバース・トレードなどオプションも実装している.ユーザーはポジションを管理するために,固定ストップ・ロスト,トレーリング・ストップ・ロスト,固定テイク・プロフィートなどを選択することができます.取引論理は逆転することもできます.つまり,オーバー買い信号でロング,オーバーセール信号でショート.

利点

  1. RSI インディケーターを使用することで,過買い/過売り状態を正確に特定し,逆転の機会を把握できます.RSIは,逆転を特定するために広く使用されている成熟した振動器です.

  2. 遅延入場は偽のブレイクアウトによる損失を回避する.多くのブレイクアウトは必ずしも実際の逆転につながらない.遅延入場は有効性を確認する.

  3. 調整可能な遅延期間は,正確なエントリータイミングを可能にします.ユーザーは,最高のエントリーのために製品特性に基づいて遅延期間を最適化することができます.

  4. リスク管理のためにストップ・ロスを実行し,利益を上げます. 戦略はリスク管理のための固定SL/TP,トライリングSLなど,複数の方法を提供しています.

  5. リバース・トレードオプションは,異なる製品に適応します.ユーザーは不確実性をヘッジするために通常の論理またはリバース・ロジックを選択できます.

リスク

  1. RSIからの偽信号の危険性 RSI信号は必ずしも正確ではなく,時には偽信号を与える可能性があります.

  2. 遅延が長すぎると機会が逃れるリスクがあります. 遅延が長すぎると,入口ポイントが逃れる可能性があります.

  3. リバース・トレードによる損失リスクが増加します.リバース・トレードが不確実性をカバーする一方で,総損失も増幅する可能性があります.

  4. SLが近づいてきて 早期に止められるリスクがあります

  5. 固定TPが不正確で利益が不十分で,固定TPは最大利益を達成できないため,合理的な予測が必要である.

これらのリスクに対処するために,最適化提案は以下のとおりです.

  1. 信頼性を高めるため,KDJ,MACDなどの他の指標で RSI信号をフィルターします.

  2. 過去のデータでバックテストして,それぞれの製品に最適な遅延期間を見つける.

  3. 逆の論理を慎重に使って 傾向を追うことを好みます

  4. 価格が近づかないように,SLを後押しする大きなバッファを保持する.

  5. TP比をテストして,最適な値を見つけます.また,ダイナミック・テイク・プロフィートも考慮してください.

最適化 の 機会

この戦略は,次の側面においてさらに最適化することができる.

  1. 複数の指標を組み合わせて入力信号をフィルタリングします.例えば,KDJ,MACDとRSIを組み合わせることで,より強力な信号が得られます.

  2. 市場の変動に基づいて遅延期間を動的に調整します.これは入口精度を向上させながら偽のブレイクアウトを回避します.

  3. ダイナミック・SL,リターン・リターン・レート・SL,タイム・ベース・SLなど,SL/TP戦略を最適化し,市場変動に適応させる.

  4. トレンドを組み込む. 突破方向が主要なトレンドと一致するかどうかを測定する. また突破勢力のベースで遅延期間を調整する.

  5. 機械学習を使用して最適なパラメータの組み合わせを見つけます. MLは大規模なトレーニングとバックテストデータセットに基づいてパラメータを自動的に調整できます.

結論として,この戦略は,指標コンボ,パラメータのダイナミックな調整,トレンド統合などを通じて最適化するための十分な余地があります. MLは,今後も有望な方向です.

概要

遅延RSI戦略は,全体的にRSIを利用して過買い/過売り状態を特定し,シグナルが発生した後,不必要な損失を避けるために入力を遅らせます.この戦略には,正確な信号識別,誤ったブレイクを避けるために遅延入入力の利点,調整可能な遅延期間,SL/TP実装などがあります.しかし,信頼性のないRSI信号,過度の遅延による機会の逃れなどのリスクがあります.これらのリスクは,指標コンボ,ダイナミック遅延期チューニング,より良いSL/TP戦略などを通じて信号の精度を最適化することによってさらに改善できます.この戦略には幅広い最適化機会があり,探求に値します.


/*backtest
start: 2023-01-01 00:00:00
end: 2023-10-06 00:00:00
period: 1d
basePeriod: 1h
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/
// © tweakerID and © BacktestRookies

// This strategy uses a 21 period RSI with an overbought (RSI indicator 
// is greater than) level of 60 (user defined) to determines long entries and an oversold 
// (RSI indicator is less than) level of 40 (user defined) for shorts. It introduces a bar delay that starts
// counting when the RSI < Oversold or RSI > Overbought conditions are true, delaying the entry with 
// the amount of bars determined by the user. The trading logic can be reversed, which seems to work better.

//@version=4
strategy("Delayed RSI Strategy", 
     overlay=false, 
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=100, 
     initial_capital=10000, 
     commission_value=0.04, 
     calc_on_every_tick=false, 
     slippage=0)
     
direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)
strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : 
 (direction < 0 ? strategy.direction.short : strategy.direction.long))

// Bought and Sold Boolean Signal
bought = strategy.position_size > strategy.position_size[1] 
 or strategy.position_size < strategy.position_size[1]

/////////////////////// STRATEGY INPUTS ////////////////////////////////////////
title1=input(true, "-----------------Strategy Inputs-------------------")  

rsiLen=input(21, title="RSI Length")
i_OB = input(60, title="Overbought")
i_OS = input(40, title="Oversold")
i_delay = input(15, title="Entry Delay (# of Bars)")
i_Close= input(false, title="Use Strategy Close")

/////////////////////// BACKTESTER /////////////////////////////////////////////
title2=input(true, "-----------------General Inputs-------------------")  

// Backtester General Inputs
i_SL=input(true, title="Use Stop Loss and Take Profit")
TS=input(false, title="Use Trailing Stop")
i_SLType=input(defval="ATR Stop", title="Type Of Stop", options=["Strategy Stop", "Swing Lo/Hi", "ATR Stop"])
i_SPL=input(defval=10, title="Swing Point Lookback")
i_PercIncrement=input(defval=3, step=.1, title="Swing Point SL Perc Increment")*0.01
i_ATR = input(14, title="ATR Length")
i_ATRMult = input(3, step=.1, title="ATR Multiple")
i_TPRRR = input(2, step=.1, title="Take Profit Risk Reward Ratio")
DPR=input(false, "Allow Direct Position Reverse")
reverse=input(true, "Reverse Trades")

// Swing Points Stop and Take Profit
SwingStopProfit() =>
    LL=(lowest(i_SPL))*(1-i_PercIncrement)
    HH=(highest(i_SPL))*(1+i_PercIncrement)
    LL_price = valuewhen(bought, LL, 0)
    HH_price = valuewhen(bought, HH, 0)
    entry_LL_price = strategy.position_size > 0 ? LL_price : na 
    entry_HH_price = strategy.position_size < 0 ? HH_price : na 
    tp=strategy.position_avg_price + (strategy.position_avg_price - entry_LL_price)*i_TPRRR
    stp=strategy.position_avg_price - (entry_HH_price - strategy.position_avg_price)*i_TPRRR
    [entry_LL_price, entry_HH_price, tp, stp]

// ATR Stop
ATRStop() =>
    ATR=atr(i_ATR)*i_ATRMult
    ATRLong = ohlc4 - ATR
    ATRShort = ohlc4 + ATR
    ATRLongStop = valuewhen(bought, ATRLong, 0)
    ATRShortStop = valuewhen(bought, ATRShort, 0)
    LongSL_ATR_price = strategy.position_size > 0 ? ATRLongStop : na 
    ShortSL_ATR_price = strategy.position_size < 0 ? ATRShortStop : na 
    ATRtp=strategy.position_avg_price + (strategy.position_avg_price - LongSL_ATR_price)*i_TPRRR
    ATRstp=strategy.position_avg_price - (ShortSL_ATR_price - strategy.position_avg_price)*i_TPRRR
    [LongSL_ATR_price, ShortSL_ATR_price, ATRtp, ATRstp]
    
// Strategy Stop
StrategyStop(bought) =>
    float LongStop = na
    float ShortStop = na
    float StratTP = na
    float StratSTP = na
    [LongStop, ShortStop, StratTP, StratSTP]

//TrailingStop
TrailingStop(SL,SSL) =>
    dif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0))
     -strategy.position_avg_price
    trailOffset     = strategy.position_avg_price - SL
    var tstop = float(na)
    if strategy.position_size > 0
        tstop := high- trailOffset - dif
        if tstop<tstop[1]
            tstop:=tstop[1]
    else
        tstop := na
    StrailOffset     = SSL - strategy.position_avg_price
    var Ststop = float(na)
    Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0 
     and strategy.position_size[1]>=0, low,0))
    if strategy.position_size < 0
        Ststop := low+ StrailOffset + Sdif
        if Ststop>Ststop[1]
            Ststop:=Ststop[1]
    else
        Ststop := na
    [tstop, Ststop]
  
//Stop Loss & Take Profit Switches  
SLTPLogic(LongStop, ShortStop, StratTP, StratSTP, LongSL_ATR_price, ShortSL_ATR_price, ATRtp, ATRstp,
 entry_LL_price, entry_HH_price, tp, stp) =>
    SL= i_SLType == "Swing Lo/Hi" ? entry_LL_price : i_SLType == "ATR Stop" ? LongSL_ATR_price : LongStop
    SSL= i_SLType == "Swing Lo/Hi" ? entry_HH_price : i_SLType == "ATR Stop" ? ShortSL_ATR_price : ShortStop
    TP= i_SLType == "Swing Lo/Hi" ? tp : i_SLType == "ATR Stop" ? ATRtp : StratTP
    STP= i_SLType == "Swing Lo/Hi" ? stp : i_SLType == "ATR Stop" ? ATRstp : StratSTP
    [SL, SSL, TP, STP]


/////////////////////// STRATEGY LOGIC /////////////////////////////////////////

rsi = rsi(close, rsiLen)
isOB= rsi > i_OB
isOS= rsi < i_OS
BarsSinceOB = barssince(not isOB)
BarsSinceOS = barssince(not isOS)

BUY = BarsSinceOS == i_delay
SELL = BarsSinceOB == i_delay

/////////////////////// FUNCTION CALLS /////////////////////////////////////////

// Stops and Profits
[entry_LL_price, entry_HH_price, tp, stp] = SwingStopProfit()
[LongSL_ATR_price, ShortSL_ATR_price, ATRtp, ATRstp] = ATRStop()
[LongStop, ShortStop, StratTP, StratSTP] = StrategyStop(bought)
[SL, SSL, TP, STP] = SLTPLogic(LongStop, ShortStop, StratTP, StratSTP, 
 LongSL_ATR_price, ShortSL_ATR_price, ATRtp, ATRstp, entry_LL_price, entry_HH_price, tp, stp)
[tstop, Ststop] = TrailingStop(SL,SSL)

// Entries
if reverse
    if not DPR
        strategy.entry("long", strategy.long, when=SELL and strategy.position_size == 0)
        strategy.entry("short", strategy.short, when=BUY and strategy.position_size == 0)
    else     
        strategy.entry("long", strategy.long, when=SELL)
        strategy.entry("short", strategy.short, when=BUY)
else
    if not DPR 
        strategy.entry("long", strategy.long, when=BUY and strategy.position_size == 0)
        strategy.entry("short", strategy.short, when=SELL and strategy.position_size == 0)
    else
        strategy.entry("long", strategy.long, when=BUY)
        strategy.entry("short", strategy.short, when=SELL)
// Exits
if i_SL
    strategy.exit("TP & SL", "long", limit=TP, stop=TS? tstop : SL)
    strategy.exit("TP & SL", "short", limit=STP, stop=TS? Ststop : SSL)
    
if i_Close
    strategy.close_all(when=cross(rsi, 50))

/////////////////////// PLOTS //////////////////////////////////////////////////

//Plots
rsiplot = plot(rsi, "RSI", color=#7E57C2)
band1 = hline(i_OB, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(i_OS, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")
plot(rsi, "RSI", color=#7E57C2)
// OSOBCount = plot(isOB ? BarsSinceOB : isOS ? BarsSinceOS : na, transp=100)
// OSOBColor = color.from_gradient(isOB ? BarsSinceOB : BarsSinceOS, 0, 20, color.black, isOB ? color.red : isOS ? color.green : na)
// OBP = plot(rsi > i_OB ? rsi : na, color=color.white, display=display.none)
// fill(plot(i_OB, display=display.none), OBP, color=OSOBColor, transp=0, fillgaps=false)
// OSP = plot(rsi < i_OS ? rsi : na, color=color.white, display=display.none)
// fill(plot(i_OS, display=display.none), OSP, color=OSOBColor, transp=0, fillgaps=false)

// plotshape(BUY ? 1 : na, style=shape.arrowdown, location=location.bottom, 
//  color=color.green, title="Bullish Setup", size=size.normal)
// plotshape(SELL ? 1 : na, style=shape.arrowup, location=location.top, 
//  color=color.red, title="Bearish Setup", size=size.normal)



もっと