ブレイクボリンガーバンド取引戦略

作者: リン・ハーンチャオチャン, 日付: 2023-10-07 09:59:11
タグ:

概要

この戦略は,ボリンジャーバンドのブレイクアウトを取引し,価格が上または下帯を完全に突破したとき,反トレンドポジションをとる.異常波動後の平均逆転を捕捉することを目的としています.迅速な利益を求めるアクティブトレーダーに適しています.

原則

この戦略は,現在の波動範囲を定義するためにボリンジャー帯を使用する.価格が上部帯以上または下部帯以下に完全なキャンドルスタイクを形成すると,非常に波動状態と平均値への潜在的な逆転を示します.

中間,上位,下位帯は20期間の閉盤価格を使用して計算される. 価格が上位開盤後下位帯を下回るとロング信号が生成される. 価格が上位帯を下回って開盤後上位帯を下回るとショート信号がトリガーされる. ブレイクアウトポイントはストップ損失として機能し,中間帯は初期利益目標である.

利点

主な利点は以下の通りです.

  1. ボリンジャー・バンドは 市場変動を効果的に測定し 異常を特定します

  2. ブレイクポイントはリスクをコントロールするための 明確なストップ・ロストレベルです

  3. 中央帯は平均逆転の 合理的な標的を提示します

  4. 完全なキャンドルスタイキは 信号の信頼性により 誤差をフィルターします

  5. シンプルなパラメータにより 実装と最適化が容易になります

  6. コードで簡潔に表現された 純粋な論理です

リスク

いくつかのリスクには,以下が含まれます.

  1. BBのパラメータが悪ければ 戦略が無効になる

  2. 突破はトレンド開始の兆候であり 早期離脱の危険性がある

  3. 中間帯のターゲットが 保守的で 利得を制限するかもしれません

  4. 広い断片は完全に満たされず 滑りやすいかもしれません

  5. ワイプソウは 市場範囲で 過剰な無意味な取引を 誘発する可能性があります

改良

改善のいくつかの考慮事項:

  1. 設定や頻度を調整するためにトレンド強さを測定する.

  2. 他の指標を追加して 入力のタイミングを微調整します

  3. ストップ・ロスを変動に基づいて調整する.

  4. 初期目標を最適化して 安定した利益を得ます

  5. 複合利益への再入力のメカニズムを実装する

  6. 失敗した取引を避けるために 脱出の有効性を評価する.

結論

この戦略は,アクティブトレーダーに適した短期利益のためにBBブレイアウトを取引する.プロは明確なリスク制御であり,デメリットは早期出口と利益の上限である.微調整パラメータ,フィルターなどを追加することでパフォーマンスを改善することができます.


/*backtest
start: 2023-09-06 00:00:00
end: 2023-10-06 00:00:00
period: 3h
basePeriod: 15m
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/
// © Bishnu103

//@version=4
strategy(title="Full Candle Outside BB [v1.0][Bishnu103]",shorttitle="OUTSIDE BB",overlay=true,calc_on_every_tick=true,backtest_fill_limits_assumption=2)

// ***********************************************************************************************************************
// input variables
buy_session         = input(title="Buy Session", type=input.session, defval="0915-1430")
exit_inraday        = input(title="Exit Intraday?", type=input.bool, defval=true)
entry_distance      = input(title="Entry distance from alert", minval=1, maxval=10, defval=3)
show_bb_switch      = input(title="Show BB", type=input.bool, defval=true)
//
bbLength            = input(title="BB Length", minval=1, defval=20)
bbStdDev            = input(title="BB StdDev", minval=1, defval=2)

// ***********************************************************************************************************************
// global variables
long_entry          = false
short_entry         = false
long_exit           = false
short_exit          = false

// variable values available across candles
var entry_price     = 0.0  
var sl_price        = 0.0
var exit_price      = 0.0
var candle_count    = 0

// ***********************************************************************************************************************
// function to return bollinger band values based on candle poition passed 
getBB(pos) => [mBB, uBB, lBB] = bb(close[pos], bbLength, bbStdDev)

// function returns true if current time is within intraday byuing session set in input
BarInSession(sess) => time(timeframe.period, sess) != 0

// ***********************************************************************************************************************
// strategy
//
// get current bb value
[mBB_0,uBB_0,lBB_0] = getBB(0)

// check if full candle outside upper BB
outside_uBB = low > uBB_0 and close <= open
outside_lBB = high < lBB_0 and close >= open

// ***********************************************************************************************************************
// entry conditions 
long_entry   := outside_lBB
short_entry  := outside_uBB

// keep candle count since the alert generated so that order can be cancelled after N number of candle calling it out as invalid alert
candle_count := candle_count + 1
if long_entry or short_entry
    candle_count    := 0

// ***********************************************************************************************************************
// risk management
//
// decide entry and sl price
if long_entry
    entry_price := high
    
if short_entry
    entry_price := low
    
if long_entry
    sl_price    := low
    
if short_entry
    sl_price    := high

// first exit is when price hits middle BB, gets updated for each candle based on it's middle BB value
exit_price  := mBB_0

// ***********************************************************************************************************************
// position sizing
price = if close[0] > 25000
    25000
else
    price = close[0]

qty = 25000/price

// ***********************************************************************************************************************
// entry
//if long_entry and strategy.position_size == 0
//    strategy.entry("BUY", strategy.long, qty, stop=entry_price, comment="BUY @ "+ tostring(entry_price)) 
if long_entry and strategy.position_size == 0
    strategy.order("BUY", strategy.long, qty, stop=entry_price, comment="BUY @ "+ tostring(entry_price))

//if short_entry and strategy.position_size == 0
//    strategy.entry("SELL", strategy.short, qty, stop=entry_price, comment="SELL @ "+ tostring(entry_price))
if short_entry and strategy.position_size == 0
    strategy.order("SELL", strategy.short, qty, stop=entry_price, comment="SELL @ "+ tostring(entry_price))

// cancel an order if N number of candles are completed after alert candle
strategy.cancel_all(candle_count > entry_distance)

// if current time is outside byuing session then do not enter intraday trade
strategy.cancel_all(timeframe.isintraday and not BarInSession(buy_session))

// ***********************************************************************************************************************
// exit
if strategy.position_size > 0
    strategy.cancel("EXIT at MBB", true)
    strategy.cancel("EXIT at SL", true)
    strategy.order("EXIT at MBB", strategy.short, abs(strategy.position_size), limit=exit_price, comment="EXIT TG @ "+ tostring(exit_price))
    strategy.order("EXIT at SL", strategy.short, abs(strategy.position_size), stop=sl_price, comment="EXIT SL @ "+ tostring(sl_price))

if strategy.position_size < 0
    strategy.cancel("EXIT at MBB", true)
    strategy.cancel("EXIT at SL", true)
    strategy.order("EXIT at MBB", strategy.long, abs(strategy.position_size), limit=exit_price, comment="EXIT TG @ "+ tostring(exit_price))
    strategy.order("EXIT at SL", strategy.long, abs(strategy.position_size), stop=sl_price, comment="EXIT SL @ "+ tostring(sl_price))

// if intraday trade, close the trade at open of 15:15 candle //!!!!!!!!!!!!!!!!!!!!! TO BE CORRECTED !!!!!!!!!!!!!!!!!!!!!!!
if timeframe.isintraday and exit_inraday and hour == 15 and minute == 00
    strategy.close("BUY",  when=strategy.position_size > 0, qty=strategy.position_size, comment="EXIT @ "+ tostring(close))
    strategy.close("SELL", when=strategy.position_size < 0, qty=strategy.position_size, comment="EXIT @ "+ tostring(close))

// ***********************************************************************************************************************
// plots    
//
// plot BB
[mBBp,uBBp,lBBp] = getBB(0)
p_mBB = plot(show_bb_switch ? mBBp : na, color=color.teal)
p_uBB = plot(show_bb_switch ? uBBp : na, color=color.teal)
p_lBB = plot(show_bb_switch ? lBBp : na, color=color.teal)
fill(p_uBB,p_lBB,color=color.teal,transp=95)

もっと