Download MetaTrader 5

Bollinger band on spread

To add comments, please log in or register
Hyun Hwang
327
Hyun Hwang  

Hi guys,

I am a beginner at coding and I tried to code and apply bollinger band on closing prices of a spread ( (EURGBP-EURUSD)-(EURUSD-GBPUSD)) but the band always seem to be containing the closing price i.e the price never cross either upper or lower band

so I thought I'd ask someone to check out if the code is written properly 

as you can see red line (close prices of the spread) never goes beyond or below dotted blue line.


Below is the code 

//+------------------------------------------------------------------+
//|                                                    Fly Price.mq4 |
//|                                                   Hyun soo Hwang |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Hyun soo Hwang"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_level1     0
#property indicator_buffers 5
#property indicator_color1     Red
#property indicator_color2     LightSeaGreen
#property indicator_color3     LightBlue
#property indicator_color4     Blue
#property indicator_color5     Blue

//--- buffers
double ExtFlyBuffer[];
double Ext10MaBuffer[];
double Ext20MaBuffer[];
double ExtUpperBandBuffer[];
double ExtLowerBandBuffer[];

double ExtStdevBuffer[];

//--- variables
int limit, i, x, y;
double total;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(6);
   
   SetIndexBuffer(0,ExtFlyBuffer);
   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID, 2);
   SetIndexLabel(0,"Fly Close");
   
   SetIndexBuffer(1,Ext10MaBuffer);
   SetIndexStyle(1,DRAW_LINE, STYLE_SOLID, 1);
   SetIndexLabel(1,"10 MA");
   
   SetIndexBuffer(2,Ext20MaBuffer);
   SetIndexStyle(2,DRAW_LINE, STYLE_SOLID, 1);
   SetIndexLabel(2,"20 MA");
   
   SetIndexBuffer(3,ExtUpperBandBuffer);
   SetIndexStyle(3,DRAW_LINE, STYLE_DOT, 1);
   SetIndexLabel(3,"Upper Band");
   
   SetIndexBuffer(4,ExtLowerBandBuffer);
   SetIndexStyle(4,DRAW_LINE, STYLE_DOT, 1);
   SetIndexLabel(4,"Lower Band");
   
   
   SetIndexBuffer(5, ExtStdevBuffer);
   
//---
   return(INIT_SUCCEEDED);
  }
 
//+------------------------------------------------------------------+
//| 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[])
  {
 
//--- Find number of bars to fill (aka find Limit)
   //int counted_bars=IndicatorCounted(); // IndicatorCounted returns number of bars not changed since the indicator had been launched last
   if(rates_total<20 || prev_calculated<0) return(0); // To check for errors
   //if(counted_bars>0) counted_bars--; // Minus 1 so as to always recount the last bar. Last bar may be formed incompletely

   limit=(prev_calculated==0)?rates_total-1:rates_total-prev_calculated; // Bars represent number of bars in the chart


//--- Main Calculation 
   for(i=limit; i>=0 && !_StopFlag; i--) {
      ///--- Fly price
      ExtFlyBuffer[i] = flyPrice(i);
      
      ///--- Moving averages
      Ext10MaBuffer[i] = MA(i, 10);
      Ext20MaBuffer[i] = MA(i, 20);
      
      //--- Standard Deviation    
      ExtStdevBuffer[i] = Stdev(i,ExtFlyBuffer,Ext20MaBuffer[i],20);
      ExtUpperBandBuffer[i] = Ext20MaBuffer[i] + 2 * ExtStdevBuffer[i];
      ExtLowerBandBuffer[i] = Ext20MaBuffer[i] - 2 * ExtStdevBuffer[i];
      
    }
      
   return(rates_total);
//--- return value of prev_calculated for next call  
  }
  
//+------------------------------------------------------------------+

//--- Functions

double flyPrice(int k) {
   
   double flyPrice = (iClose("EURGBP", 0, k)-iClose("EURUSD", 0, k))-(iClose("EURUSD", 0, k)-iClose("GBPUSD", 0, k));
   
   return(flyPrice);


}

double MA(int m, int period1) {
   double sum1 = 0;
   double ma1;
   for(x=m; x<m+period1; x++) {
            sum1 = sum1 + flyPrice(x);
         }

   ma1 = sum1/period1; // Sum of prices divided number of bars gives us the SMA on closing price 
   return(ma1);        
   
}

double Stdev(int pos, double &price[], double MA, int period) {

   double StdevTemp =0.0;
   
   for (y=pos; y<pos+period; y++) {
      StdevTemp +=MathPow(price[pos]-MA,2);    
   }
   StdevTemp = MathSqrt(StdevTemp/period);
   
   return(StdevTemp);
}



Much appreciated in advance.


Thank you :)

Mladen Rakic
59915
Mladen Rakic  
Hyun Hwang:

Hi guys,

I am a beginner at coding and I tried to code and apply bollinger band on closing prices of a spread ( (EURGBP-EURUSD)-(EURUSD-GBPUSD)) but the band always seem to be containing the closing price i.e the price never cross either upper or lower band

so I thought I'd ask someone to check out if the code is written properly 

as you can see red line (close prices of the spread) never goes beyond or below dotted blue line.


Below is the code 



Much appreciated in advance.


Thank you :)

You shall have to use deviations multiplier less than 2 to get crosses (in fact less than 1 the way you calculate it)

See the example


Files:
Fly_price.mq4 10 kb
Hyun Hwang
327
Hyun Hwang  
Mladen Rakic:

You shall have to use deviations multiplier less than 2 to get crosses (in fact less than 1 the way you calculate it)

See the example



thank you so much for that.


When you say the way I calculated it, is there anything wrong with my calculation? is there a better way to do it?


much appreciated

Mladen Rakic
59915
Mladen Rakic  
Hyun Hwang:


thank you so much for that.


When you say the way I calculated it, is there anything wrong with my calculation? is there a better way to do it?


much appreciated

The way you calculate the bands - and that is why you have to have deviations multiplier less than 1 :)

Hyun Hwang
327
Hyun Hwang  
Mladen Rakic:

The way you calculate the bands - and that is why you have to have deviations multiplier less than 1 :)


ah ok, I can see that the prices now do cross when I use deviation multiplier less than 1 thank you for that. 

To add comments, please log in or register