Not able to update buffer values when creating an indicator in a loop mql5

 

Hi all,

I'm hoping this is a simple question. I am writing an EA that gets indicator values from multiple currencies. The problem I face is that the handle is created, the values copied to a buffer correctly for the first market in the loop but when it moves onto the next market, the handle is not updated to reflect the new parameters and so the value I get out of the buffers is wrong. It is always the value from the first market in the loop. 

The code is below, please could someone advise where I am going wrong? Ideally, I want to be able to backtest this so whatever the solution, it needs to work in the Strategy Tester


Thanks!

   string      CPs[] =  {  "EURUSD","EURGBP" };

   for(i = 0;i<ArraySize(CPs);i++)
   {
      currency1 = StringSubstr(CPs[i],0,3);
      currency2 = StringSubstr(CPs[i],3,3);
      
      
      if(StringFind(CPs[i],currency_base)>=0 || StringFind(CPs[i],currency_quote)>=0)
      {
        
      //--- create MA5 indicator and add it to collection
         if(m_handle_ma5==INVALID_HANDLE)
            if((m_handle_ma5=iMA(CPs[i],analysis_period,5,0,MODE_EMA,PRICE_CLOSE))==INVALID_HANDLE)
              {
               printf("Error creating MA5 indicator");
               return(-99);
              }
              
         if(m_handle_ma12==INVALID_HANDLE)
            if((m_handle_ma12=iMA(CPs[i],analysis_period,12,0,MODE_EMA,PRICE_CLOSE))==INVALID_HANDLE)
              {
               printf("Error creating MA12 indicator");
               return(-99);
              }

         //--- create ROC indicator and add it to collection
         if(m_handle_roc1==INVALID_HANDLE)
            if((m_handle_roc1=iCustom(CPs[i],analysis_period,"ROC",3,0))==INVALID_HANDLE)
              {
               printf("Error creating ROC1 indicator");
               return(-99);
              }

         //--- create ROC indicator and add it to collection
         if(m_handle_roc2==INVALID_HANDLE)
            if((m_handle_roc2=iCustom(CPs[i],analysis_period,"ROC",12,0))==INVALID_HANDLE)
              {
               printf("Error creating ROC2 indicator");
               return(-99);
              }
         
         if(CopyBuffer(m_handle_ma5,0,0,3,m_buff_MA5)    <= 0) {Print("CS3 Buffer 1 failed");return(-99);}
         if(CopyBuffer(m_handle_ma12,0,0,3,m_buff_MA12)  <= 0 ) {Print("CS3 Buffer 2 failed");return(-99);}
         if(CopyBuffer(m_handle_roc1,0,0,3,m_buff_ROC1)  <= 0) {Print("CS3 Buffer 3 failed");return(-99);}
         if(CopyBuffer(m_handle_roc2,0,0,3,m_buff_ROC2)  <= 0) {Print("CS3 Buffer 4 failed");return(-99);}
         
         //   m_indicators.Refresh();
         //--- to simplify the coding and speed up access
         //--- data are put into internal variables
             
         ma51     = m_buff_MA5[1];
         ma52     = m_buff_MA5[2];
         ma121    = m_buff_MA12[1];
         ma122    = m_buff_MA12[2];
         roc11    = m_buff_ROC1[1];
         roc12    = m_buff_ROC1[2];
         roc21    = m_buff_ROC2[1];
         roc22    = m_buff_ROC2[2];
         

         //PERFORM CALCULATIONS
      }
   }
 
wildkev146:

Hi all,

I'm hoping this is a simple question. I am writing an EA that gets indicator values from multiple currencies. The problem I face is that the handle is created, the values copied to a buffer correctly for the first market in the loop but when it moves onto the next market, the handle is not updated to reflect the new parameters and so the value I get out of the buffers is wrong. It is always the value from the first market in the loop. 

The code is below, please could someone advise where I am going wrong? Ideally, I want to be able to backtest this so whatever the solution, it needs to work in the Strategy Tester

Thanks!

What values do you initialize your handles with? Are they all INVALID_HANDLE (i.e. -1)?

Assuming they are, so when i==0, the handles are assigned a valid value based on your calls to iMA() and iCustom(), for CPs[0]. Then comes i==1... this time round all handles are no longer INVALID_HANDLE, so they won't get reassigned with iMA() and iCustom() calls for CPs[1].

Also, you should move all handle creation to OnInit(), so use m_handle_ma5[2] instead, for example.

 
Seng Joo Thio:

What values do you initialize your handles with? Are they all INVALID_HANDLE (i.e. -1)?

Assuming they are, so when i==0, the handles are assigned a valid value based on your calls to iMA() and iCustom(), for CPs[0]. Then comes i==1... this time round all handles are no longer INVALID_HANDLE, so they won't get reassigned with iMA() and iCustom() calls for CPs[1].

Also, you should move all handle creation to OnInit(), so use m_handle_ma5[2] instead, for example.

Hi Seng,


Thanks for the reply. Yes, they are all initialised with INVALID_HANDLE. The reason I have not done them on OnInit() is because this piece of code is for determining currency strength and I didn't want to have to create 28 different handles, one for each market. As I looped through the list of markets to check, I wanted to create and update the handle during the loop. However, the comment you made about initislising with INVALID_HANDLE made me realise an obvious mistake. So, I have solved my issue and and to do so, I set the handles back to -1 at the end of each loop. This ensures that the handle is recreated and the correct indicator values are stored.

Thank you so much for your help!

Reason: