ボリンジャー・バンドのトレンド 戦略をフォローする

作者: リン・ハーンチャオチャン,日付: 2024-01-15 14:31:21
タグ:

img

概要

この戦略は"ボリンジャーバンドストレンドフォロー戦略"と呼ばれる.ボリンジャーバンドス指標を使用して価格動向を決定し,価格がボリンジャーバンドスチャネルを突破するとロングまたはショートに入ります.ブレイク時にトレンド方向を判断するための移動平均フィルターを組み込み,その結果,ロングとショートエントリーを決定します.

原則

この戦略は,主にボリンジャー・バンド指標を基に価格傾向とエントリーポイントを決定する.ボリンジャー・バンドには3つの線が含まれます.

  1. 中間線:n日移動平均線
  2. 上行: n 日標準偏差向上
  3. 下の線: n 日標準偏差を下回る

価格が上線から下線に突破すると,上昇傾向が確認される.価格が上線から下線に突破すると,下線傾向が始まっている.この2種類の突破が発生すると,戦略はロングまたはショートに入ります.

具体的には 戦略の論理は

  1. バンドの下線から上向きに 接近が突破すると ロングに入ります
  2. バンドの上線から下へと近距離が切れる時 ショートに入ります

誤ったブレイクを避けるため,移動平均のフィルターを追加します. 接近が移動平均を壊すことと共にバンドを壊したときのみエントリが発生します.

ここでは指数的な移動平均が指標として使用されます.

概要すると,トレンドブレイクを決定する基準は以下の通りである.

  1. 長信号: 閉じる 波長 上線 && 閉じる 移動平均
  2. 短信号: 閉じる ダウンライン 帯 閉じる 移動平均

価格が再び中間線に触れたときに終了します.

強度分析

この戦略の主な強みには以下の点が含まれます.

  1. 中間線ブレイクによって形成された新しいトレンドをキャッチします. バンドチャネルは価格変動の余地を提供し,ブレイクシグナルは新しい方向の開始です.
  2. 移動平均フィルターを通して偽のブレイクを避け,実際のトレンド逆転時にのみエントリを保証します.
  3. 中間線を追跡することで リスクを効果的に制御します
  4. シンプルで明快な論理で 理解し実行しやすい アルゴ取引戦略に適しています
  5. バンドチャネルと移動平均指標を使用し,価格を予測する必要はありません. 事実の後に証拠に基づいて傾向を特定します.

リスク分析

この戦略は利点にもかかわらず,次のリスクも伴います.

  1. 不適切な帯域パラメータは,取引頻度とリスクを増加させる可能性があります.過度に敏感な設定は,過剰な誤ったブレイクと高いシステムターンオーバーを引き起こす可能性があります.
  2. 移動平均パラメータの選択が不十分である場合,実際のトレンドが欠落したり,誤った信号を生成したりする可能性があります. 繰り返しテストと最適化を通じてパラメータ調整が必要です.
  3. ストップ・ロスはミドルラインに依存し,早めに退場したり,リトラセイシングの余地が多すぎる可能性があります.これはほとんどの利益を失うことや損失リスクの増加につながる可能性があります.

上記のリスクを制御するために,次の最適化を行うことができます.

  1. バンドのパラメータを正しく調整して 誤った突破の可能性を減らすために チャンネル幅を増やしてください
  2. 適正な組み合わせを見つけるために 異なる移動平均の種類と長さをテストします
  3. ストップ・ロスの他の方法,例えば後続ストップ・ロスのレベルや 漸進的なストップ・ロスのレベルを試す.

オプティマイゼーションの方向性

リスク分析に基づいて,次の分野でのさらなる最適化を行うことができます.

  1. パラメータ最適化: バンドと移動平均値の最適なパラメータの組み合わせを見つけるために,遺伝アルゴリズムのようなより体系的な方法を使用し,戦略をより安定し,収益性のあるものにします.

  2. ストップ損失最適化: 最適なストップメカニズムを決定するために,ATRストップ,トレーリングストップなど,さまざまなストップ損失技術をテストします.

  3. フィルター最適化誤った信号の確率を低くし,収益率を上げるため,追加のフィルターとしてRSI,KDなどの他の指標を追加してみてください.

  4. 入場基準の最適化: トレンド条件,異常のボリュームなどの他の考慮事項を追加して,厳格にエントリータイミングを選択し,不必要なエントリを避ける.

  5. 機械学習: AIによる最高のエントリーと終了タイミングを可能にするため,LSTM,RNN,その他のディープラーニングモデルを構築するためにより多くの歴史的データを収集します.

  6. ダイナミックなリスク・報酬管理: 固定比率停止,特定の利益レベルに達した後,利益目標の急上昇を組み込む. リスクを動的に制御する.

上記の分野での最適化により 安定性,収益性,リスク調整能力などの重要な指標が全面的に改善され,戦略は生產レベルのアルゴリズムに変えられます.

結論

結論として,Bollinger Bands Trend Following Strategyは,Bands インジケーターと移動平均値を用い,主要なブレイクアウトポイントで価格トレンドを識別する. 明確な論理,シンプルさ,実装の容易さのメリットがあり,パラメータチューニング,ストップロスのメカニズムなどの改善分野もあります. パラメータ最適化,ストップロスの強化,機械学習統合などのさらなる改良により,堅牢で安定したアルゴ取引システムになります.


/*backtest
start: 2023-12-15 00:00:00
end: 2024-01-14 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//VERSION =================================================================================================================
//@version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// This strategy is intended to study.
// It can also be used to signal a bot to open a deal by providing the Bot ID, email token and trading pair in the strategy settings screen.
// As currently written, this strategy uses a Bollinger Bands for trend folling, you can use a EMA as a filter.
//Autor Credsonb (M4TR1X_BR)

//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//STRATEGY ================================================================================================================

strategy(title = 'BT-Bollinger Bands - Trend Following',
         shorttitle = 'BBTF',
         overlay = true )


//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// CONFIG =================================================================================================================

// TIME INPUTS
usefromDate = input.bool(defval = true, title = 'Start date', inline = '0', group = "Time Filters")
initialDate = input(defval = timestamp('01 Jan 2022 00:00 UTC'), title = '', inline = "0",group = 'Time Filters',tooltip="This start date is in the time zone of the exchange ")
usetoDate = input.bool(defval = true, title = 'End date', inline = '1', group = "Time Filters")
finalDate = input(defval = timestamp('31 Dec 2029 23:59 UTC'), title = '', inline = "1",group = 'Time Filters',tooltip="This end date is in the time zone of the exchange")

// TIME LOGIC 
inTradeWindow = true

// ENABLE LONG SHORT OPTIONS
string entrygroup ='Long/Short Options ==================================='
checkboxLong = input.bool(defval=true, title="Enable Long Entrys",group=entrygroup)
checkboxShort = input.bool(defval=true, title="Enable Short Entrys",group=entrygroup)


// BOLLINGER BANDS INPUTS ==================================================================================================
string bbgroup ='Bollinger Bands ======================================'
bbLength = input.int(defval=20,title='BB Length', minval=1, step=5, group=bbgroup)
bbStddev = input.float(defval=2, title='BB StdDev', minval=0.5, group=bbgroup)

//BOLLINGER BANDS LOGIC
[bbMiddle, bbUpper, bbLower] = ta.bb(close, bbLength, bbStddev)


// MOVING AVERAGES INPUTS ================================================================================================
string magroup =  'Moving Average ======================================='
useEma = input.bool(defval = true, title = 'Moving Average Filter',inline='', group= magroup,tooltip='This will enable or disable Exponential Moving Average Filter on Strategy')
emaType=input.string (defval='Ema',title='Type',options=['Ema','Sma'],inline='', group= magroup)
emaSource = input.source(defval=close,title="  Source",inline="", group= magroup)
emaLength = input.int(defval=100,title="Length",minval=0,inline='', group= magroup)

// MOVING AVERAGE LOGIC
float ema = emaType=='Ema'? ta.ema(emaSource,emaLength): ta.sma(emaSource,emaLength)

// BOT MESSAGES
string msgroup='Alert Message For Bot ================================'
messageEntry = input.string("", title="Strategy Entry Message",group=msgroup)
messageExit  =input.string("",title="Strategy Exit Message",group=msgroup)
messageClose = input.string("", title="Strategy Close Message",group=msgroup)




// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// POSITIONS =============================================================================================================

//VERIFY IF THE BUY FILTERS ARE ON OR OFF 
bool emaFilterBuy = useEma? (close > ema):(close >= ema) or (close <= ema)                      

//LONG / SHORT POSITIONS LOGIC
bool openLongPosition  = (close[1] < bbUpper) and (close > bbUpper)   and (emaFilterBuy)
bool openShortPosition = (close[1] > bbLower) and (close < bbLower) and (emaFilterBuy)
//bool closeLongPosition = (close > bbMiddle)
//bool closeShortPosition= (close < bbLower)


// CHEK OPEN POSITONS =====================================================================================================
// open signal when not already into a position
bool validOpenLongPosition = openLongPosition and strategy.opentrades.size(strategy.opentrades - 1) <= 0
bool longIsActive = validOpenLongPosition or strategy.opentrades.size(strategy.opentrades - 1) > 0

bool validOpenShortPosition = openShortPosition and strategy.opentrades.size(strategy.opentrades - 1) <= 0
bool shortIsActive = validOpenShortPosition or strategy.opentrades.size(strategy.opentrades - 1) < 0

longEntryPoint = high
if (openLongPosition) and (inTradeWindow) and (checkboxLong)
    strategy.entry(id = 'Long Entry', direction = strategy.long, stop = longEntryPoint, alert_message=messageEntry)

if not (openLongPosition)
    strategy.cancel('Long Entry')

//submit exit orders for trailing take profit price 
if (longIsActive) and (inTradeWindow)
    strategy.exit(id = 'Long Exit',  stop=bbMiddle, alert_message=messageExit)

//if (closeLongPosition)
   // strategy.close(id = 'Long Entry', alert_message=messageClose)
      

shortEntryPoint = low 
if (openShortPosition) and (inTradeWindow) and (checkboxShort)
    strategy.entry(id = 'Short Entry', direction = strategy.short, stop = shortEntryPoint, alert_message=messageEntry)

if not(openShortPosition)
    strategy.cancel('Short Entry')

if (shortIsActive)
    strategy.exit(id = 'Short Exit',  stop = bbMiddle, alert_message=messageExit)

//if (closeShortPosition)
    //strategy.close(id = 'Short Close', alert_message=messageClose)

// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// PLOTS ===============================================================================================================

// TRADE WINDOW ========================================================================================================
bgcolor(color = inTradeWindow ? color.new(#089981,90):na, title = 'Time Window')

// EMA/SMA 
var emafilterColor = color.new(color.white, 0)
plot(series=useEma? ema:na, title = 'EMA Filter', color = emafilterColor, linewidth = 2, style = plot.style_line)

// BOLLINGER BANDS
plot(series=bbUpper, title = "Upper Band", color = color.aqua)//, display = display.none)
plot(series=bbMiddle, title = "MA Band", color = color.red)//, display = display.none)
plot(series=bbLower, title = "Lower Band", color = color.aqua)//, display = display.none)

// PAINT BARS COLORS
bool bulls = (close[1] < bbUpper[1]) and (close > bbUpper)
bool bears = (close[1] > bbLower [1]) and (close < bbLower)
neutral_color = color.new(color.black, 100)
barcolors = bulls ? color.green : bears ? color.red : neutral_color
barcolor(barcolors)

// ======================================================================================================================


もっと