Indicators within a loop

 
Hi, 

Can anyone help with calculating indicators within a for loop. I am trying to call out the moving average and atr indicators within a loop of bars in a chat.

I already calculated the indicators handles and set the array for each indicator as a series in the "Oninit" function.

It keeps saying "Array out of range" when I try to call out the ma in the loop below;


   int bars = iBars(_Symbol,PERIOD_CURRENT);
   for(int i=0; i<=bars; i++){
         if(mabuffer[i] + atrbuffer[i] > ...
      
 
Mr Victor Idamuze Oyinbo:
Hi, 

Can anyone help with calculating indicators within a for loop. I am trying to call out the moving average and atr indicators within a loop of bars in a chat.

I already calculated the indicators handles and set the array for each indicator as a series in the "Oninit" function.

It keeps saying "Array out of range" when I try to call out the ma in the loop below;


   int bars = iBars(_Symbol,PERIOD_CURRENT);
   for(int i=0; i<=bars; i++){
         if(mabuffer[i] + atrbuffer[i] > ...
      
It can have a lot of reasons at this point, either the buffers are not correctly declared or the loop parameters are wrong. But this little code snippet does not give those informations.
 
Tobias Johannes Zimmer #:
It can have a lot of reasons at this point, either the buffers are not correctly declared or the loop parameters are wrong. But this little code snippet does not give those informations.

See full code below;

Thanks

//--- ATR parameters
input group "=== ATR parmeters ==="
input int      atr_Period           = 14;             //ATR period
input double   atr_Range            = 2;              //ATR range
input double   atr_Mult             = 2;              //ATR multiplier

//+------------------------------------------------------------------+
//| Handles and global variables
//+------------------------------------------------------------------+
//indicator handles
int maHandle;    // handle for our Moving Average indicator
int atrHandle;       // handle for ATR

//--- set our arrays
double ma[];      // Dynamic array to hold the values of fast moving average for each bars
double atr[];         // Dynamic array to hold the values of Average true range (ATR) for each bars
MqlRates priceInfo[];

//Other variables
double topLimit, bottomLimit;
static bool isRangeActive = false;

//include variables
CTrade trade;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
   
   // defne indicator numbers
   maHandle = iMA(_Symbol,PERIOD_CURRENT,ma_Period,0,ma_Method,ma_Price);
   atrHandle = iATR(_Symbol,PERIOD_CURRENT,atr_Period);

   // sort the indicators from the current value downwards
   ArraySetAsSeries(priceInfo, true);
   ArraySetAsSeries(ma, false);
   ArraySetAsSeries(atr, true);


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){

   if(maHandle != INVALID_HANDLE){
      IndicatorRelease(maHandle);
     }
   if(atrHandle != INVALID_HANDLE){
      IndicatorRelease(atrHandle);
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){

   //Fill the price info array with price data
   int priceData = CopyRates(_Symbol,PERIOD_CURRENT,0,3,priceInfo);
      
   //Get the bid and ask price
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
 
   //identify range
   int bars = iBars(_Symbol,PERIOD_CURRENT);
   for(int i=0; i<=bars; i++){
      int priceData = CopyRates(_Symbol,PERIOD_CURRENT,i,bars,priceInfo);
      CopyBuffer(maHandle,0,i,bars,ma);
      CopyBuffer(atrHandle,0,i,bars,atr);
      
      double candleOpen = iOpen(_Symbol,_Period,i);
      double candleClose = iClose(_Symbol,_Period,i);
      double atrValue = atr[i] * atr_Mult;
       
      if(MathAbs(candleClose - ma[i]) <= atrValue) && !isRangeActive){
         isRangeActive = true;
         datetime time1 = iTime(_Symbol,_Period,0);
         datetime time2 = time1 + PeriodSeconds(_Period) * 20;
         topLimit = ma[i] + atr[i];
         bottomLimit = ma[i] - atr[i];
         Comment("Range is active");
          
         } else if(isRangeActive){
                   if(candleClose > topLimit || candleClose < bottomLimit){
                      isRangeActive = false;
                      Comment("Range is inactive");
                      }
                   }
      }
   
  }
 
Mr Victor Idamuze Oyinbo #:

See full code below;

Thanks

You are processing all history bars on every tick.

Don't do that. Redesign your logic.

Don't CopyBuffer on every iteration of the loop.

Don't assume CopyBuffer was successful, check your return values, and check _LastError.
 
Dominik Egert #:
You are processing all history bars on every tick.

Don't do that. Redesign your logic.

Don't CopyBuffer on every iteration of the loop.

Don't assume CopyBuffer was successful, check your return values, and check _LastError.
How would you suggest I approach this? I want to identify a condition, and then confirm whether the condition holds true for each bar
 
Mr Victor Idamuze Oyinbo #:
How would you suggest I approach this? I want to identify a condition, and then confirm whether the condition holds true for each bar
I would suggest you read the book that is available on this webpage.

You are currently at rookie stage, there is way to much to lecture for a post...
 
Mr Victor Idamuze Oyinbo:
Hi, 

Can anyone help with calculating indicators within a for loop. I am trying to call out the moving average and atr indicators within a loop of bars in a chat.

I already calculated the indicators handles and set the array for each indicator as a series in the "Oninit" function.

It keeps saying "Array out of range" when I try to call out the ma in the loop below;


   int bars = iBars(_Symbol,PERIOD_CURRENT);
   for(int i=0; i<=bars; i++){
         if(mabuffer[i] + atrbuffer[i] > ...
      
https://www.mql5.com/en/code/27733
Reason: