mql5 different behavior of iMA() function in script and in custom indicator

 

hi

this code is mql5 script and print the ma value of D1 period and work as it expected:

void OnStart()
{
   int mahandle;
   mahandle=iMA(NULL,PERIOD_D1,1,0,MODE_SMA,PRICE_MEDIAN);
   double mavalue[1];
   CopyBuffer(mahandle,0,0,1,mavalue);
   Print(mavalue[0]);  
}

but same code in custom indicator does not work correctly and seems copy buffer assigns no value to mavalue variable and the return value of copybuffer is -1):

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite


int mahandle;
double ma[];
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0,ma,INDICATOR_DATA);
   mahandle=iMA(NULL,PERIOD_D1,1,0,MODE_SMA,PRICE_MEDIAN);
   double mavalue[1];
   CopyBuffer(mahandle,0,0,1,mavalue);
   Print(mavalue[0]);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
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[])
{

   return(rates_total);
}
//+------------------------------------------------------------------+

can anyone give me a clue about what cause  this problem?

 
Mohsen Farghadani:

hi

this code is mql5 script and print the ma value of D1 period and work as it expected:

but same code in custom indicator does not work correctly and seems copy buffer assigns no value to mavalue variable and the return value of copybuffer is -1):

can anyone give me a clue about what cause  this problem?

Don’t try and get data values in OnInit() because the data is unlikely to be ready
 
void OnStart()
{
   int mahandle=iMA(NULL,PERIOD_D1,1,0,MODE_SMA,PRICE_MEDIAN);
   CopyBuffer(mahandle,0,0,1,mavalue);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate/OnStart (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
Paul Anscombe #:
Don’t try and get data values in OnInit() because the data is unlikely to be ready

yes you are right but in this case even with changing the place of copy buffer to onCalculate the printed value is wrong and return value of copybuffer is -1.

this is the new code:

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite


int mahandle;
double ma[];
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0,ma,INDICATOR_DATA);
   mahandle=iMA(NULL,PERIOD_D1,1,0,MODE_SMA,PRICE_MEDIAN);
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
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[])
{
   double mavalue[1];
   CopyBuffer(mahandle,0,0,1,mavalue);
   Print(mavalue[0]);
   return(rates_total);
}
//+------------------------------------------------------------------+
 
William Roeder #:

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate/OnStart (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

actually the code you reply to is working corectly the problem is the other one.

Perhaps before making assumptions you should pay enough attention to the question

as i mentioned in #3 the problem is not where data values called and assigned to my variable. it seems the problem is with timeframe in iMA() if it's be the same with current time copybuffer work as expected but for higher time frames its not.

also i checked last error after calling copybuffer and it is : 4806

 
Mohsen Farghadani #:

actually the code you reply to is working corectly the problem is the other one.

Perhaps before making assumptions you should pay enough attention to the question

as i mentioned in #3 the problem is not where data values called and assigned to my variable. it seems the problem is with timeframe in iMA() if it's be the same with current time copybuffer work as expected but for higher time frames its not.

also i checked last error after calling copybuffer and it is : 4806

Your post #3 didn’t exist when William replied to you !!!!
Now you are talking about different timeframes you need to deal with data synchronisation because the problem is the same - data not ready

Lots of forum posts on it especially from William
Suggest you do a search