Trade with double bollinger band strategy

Avatar
  • updated

The Double Bollinger Band Strategy makes use of two Bollinger Bands in order to filter entries and exits in the forex market.

The strategy aims to enter Buy (Sell) trades when price crosses above (below) 3σ (2 standard deviation . The strategy also

considers the 2σ (2 standard deviations) standard deviation level of the Bollinger Bands for making trade decisions.

Please may reference the following web link for mql5.com to this related topic:

https://www.mql5.com/en/code/46630

Some work did to rewrite it in mql5 code as an indicator. Anyone familiar with this could help to improve and test it with custom Indy of CP?  Thank you.

//+------------------------------------------------------------------+
//| BB_Signal.mq5|
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2023, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
//--- input parameters
input int BB_KIKAN=20; // BB period

double waitBB = 0; // Initialize waitBB
datetime prevtime;

int orderPtn=0; // 0: Do nothing, 1: Buy, 2: Sell

// Create buffers to hold the upper and lower bands
double upperBuffer[];
double lowerBuffer[];

double BB3UP, BB3LO, BB2UP, BB2LO; // Define these variables here to make them global

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---

return(INIT_SUCCEEDED);
}

// Custom function to plot arrows
void PlotArrow(int direction, int shift, double price, color clr) {
string arrowName = direction > 0 ? "BuyArrow" : "SellArrow";
arrowName += "_" + IntegerToString(shift);
if (ObjectFind(0, arrowName) != -1) {
ObjectDelete(0, arrowName);
}
if (direction > 0)
ObjectCreate(0, arrowName, OBJ_ARROW_BUY, 0, iTime(_Symbol, _Period, shift), price);
else
ObjectCreate(0, arrowName, OBJ_ARROW_SELL, 0, iTime(_Symbol, _Period, shift), price);
ObjectSetInteger(0, arrowName, OBJPROP_COLOR, clr);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[])
{
// Get the handle of the Bollinger Bands indicator
int handle = iBands(_Symbol, _Period, BB_KIKAN, 3, 0, PRICE_CLOSE);

// Copy the upper band (index 0) and lower band (index 2) into the buffers
CopyBuffer(handle, 0, 0, rates_total, upperBuffer);
CopyBuffer(handle, 2, 0, rates_total, lowerBuffer);

// Now you can get the upper and lower bands using the buffers
BB3UP = upperBuffer[1]; // 3σ upper
BB3LO = lowerBuffer[1]; // 3σ lower

// Repeat the same for the 2σ Bollinger Bands
handle = iBands(_Symbol, _Period, BB_KIKAN, 2, 0, PRICE_CLOSE);
CopyBuffer(handle, 0, 0, rates_total, upperBuffer);
CopyBuffer(handle, 2, 0, rates_total, lowerBuffer);

BB2UP = upperBuffer[1]; // 2σ upper
BB2LO = lowerBuffer[1]; // 2σ lower

//*** Buy/Sell Conditions ***//
// Rest of your code...

// If in a Buy or Buy-wait state
if ((orderPtn == 1) || (waitBB > 0)) {
// Buy within BB2 range
if ((BB2UP > close[1]) && (BB2LO < close[1])) {
orderPtn = 1;
waitBB = 0;
PlotArrow(1, 1, close[1], clrGreen); // Plot Green Arrow for Buy Signal
} else {
orderPtn = 0;
waitBB = 1;
}
}

// If in a Sell or Sell-wait state
if ((orderPtn == 2) || (waitBB < 0)) {
// Sell within 2σ BB range
if ((BB2LO < close[1]) && (BB2UP > close[1])) {
orderPtn = 2;
waitBB = 0;
PlotArrow(-1, 1, close[1], clrRed); // Plot Red Arrow for Sell Signal
} else {
orderPtn = 0;
waitBB = -1;
}
}

return(rates_total);
}

Double Bolling Band.mq5

Double Bolling Band.ex5

MT version:
Both MT5 and MT4 set-files available
EA version:
2.55.4
Symbol:
any
TimeFrame:
any
Avatar
jay hu

I asked the original coder to help fix the code. https://www.mql5.com/en/code/46727

Please see attached updated indicator.

Double Bolling Band.ex5

Double Bolling Band.mq5

Avatar
Jan Corsair

You can use the CP modules "MA Filter" to setup a BB like this: 

Step 1

Image 5088

The Deviation is defined here:

Step 2

Image 5089

In MA Filter #2 you can setup other values for your 2nd BB. Deviation calculation can be definded only one time - this may or may not be a problem for your strategy.

Avatar
jay hu
Quote from Jan Corsair

You can use the CP modules "MA Filter" to setup a BB like this: 

Step 1

Image 5088

The Deviation is defined here:

Step 2

Image 5089

In MA Filter #2 you can setup other values for your 2nd BB. Deviation calculation can be definded only one time - this may or may not be a problem for your strategy.

Great to learn this. Thank you, Jan!