Download MetaTrader 5

copybuffer for bollinger bands

To add comments, please log in or register
n22alpha
63
n22alpha  

Hi guys,

 I'm new to MQL5 and programming as a whole and want to know how do I go about using copybuffer with bollinger bands???

 Do I need to define BBUP,BBDOWN and BBMIDDLE and set one copybuffer for each???

 Here is my code btw.

 I'm trying to build a multitimeframe multicurrency indicator which attaches to one chart.

 

//+------------------------------------------------------------------+
//|                                                bollingertest.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

input int BandsPeriod = 20;
input int BandsDeviation = 2;
input int BandsShift = 0;
input string StrategySymbol = "GBPUSD";
input ENUM_TIMEFRAMES Time;
input ENUM_APPLIED_PRICE PriceType;

int boll_handle;
double boll_buf[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   boll_handle = iBands(StrategySymbol,Time,BandsPeriod,BandsShift,BandsDeviation,PriceType);
   
   if (boll_handle = INVALID_HANDLE)
      {
         Print("Failed to get handle");
         Return (-1);
      };
    
   ChartIndicatorAdd(ChartID(),0,boll_handle);
   ArraySetAsSeries(boll_buf, true);
   
   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[])
  {
//---
   int copied = CopyBuffer(boll_handle,0,0
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
32554
Alain Verleyen  

See if this topic helps : https://www.mql5.com/en/forum/11488

n22alpha
63
n22alpha  

ok here is the code I have now but I am getting error: "failed to get handle" i'm not sure why could you check it?

 

I can't post code for some reason basically its the same code as above with buffers and arraysetasseries in the on calculate but the handle code is the same I'm not sure why it isn't working. 

Alain Verleyen
Moderator
32554
Alain Verleyen  
n22alpha:

ok here is the code I have now but I am getting error: "failed to get handle" i'm not sure why could you check it?

 

I can't post code for some reason basically its the same code as above with buffers and arraysetasseries in the on calculate but the handle code is the same I'm not sure why it isn't working. 

Waiting the code.
n22alpha
63
n22alpha  
//+------------------------------------------------------------------+
//|                                                bollingertest.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

input int BandsPeriod = 20;
input int BandsDeviation = 2;
input int BandsShift = 0;
input string StrategySymbol = "GBPUSD";
input ENUM_TIMEFRAMES Time;


int boll_handle;
double upper_buf[];
double middle_buf[];
double lower_buf[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   boll_handle = iBands(StrategySymbol,Time,BandsPeriod,BandsShift,BandsDeviation,PRICE_CLOSE);
   
   if (boll_handle = INVALID_HANDLE)
      {
         Print("Failed to get handle");
         return (-1);
      };
    
   ChartIndicatorAdd(ChartID(),0,boll_handle);
 
   
   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[])
  {
   ArraySetAsSeries(upper_buf, true);
   ArraySetAsSeries(middle_buf, true);
   ArraySetAsSeries(lower_buf, true);
   if (CopyBuffer(boll_handle,0,0,100,upper_buf) <=0)
      {
         Alert("Error in buffer upper",GetLastError());
         return(-1);
      }
      
     if (CopyBuffer(boll_handle,0,1,100,middle_buf) <=0)
      {
         Alert("Error in buffer upper",GetLastError());
         return(-1);
      }
      
      if (CopyBuffer(boll_handle,0,2,100,lower_buf) <=0)
      {
         Alert("Error in buffer upper",GetLastError());
         return(-1);
      }
      
      
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
32554
Alain Verleyen  
n22alpha:

Please pay attention to warning when you compile :

expression not boolean    36539.mq5    29    20

Line 29. It should be :

   if (boll_handle == INVALID_HANDLE)
To add comments, please log in or register