Return 4806 when create a iMACD handle in OnTick and useiGetArray to get buffer data, what`s the problem?

 

The following is my test code

void OnTick()
  {
//---
   tick_cnt++;
   if(tick_cnt % 100 == 0)
     {
      signal_period++;
      int handle =iMACD(Symbol(),PERIOD_CURRENT,120,260,signal_period, PRICE_CLOSE);
      if(handle==INVALID_HANDLE)
        {
         PrintFormat("Failed to create handle of iMACD indicator for the symbol %s/%s, signal_period:%d, error code %d",
                     Symbol(),
                     EnumToString(Period()),
                     signal_period,
                     GetLastError());
         return;
        }
      double macd_buffer[];
      ArraySetAsSeries(macd_buffer,true);
      double signal_buffer[];
      ArraySetAsSeries(signal_buffer,true);
      if(!getMacdBuffer(handle, 0, 6, macd_buffer, signal_buffer))
        {
         PrintFormat("getMacdBuffer failed!%d", handle);
        }
        else{
          PrintFormat("getMacdBuffer OK!%d", handle);
        }
      IndicatorRelease(handle);
     }

  }

and the log

2020.09.16 09:08:22.250 2020.07.02 13:21:11   ERROR! EA: macd_demo.mq5, FUNCTION: iGetArray, amount to copy: 6, copied: -1, error code 4806
2020.09.16 09:08:22.250 2020.07.02 13:21:11   INFO! EA: macd_demo.mq5, FUNCTION: getMacdBuffer, iGetArray 0 failed! curr_time:2020.07.02 13:21
2020.09.16 09:08:22.250 2020.07.02 13:21:11   getMacdBuffer failed!636
2020.09.16 09:08:22.287 2020.07.02 13:24:24   ERROR! EA: macd_demo.mq5, FUNCTION: iGetArray, amount to copy: 6, copied: -1, error code 4806
2020.09.16 09:08:22.287 2020.07.02 13:24:24   INFO! EA: macd_demo.mq5, FUNCTION: getMacdBuffer, iGetArray 0 failed! curr_time:2020.07.02 13:24
2020.09.16 09:08:22.287 2020.07.02 13:24:24   getMacdBuffer failed!637
2020.09.16 09:08:22.313 2020.07.02 13:27:41   ERROR! EA: macd_demo.mq5, FUNCTION: iGetArray, amount to copy: 6, copied: -1, error code 4806
2020.09.16 09:08:22.313 2020.07.02 13:27:41   INFO! EA: macd_demo.mq5, FUNCTION: getMacdBuffer, iGetArray 0 failed! curr_time:2020.07.02 13:27
2020.09.16 09:08:22.313 2020.07.02 13:27:41   getMacdBuffer failed!638
2020.09.16 09:08:22.328 2020.07.02 13:30:00   ERROR! EA: macd_demo.mq5, FUNCTION: iGetArray, amount to copy: 6, copied: -1, error code 4806
2020.09.16 09:08:22.328 2020.07.02 13:30:00   INFO! EA: macd_demo.mq5, FUNCTION: getMacdBuffer, iGetArray 0 failed! curr_time:2020.07.02 13:30
 
Hardy5012 :

The following is my test code

and the log

You are making a gross mistake! In MQL5, the indicator handle SHOULD be received ONLY ONCE! This is done in OnInit ().

  • Example from help: iMACD
  • An example of how to create an indicator handle and get indicator values from an expert: iMACD value on chart

 
Vladimir Karputov:

You are making a gross mistake! In MQL5, the indicator handle SHOULD be received ONLY ONCE! This is done in OnInit ().

  • Example from help: iMACD
  • An example of how to create an indicator handle and get indicator values from an expert: iMACD value on chart

Thanks again for your answers.There is a requirement that create a new macd handle as a quotation changed.

 
Hardy5012 :

Thanks again for your answers. There is a requirement that create a new macd handle as a quotation changed.

I did not understand the highlighted phrase . Can you ask your question differently?

 
Vladimir Karputov:

I did not understand the highlighted phrase . Can you ask your question differe

Dynamically created new macd handle(change signal_period paramter) as the market quotaion changed. Like the following code , I use iRSI change signal_period paramter with  iMacd .

void OnTick()
  {
//---
   tick_cnt++;
   if(tick_cnt % 100 == 0)
     {
      double rsi_buffer[];
      ArraySetAsSeries(rsi_buffer,true);
      if(!iGetArray(handle_ris,0,0,1,rsi_buffer))
        {
         PrintFormat("INFO! EA: %s, FUNCTION: %s,  macd_type:%s,  iGetArray 0 failed! curr_time:%s",
                     __FILE__,__FUNCTION__, EnumToString(macd_type), TimeToString(time_current));
         return(false);
        }
      double rsi_value = rsi_buffer[0];
      int signal_period = int((rsi_value - 20) * rsi_macd_coeff + 2);
      int handle =iMACD(Symbol(),PERIOD_CURRENT,120,260,signal_period, PRICE_CLOSE);
 
Hardy5012 :

Dynamically created new macd handle(change signal_period paramter) as the market quotaion changed. Like the following code , I use iRSI change signal_period paramter with  iMacd .

Bad, very bad example.

All indicators must be created in advance in OnInit ().

 
Hardy5012:

Dynamically created new macd handle(change signal_period paramter) as the market quotaion changed. Like the following code , I use iRSI change signal_period paramter with  iMacd .

One option might be the usage of IndicatorCreate() together with IndicatorParameters and IndicatorRelease.

But the better way would be to calculate the MACD yourself and just change the coefficients of the used ema for the fast and slow MA

Documentation on MQL5: Timeseries and Indicators Access / IndicatorCreate
Documentation on MQL5: Timeseries and Indicators Access / IndicatorCreate
  • www.mql5.com
[in] The number of parameters passed in the parameters_array[] array. The array elements have a special structure type MqlParam. By default, zero - parameters are not passed. If you specify a non-zero number of parameters, the parameter field of the first element must contain the name of the custom indicator. The custom indicator must be...
Reason: