OPEN-SOURCE SCRIPT

Bollinger Bands Strategy

Bollinger Bands Strategy :


INTRODUCTION :

This strategy is based on the famous Bollinger Bands. These are constructed using a standard moving average (SMA) and the standard deviation of past prices. The theory goes that 90% of the time, the price is contained between these two bands. If it were to break out, this would mean either a reversal or a continuation. However, when a reversal occurs, the movement is weak, whereas when a continuation occurs, the movement is substantial and profits can be interesting. We're going to use BB to take advantage of this strong upcoming movement, while managing our risks reasonably. There's also a money management method for reinvesting part of the profits or reducing the size of orders in the event of substantial losses.


BOLLINGER BANDS :

The construction of Bollinger bands is straightforward. First, plot the SMA of the price, with a length specified by the user. Then calculate the standard deviation to measure price dispersion in relation to the mean, using this formula :

stdv = (((P1 - avg)^2 + (P2 - avg)^2 + ... + (Pn - avg)^2) / n)^1/2

To plot the two Bollinger bands, we then add a user-defined number of standard deviations to the initial SMA. The default is to add 2. The result is :
  • Upper_band = SMA + 2*stdv
  • Lower_band = SMA - 2*stdv

When the price leaves this channel defined by the bands, we obtain buy and sell signals.


PARAMETERS :

  • BB Length : This is the length of the Bollinger Bands, i.e. the length of the SMA used to plot the bands, and the length of the price series used to calculate the standard deviation. The default is 120.
  • Standard Deviation Multipler : adds or subtracts this number of times the standard deviation from the initial SMA. Default is 2.
  • SMA Exit Signal Length : Exit signals for winning and losing trades are triggered by another SMA. This parameter defines the length of this SMA. The default is 110.
  • Max Risk per trade (in %) : It's the maximum percentage the user can lose in one trade. The default is 6%.
  • Fixed Ratio : This is the amount of gain or loss at which the order quantity is changed. The default is 400, meaning that for each $400 gain or loss, the order size is increased or decreased by a user-selected amount.
  • Increasing Order Amount : This is the amount to be added to or subtracted from orders when the fixed ratio is reached. The default is $200, which means that for every $400 gain, $200 is reinvested in the strategy. On the other hand, for every $400 loss, the order size is reduced by $200.
  • Initial capital : $1000
  • Fees : Interactive Broker fees apply to this strategy. They are set at 0.18% of the trade value.
  • Slippage : 3 ticks or $0.03 per trade. Corresponds to the latency time between the moment the signal is received and the moment the order is executed by the broker.
  • Important : A bot has been used to test the different parameters and determine which ones maximize return while limiting drawdown. This strategy is the most optimal on BTCUSD in 8h timeframe with the following parameters :

BB Length = 120
Standard Deviation Multipler = 2
SMA Exit Signal Length = 110
Max Risk per trade (in %) = 6%


ENTER RULES :

The entry rules are simple:
  • If close > Upper_band it's a LONG signal
  • If close < Lower_band it's a SHORT signal


EXIT RULES :

  • If we are LONG and close < SMA_EXIT, position is closed
  • If we are SHORT and close > SMA_EXIT, the position is closed
  • Positions close automatically if they lose more than 6% to limit risk


RISK MANAGEMENT :

This strategy is subject to losses. We manage our risk using the exit SMA or using a SL sets to 6%. This SMA gives us exit signals when the price closes below or above, thus limiting losses. If the signal arrives too late, the position is closed after a loss of 6%.


MONEY MANAGEMENT :

The fixed ratio method was used to manage our gains and losses. For each gain of an amount equal to the fixed ratio value, we increase the order size by a value defined by the user in the "Increasing order amount" parameter. Similarly, each time we lose an amount equal to the value of the fixed ratio, we decrease the order size by the same user-defined value. This strategy increases both performance and drawdown.


NOTE :

Please note that the strategy is backtested from 2017-01-01. As the timeframe is 8h, this strategy is a medium/long-term strategy. That's why only 51 trades were closed. Be careful, as the test sample is small and performance may not necessarily reflect what may happen in the future.


Enjoy the strategy and don't forget to take the trade :)
Bands and ChannelsBollinger Bands (BB)Standard Deviation

Skrip sumber terbuka

Dalam semangat sebenar TradingView, penulis telah menerbitkan kod Pine ini sebagai sumber terbuka supaya pedagang dapat memahami dan mengesahkannya. Sorakan kepada penulis! Anda boleh menggunakan perpustakaan ini secara percuma, tetapi penggunaan semula kod dalam penerbitan ini adalah dikawal oleh Peraturan dalaman. Anda boleh menyukainya untuk menggunakannya pada carta.

Ingin menggunakan skrip ini pada carta?

Penafian