Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1501

 
Николай К:

Please advise how to solve the error 4806 - requested data not found. I want to get data of different averaging periods for bolinger from indicator. The Handle is received in OnInit, but for my purpose it should be inOnCalculate, hence the error. Help me to understand it by example.


code
 
//+------------------------------------------------------------------+
//|                                                          вап.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

//---
   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 limit=rates_total-prev_calculated;
   if(limit>1) 
     {
      limit=rates_total-1;                 
     }
   for(int i=limit; i>=0 && !IsStopped(); i--)
     {
      if(i>=rates_total-10) 
         continue;

      ArrayInitialize(middle1,0);
      ArrayInitialize(middle2,0);


      for(BandsPeriod=2; BandsPeriod<=5; BandsPeriod++)
        {
         int T=3;
         Handle1=iMA(Symbol(),PERIOD_CURRENT,BandsPeriod,InpBandsShift,MODE_SMA,PRICE_HIGH);
         Handle2=iMA(Symbol(),PERIOD_CURRENT,BandsPeriod,InpBandsShift,MODE_SMA,PRICE_LOW);
         count=Bars(Symbol(),PERIOD_CURRENT)-10;
         if(!iGetArray(Handle1,BASE_LINE,start_pos,count,middle1) ||
            !iGetArray(Handle2,BASE_LINE,start_pos,count,middle2))// ||
            //CopyRates(Symbol(),PERIOD_CURRENT,start_pos,count,rates)!=count)

           {
            return(false);
           }
        }
      //--- return value of prev_calculated for next call
      return(rates_total);
     }
     }
//+------------------------------------------------------------------+
   
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      if(1==1)
         PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      if(1==1)
         PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                     __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+
Organised roughly like this, sorry for the integrity.
 
Николай К:
Organised like this, sorry for the integrity.

The indicator handle in MQL5 must be created in OnInit()!

 
Vladimir Karputov:

The indicator handle in MQL5 must be created in OnInit()!

Then there will be a separate handle for each averaging period?

 
Николай К:
It is organized like this, sorry for the consistency.

The resource is not freed up...but otherwise it's correct, though not efficient

after iMA(...) and operations with it, you need to call IndicatorRelease if you don't need it anymore

IndicatorRelease() function serves to release the indicator from the computer memory, to which this indicator handle is passed.

And an indicator handle can be created anywhere, except OnDeinit. Just worry about "what to do in case of creation error".

 
@Vladimir Karputov @Maxim Kuznetsov Glue or nail? )) Simple task - complex preparation, in mql4 it was done in one or two. So, how do I gather the bolinger data for different timeframes and averaging periods into one array without errors?
 
Николай К:
@Vladimir Karputov @Maxim Kuznetsov Nail it or nail it? )) Simple task - complex preparation, in mql4 was done in one two. So, how do I gather the bolinger data for different timeframes and averaging periods into one array without errors?

You got it right, you just ate all the handles, it's an exhaustible resource... you need to free them.

hdl=iMA(....)

if (hdl!=INVALID_HANDLE) {

    /// do something

   IndicatorRelease(hdl);

}

see how fxsaber did it in his libraries to make it easier to move from 4 to 5

 
MakarFX:
The first thing that comes to mind is to check the operation of the indicator and then watch the EA

I can't find anything there, in the indicator there are no lines like - outputs if..., every tick is processed, and in the robot comes every tick.

The entire m1 has no signal until the bar changes and a new signal, recall the signal all ticks are watching, processing a new bar is not.

And i remind you that i have to test in 3 terminals at once. i will be buried with 3 terminals in my hand.

 
Fast235:

I can't find anything there, in the indicator there are no lines like - outputs if..., every tick is processed, and in the robot comes every tick.

The entire m1 has no signal until the bar changes and a new signal, recall the signal all ticks are watching, processing a new bar is not.

The robot has no signal in all aspects of the robot.

Perhaps in the indicator the signal only when the bar changes...

Better to see the code of course.

 
Hello Artem!
I am envious to see how quickly comrades in MQL5 code using CTrade and other classes, originally built in MT5.
Due to various circumstances, I have to code in MQL4 and come up with my own awkward constructs.
Maybe, there is such a set of classes for MQL4 already?
Thanks in advance)