休止範囲逆転戦略

作者: リン・ハーンチャオチャン開催日:2023年10月30日 10:54:00
タグ:

img

概要

休眠範囲逆転戦略は,変動が減少する期間をエントリー信号として利用し,変動が再び上昇すると利益を得ることを目的としています. 価格が狭い休眠範囲内に収束している状況を特定し,来年の価格傾向を把握します. 現在の変動が低く,ブレイクアウトが予想される場合,この戦略はうまく機能します.

戦略の論理

戦略は,まず,価格が前の取引日の価格範囲内に収まっているときの休眠範囲を特定します.これは,数日前と比較して変動が減少したことを示します.現在の日の高値 < n日前の高値 (通常は4日) と現在の日の低値 > n日前の低値が休眠範囲に該当するかどうかを確認します.

眠っている範囲が特定されると,戦略は2つの待機注文を - 範囲の上部近くで購入ストップと範囲の下部近くで販売ストップを - 配置する.その後,価格が上方または下方に範囲を突破するのを待つ.価格が上方に突破した場合,購入注文はロングに行くように誘発される.価格が下方に突破した場合,販売注文はショートに行くように誘発される.

入場後,ストップ・ロストとテイク・プロフィート・オーダーが配置される.ストップ・ロスはダウンサイドリスクを制御し,テイク・プロフィートは利益のために取引を閉じる.ストップ・ロスはリスクパラメータで定義されたエントリー価格から%の距離に置かれる.テイク・プロフィートは,価格が以前の変動に類似して動いていることを期待しているため,休止範囲のサイズに等しい距離に置かれる.

最後に,固定分数式ポジションサイジングモデルは取引のサイズを管理します.リスク調整収益を改善するために,利益のサイズを増加させ,損失のサイズを減らす.

利点

この戦略の利点は次のとおりです.

  1. 波動性の低下を信号として利用して,次の傾向を把握します.

  2. 双方向のオーダーは 上昇傾向か 下降傾向を捉える.

  3. ストップ・ロスト・アンド・テイク・プロフィット 単一の取引リスクを制御する

  4. 固定的な分数的なサイズ化により 資本効率が向上します

  5. シンプルな論理で 簡単に実行できます

リスク

考慮すべきリスクは:

  1. 範囲が不明な場合は 誤った突破方向

  2. 突破は短期的な逆転であり 持続的な傾向ではないかもしれません

  3. ストップ・ロスは大きな動きによって取り除かれるリスクです

  4. 固定分数のサイズ化により損失が増加します.

  5. パラメータが正しく設定されていない場合,性能が悪い.

増進 の 機会

戦略の強化の方法:

  1. 偽の突破を避けるため フィルターを追加します

  2. ストップ・ロスを改善し,ストップ・ロスを追いかけるか,ストップ・ロスを注文する.

  3. 逆トレンドのエントリを避けるためにトレンドフィルターを追加します.

  4. リスク/報酬のバランスをとるために固定分数の比率を最適化します

  5. 利点を向上させるため 複数の時間枠を見てください

  6. 自動パラメータ最適化のために機械学習を利用する.

結論

休止範囲逆転戦略には明確な論理と利益の可能性があります.最適化,リスク管理,シグナルフィルタリングによる微調整により一貫性がさらに向上できます.しかし,すべての平均逆転戦略には固有のリスクがあり,ポジションサイズが制御する必要があります.逆転戦術に精通し,健全なリスク意識を持っているトレーダーに適しています.


/*backtest
start: 2023-09-29 00:00:00
end: 2023-10-29 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/
// © gsanson66


//This code is based on the Narrow Range strategy
//Interactive Broker fees are applied on this strategy
//@version=5
strategy("NARROW RANGE BACKTESTING", shorttitle="NR BACKTESTING", overlay=true, initial_capital=1000, default_qty_type=strategy.fixed, commission_type=strategy.commission.percent, commission_value=0.18)


//--------------------------------FUNCTIONS------------------------------------//

//@function to print label
debugLabel(txt, color) =>
    label.new(bar_index, high, text = txt, color=color, style = label.style_label_lower_right, textcolor = color.black, size = size.small)

//@function which looks if the close date of the current bar falls inside the date range
inBacktestPeriod(start, end) => (time >= start) and (time <= end)


//--------------------------------USER INPUTS------------------------------------//

//Narrow Range Length 
nrLength = input.int(4, minval=2, title="Narrow Range Length", group="Strategy parameters")
//Risk Management
stopLossInput = input.float(0.5, title="Stop Loss (in percentage of reference range)", group="Strategy parameters")
//Money Management
fixedRatio = input.int(defval=400, minval=1, title="Fixed Ratio Value ($)", group="Money Management")
increasingOrderAmount = input.int(defval=200, minval=1, title="Increasing Order Amount ($)", group="Money Management")
//Backtesting period
startDate = input(title="Start Date", defval=timestamp("1 Janv 2020 00:00:00"), group="Backtesting Period")
endDate = input(title="End Date", defval=timestamp("1 July 2024 00:00:00"), group="Backtesting Period")


//--------------------------------VARIABLES INITIALISATION--------------------------//
strategy.initial_capital = 50000
bool nr = na
var bool long = na
var bool short = na
var float stopPriceLong = na
var float stopLossLong = na
var float takeProfitLong = na
var float stopPriceShort = na
var float stopLossShort = na
var float takeProfitShort = na
var float takeProfit = na
var float stopLoss = na
bool inRange = na
int closedtrades = strategy.closedtrades
equity = math.abs(strategy.equity - strategy.openprofit)
var float capital_ref = strategy.initial_capital
var float cashOrder = strategy.initial_capital * 0.95


//------------------------------CHECKING SOME CONDITIONS ON EACH SCRIPT EXECUTION-------------------------------//

//Checking if the date belong to the range
inRange := true

//Checking performances of the strategy
if equity > capital_ref + fixedRatio
    spread = (equity - capital_ref)/fixedRatio
    nb_level = int(spread)
    increasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder + increasingOrder
    capital_ref := capital_ref + nb_level*fixedRatio
if equity < capital_ref - fixedRatio
    spread = (capital_ref - equity)/fixedRatio
    nb_level = int(spread)
    decreasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder - decreasingOrder
    capital_ref := capital_ref - nb_level*fixedRatio

//We check if a trade has been closed to cancel all previous orders
if closedtrades > closedtrades[1]
    strategy.cancel("Long")
    strategy.cancel("Short")
    stopPriceLong := na
    stopPriceShort := na

//Checking if we close all trades in case where we exit the backtesting period
if strategy.position_size!=0 and not inRange
    debugLabel("END OF BACKTESTING PERIOD : we close the trade", color=color.rgb(116, 116, 116))
    strategy.close_all()
    long := na
    short := na
    stopPriceLong := na
    stopLossLong := na
    takeProfitLong := na
    stopPriceShort := na
    stopLossShort := na
    takeProfitShort := na
    takeProfit := na
    stopLoss := na

//----------------------------------FINDING NARROW RANGE DAY------------------------------------------//

// We find the Narrow Range Day
if low > low[nrLength] and high < high[nrLength]
    nr := true 


//------------------------------------STOP ORDERS--------------------------------------------//

// We handle plotting of stop orders and cancellation of other side order if one order is triggered
if strategy.position_size > 0 and not na(stopPriceLong) and not na(stopPriceShort)
    long := true
    strategy.cancel("Short")
    stopPriceLong := na
    stopPriceShort := na
    takeProfit := takeProfitLong
    stopLoss := stopLossLong
if strategy.position_size < 0 and not na(stopPriceLong) and not na(stopPriceShort)
    short := true
    strategy.cancel("Long") 
    stopPriceLong := na
    stopPriceShort := na
    takeProfit := takeProfitShort
    stopLoss := stopLossShort


//------------------------------------STOP LOSS & TAKE PROFIT--------------------------------//

// If an order is triggered we plot TP and SL
if not na(takeProfit) and not na(stopLoss) and long
    if high >= takeProfit and closedtrades == closedtrades[1] + 1
        takeProfit := na
        stopLoss := na
        long := na
    if low <= stopLoss and closedtrades == closedtrades[1] + 1
        takeProfit := na
        stopLoss := na
        long := na
if not na(takeProfit) and not na(stopLoss) and short
    if high >= stopLoss and closedtrades == closedtrades[1] + 1
        takeProfit := na
        stopLoss := na
        short := na
    if low <= takeProfit and closedtrades == closedtrades[1] + 1
        takeProfit := na
        stopLoss := na
        short := na


//-----------------------------LONG/SHORT CONDITION-------------------------//

// Conditions to create two stop orders (one for Long and one for Short) and SL & TP calculation
if nr and inRange and strategy.position_size == 0
    stopPriceLong := high[4]
    takeProfitLong := high[4] + (high[4] - low[4])
    stopLossLong := high[4] - (high[4] - low[4])*stopLossInput
    qtyLong = cashOrder/stopPriceLong
    strategy.entry("Long", strategy.long, qtyLong, stop=stopPriceLong)
    strategy.exit("Exit Long", "Long", limit=takeProfitLong ,stop=stopLossLong)
    stopPriceShort := low[4]
    takeProfitShort := low[4] - (high[4] - low[4])
    stopLossShort := low[4] + (high[4] - low[4])*stopLossInput
    qtyShort = cashOrder/stopPriceShort
    strategy.entry("Short", strategy.short, qtyShort, stop=stopPriceShort)
    strategy.exit("Exit Short", "Short", limit=takeProfitShort ,stop=stopLossShort)


//--------------------------PLOTTING ELEMENT----------------------------//

plotshape(nr, "NR", shape.arrowdown, location.abovebar, color.rgb(255, 132, 0), text= "NR4", size=size.huge)
plot(stopPriceLong, "Stop Order", color.blue, 3, plot.style_linebr)
plot(stopPriceShort, "Stop Order", color.blue, 3, plot.style_linebr)
plot(takeProfit, "Take Profit", color.green, 3, plot.style_linebr)
plot(stopLoss, "Stop Loss", color.red, 3, plot.style_linebr)

もっと