Questions from Beginners MQL5 MT5 MetaTrader 5 - page 116

 
In principle it was, the error was probably in the fact, that my indicator handle is created in the OnTick function and data is copied immediately on the same tick. I have understood it at night, now I will try to transfer indicator handles to OnInit function.
Документация по MQL5: Основы языка / Функции / Функции обработки событий
Документация по MQL5: Основы языка / Функции / Функции обработки событий
  • www.mql5.com
Основы языка / Функции / Функции обработки событий - Документация по MQL5
 
sss20192:
The error was in the indicator handle created in the OnTick function and data are copied on the same tick. I have understood it at night, now I will try to transfer indicator handles to OnInit function.

It didn't work. Here's the code now


#property version   "1.00"

input int                  InpFastEMA=12;                // Fast EMA period
input int                  InpSlowEMA=26;                // Slow EMA period
input int                  InpSignalMA=9;                // Signal MA period
input ENUM_APPLIED_PRICE   InpAppliedPrice=PRICE_CLOSE;  // Applied price
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int Handle1, Handle2, Handle3;
double MacdArray[];

int OnInit()
  {
//---
  Handle1 = iMACD(_Symbol, PERIOD_M5, InpFastEMA, InpSlowEMA, InpSignalMA, InpAppliedPrice);
  Handle2 = iMACD(_Symbol, PERIOD_M15, InpFastEMA, InpSlowEMA, InpSignalMA, InpAppliedPrice);
  Handle3 = iMACD(_Symbol, PERIOD_H1, InpFastEMA, InpSlowEMA, InpSignalMA, InpAppliedPrice);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
        CopyBuffer(Handle1, 0, 1, 1, MacdArray);
        Print(MacdArray[0], " ", GetLastError());
        
        CopyBuffer(Handle2, 0, 1, 1, MacdArray);
        Print(MacdArray[0], " ", GetLastError());
        
        CopyBuffer(Handle3, 0, 1, 1, MacdArray);
        Print(MacdArray[0], " ", GetLastError());
  }
//+------------------------------------------------------------------+

The following messages appear in the log

2013.04.13 15:21:31 2010.01.04 00:00:01 6.187448020344988e-005 0

2013.04.13 15:21:31 2010.01.04 00:00:01 6.187448020344988e-005 0

2013.04.13 15:21:31 2010.01.04 00:00:01 6.187448020344988e-005 0

And it's always like that. There doesn't seem to be a copying error.

 
sss20192: It did not help. Here is the code

But so far you have neither a check for successful creation of handles, nor a check for successful calculation of indicators and check for copying. The idea to move initialization of handles to OnInit() is correct.

Also, add the Print(MacdArray[0]) line at the beginning of OnTick() - to see what rubbish value is contained in the array before you start copying buffers.

To add. Of course, using GetLastError() after the function itself may be a peculiarity of programming style. But I still would check it relying on the example from the Reference. If I'm not mistaken, a null value of GetLastError() does not always mean that the function worked successfully.

 
Yedelkin:
But so far you have neither a check for successful creation of handles, nor a check for successful calculation of indicators and check for copying. The idea to move initialization of handles to OnInit() is correct.
But only for the first time. If in any other place of the program it turns out that some handle is invalid, we have to try to get it again. Therefore, we cannot say that code written for getting handles in OnInit() will completely solve the issue and be 100% correct. ))
 
Checked how much data was copied with CopyBuffer function, always writes 1. And there is no error, but data is still wrong.
Документация по MQL5: Доступ к таймсериям и индикаторам / CopyBuffer
Документация по MQL5: Доступ к таймсериям и индикаторам / CopyBuffer
  • www.mql5.com
Доступ к таймсериям и индикаторам / CopyBuffer - Документация по MQL5
 

tol64

Yedelkin : The idea to move initialization of handles to OnInit() is a good one.

But only for the first time. If anywhere else in the program it turns out that some handle is invalid, you have to try to get it again. Therefore, we cannot say that code written for getting handles in OnInit() will completely solve the issue and be 100% correct. ))

Are you saying that the idea of moving the initialization of handles to OnInit() from OnTick() is wrong in itself?
 
sss20192:
I checked how much data was copied with CopyBuffer function, it always writes 1. And there is no error, but data is still wrong.

I have this result of this code in the tester:

//---

The only thing I did before the test was just converted indicator values before output to the log:

Print(DoubleToString(MacdArray[0],Digits())," ",GetLastError());
 
Yedelkin:
Are you saying that the idea to move handler initialization to OnInit() from OnTick() is wrong by itself?
If only in OnInit() and nowhere else, then yes - wrong. The first time we try to get a handle in OnInit(). Then, before each attempt to obtain indicator data, we check if the handle is valid. If it is valid, we get the data, if not, we try to get the handle again.
 
tol64:

I got this result of this code in the tester:

//---

The only thing I did before the test was to simply convert the indicator values before outputting to the log:

Ooohtyg genius! Thank you very much! Third day solving the problem)
 
tol64:
Yedelkin: Are you saying that the idea of moving the initialisation of handles to OnInit() from OnTick() is wrong by itself?
If only in OnInit() and nowhere else, yes - wrong. The first time we try to get a handle in OnInit(). Then, before each attempt to get indicator data, we check if the handle is valid. If it is valid, we get the data, if not, we try to get the handle again.

And without "if"? And in relation to this particular situation? The indicator handle was created in the OnTick function during every tick, and the data is copied during the same tick. It means that the same indicator handle was requested each time, without checking for validity/invalidity. Against this background, will you continue to argue that the idea of transferring the initialization of handles to OnInit() from OnTick() is wrong by itself?

PS. It seems that you have helped the man, so the question may be considered exhausted.