Apply Bollinger Bands to Previous data (MA)

brackhap
19

Hello


I'm trying to use Bollinger bands indicator with data from MA indicator. I read in the documentation  that I can use iBands to handle buffer of data from my MA indicator, but the example seems complex o me. can anyone please explain me how to do this?

Let's say I used this code to calculate the MA and copy it to a handler:

int MaFast;
MaFast = iMA(_Symbol,PERIOD_M5,10,0,MODE_EMA,PRICE_CLOSE);
double maFastHandler[];
CopyBuffer(MaFast,0,0,1,maFastHandler);

How can I use iBands with this maFastHandler?

Ernst Van Der Merwe
8211
Ernst Van Der Merwe  
//---
int handleMA=INVALID_HANDLE,handleBands=INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if((handleMA=iMA(_Symbol,_Period,MAPeriod,0,MAMethod,MAPrice))==INVALID_HANDLE ||
      (handleBands=iBands(_Symbol,_Period,BandsPeriod,0,BandsDeviation,handleMA))==INVALID_HANDLE)
      return(INIT_PARAMETERS_INCORRECT);
//---


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
   int toCopy=(rates_total!=prev_calculated)?rates_total-prev_calculated:1;
//---
   if(CopyBuffer(handleMA,0,0,toCopy,MABuffer)!=toCopy ||
      CopyBuffer(handleBands,1,0,toCopy,UpperBuffer)!=toCopy || 
      CopyBuffer(handleBands,0,0,toCopy,CentrBuffer)!=toCopy || 
      CopyBuffer(handleBands,2,0,toCopy,LowerBuffer)!=toCopy)
      return(0);
Files:
MA_Bands.mq5  4 kb
brackhap
19
brackhap  
Ernst Van Der Merwe #:
Thank you very much
brackhap
19
brackhap  
Ernst Van Der Merwe #:

Another question @Ernst Van Der Merwe , I'm trying to use calculated Bollinger bands based on MA in an expert adviser, once I use your code, the metatrader detect my Expert as an indicator (I know now that it's because of the onCalculate function). Is there any way to use this code inside an Expert without without altering its functioning?

Best regards

Ernst Van Der Merwe
8211
Ernst Van Der Merwe  
brackhap #:

Another question @Ernst Van Der Merwe , I'm trying to use calculated Bollinger bands based on MA in an expert adviser, once I use your code, the metatrader detect my Expert as an indicator (I know now that it's because of the onCalculate function). Is there any way to use this code inside an Expert without without altering its functioning?

Best regards

Yes, works exactly the same way.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int toCopy=2;
   if(CopyBuffer(handleMA,0,0,toCopy,MABuffer)!=toCopy ||
      CopyBuffer(handleBands,1,0,toCopy,UpperBuffer)!=toCopy || 
      CopyBuffer(handleBands,0,0,toCopy,CentrBuffer)!=toCopy || 
      CopyBuffer(handleBands,2,0,toCopy,LowerBuffer)!=toCopy)
      return;
//---
   if(MABuffer[1]<UpperBuffer[1] && MABuffer[0]>UpperBuffer[0])
     {
      Print("Buy!");
     }
  }
//+------------------------------------------------------------------+
Files:
MA_Bands.mq5  3 kb
brackhap
19
brackhap  
Ernst Van Der Merwe #:

Yes, works exactly the same way.

Thank you very much @Ernst Van Der Merwe , I'm really grateful