Questions from Beginners MQL5 MT5 MetaTrader 5 - page 984

 
Ihor Herasko:


But the fact of the matter is. Sleep should not affect the recalculation of data in the indicator. Something is wrong with buffer filling. Maybe there is a reproducible piece of code?

The indicator was written to order - it's a wild OOP, I don't understand it :(

I spent all night with this problem, while I found the reason of divergence between tester and real account, maybe after sleeping I will remove some of secret logic and post it for review.

 
Aleksey Vyazmikin:

I faced a problem, the indicator based EA works correctly on a real account, but it is lying in the tester, in tick generation modes both by OHLC and by all ticks - the result is the same. The result of the error is the empty buffer of the indicator at zero bar (only when there is a new bar at the upper TF, which is used for the calculation of the indicator). However, I have managed to make indicator to be calculated by adding Sleep to my Expert Advisor. But I have found out that depending on the mode of ticks generation this Sleep should be different - for generation from all ticks Sleep(15000) is enough, while for OHLC Sleep(30000) is needed.

So the question arises - is the situation with Sleep normal, because it logically appears that different delay times are modeled there depending on the mode of tick generation!

Dear developers, please explain the situation with the indicator, because I myself do not understand what the reason is - a mistake in the code or in the tester!

I am ready to give you the indicator and the Expert Advisor in the PM, but tell me to whom.

If you want to copy the price to an array, you have to check if the history is available and check if the price is not changed. For this purpose, we have to check if the history for TF is available, if not, we should try to copy it again and wait until it has been loaded in the loop.

this is a bad hand of the programmer, if he did not know about it

slip is not normal
 
void OnChartEvent(            const int id,        // идентификатор события   
                              const long& lparam,  // параметр события типа long 
                              const double& dparam,// параметр события типа double 
                              const string& sparam // параметр события типа string 
                              )
   {
   Print(My_Name, " ---  ", id, "    lparam = ", lparam, "    dparam = ", dparam, "    sparam = ", sparam, "    ChartID() = ", ChartID() );        // <<|+|+|+<<  // 

   }                              

Please explain...

I press key = I get event id = 0. This can be repeated many times. The result is the same as long as the space bar is not pressed.

Pressed spacebar = I get event id = 0. After that all manipulations on the keyboard don't result in any event.

To get out of the stupor, I press the mouse button = I get event id = 4. After that you can click on the keyboard again = events come for every click. As long as no spacebar is pressed = with the same result.

Question: am I a fool not understanding something or it should not be like this? Please provide a link.

 
Nikita Chernyshov:

Hello colleagues.

Question: In mql4, in order to calculate the number of positions, you can write the function like this

How is it implemented in mql5? How can I calculate positions by magic number or by type?

Add this line before MQL4-function

#include <MT4Orders.mqh> // https://www.mql5.com/ru/code/16006

and it will work in MT5.

 
Maxim Dmitrievsky:

The thing about indicators is that timeseries may not be ready yet, i.e. it cannot copy the price to an array at once. For this purpose, we should check if the history for TF is available, if not, we should try to copy it again and wait until it has been loaded in the loop.

This is a bad hand of the programmer if he/she does not know about it.

slip is not ok

How is this possible in the tester? I understand if the problem is on the real or in the tester there is no history... However, what should this test look like?

Developers are ignoring my message, unfortunate.

 
Aleksey Vyazmikin:

How is this possible in the tester? I understand if the problem is on the real or in the tester there is no history there... However, what should this test look like?

Developers are ignoring my message, unfortunate.

check if prices are copied, if Copyclose or whatever returns -1 then they are not copied

 
Maxim Dmitrievsky:

check if prices are copied, if Copyclose or whatever returns -1 then they are not copied

The indicator is calculated once at appearance of a new bar, which is implemented, if I understand correctly, in this way:

//+------------------------------------------------------------------+
//| 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[])
  {
   static int counted_bars=0;
   if(rates_total==prev_calculated) return(rates_total);

it is also confirmed by a simple print.

That's why the situation is not clear, if we assume that there is no price to be calculated, the indicator will be calculated 1 time and the buffer will remain unfilled, but it's not so - it will fill if we add sleep to the EA and wait. Maybe, the indicator is slow in calculation and the tester just does not wait for it? But how to check it?

 
Aleksey Vyazmikin:

The indicator is calculated 1 time when a new bar appears, which is implemented, if I understand correctly, in this way:

this is also confirmed by a simple print.

That's why the situation is not clear, if we assume that there are no prices to be calculated, the indicator will be calculated once and the buffer will remain unfilled, but this is not the case - it will fill if we add sleep to the EA and wait. Maybe, the indicator is slow in calculation and the tester just does not wait for it? But how to check it?

Maybe it should be timed then

 
Maxim Dmitrievsky:

Maybe we should use a timer then.

Yes, with the timer in the EA the test goes a bit further than without the timer, but worse than with Sleep, and the timer is essentially the same as Sleep.

I think I understand what the problem is, the indicator is calculated using data of two other indicators and the code asks for the number of calculated bars of other indicators

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CChannel::BarsCalculated(void)
  {
   int upchbars=BarsCalculated(m_upch);
   int dnchbars=BarsCalculated(m_dnch);
   if(upchbars<=dnchbars) return(upchbars);
   return(dnchbars);
  }

and the expected calculated number of bars

int barsch=(InpChPeriod==PERIOD_CURRENT?rates_total:Bars(_Symbol,InpChPeriod));

If these two values don't coincide, the buffer is not filled.

   if(!channel.CreateChannel() || barsch<=0 || barsch!=channel.BarsCalculated() || channel.FillChBuffers(rates_total,calculated,time)==0)
     {
      for(;counted_bars<rates_total;counted_bars++)
        {
         ZigzagBuffer[counted_bars]=0.0;
         ZigzagStepBuffer[counted_bars]=0.0;
         UpChBuffer[counted_bars]=0.0;
         DnChBuffer[counted_bars]=0.0;
         InfoZigzagBuffer[counted_bars]=0.0;
         InfoUpChBuffer[counted_bars]=0.0;
         InfoDnChBuffer[counted_bars]=0.0;
         InfoDirectBuffer[counted_bars]=0.0;
         InfoBegPriceBuffer[counted_bars]=0.0;
         InfoEndPriceBuffer[counted_bars]=0.0;
         InfoBegTimeBuffer[counted_bars]=0.0;
         InfoEndTimeBuffer[counted_bars]=0.0;
         InfoStartPriceBuffer[counted_bars]=0.0;
         InfoStartTimeBuffer[counted_bars]=0.0;
         InfoZigzagTotal[counted_bars]=InfoZigzagTotal[counted_bars-1];
        }
      if(InpInfEnd) CopyInfoBuffers(rates_total-1,(int)InfoZigzagTotal[rates_total-1]);
      return(calculated);
     }

If I understand correctly, the indicators are executed in one thread and their priority is distributed by the time of their creation, i.e. it turns out that the indicator I address from the EA has the higher priority and performs its calculation first and after that it passes the thread to those indicators which should perform the calculation (based on the buffers data).

In the print you can see that if I set Sleep of a sufficient size, the recalculation is performed in the indicator (I don't understand how yet) and why only after 13 seconds?

2019.01.22 19:50:16.992 2019.01.21 23:45:00   barsch=6275
2019.01.22 19:50:16.992 2019.01.21 23:45:00   BarsCalculated=6274

2019.01.22 19:50:16.993 2019.01.21 23:45:13   barsch=6275
2019.01.22 19:50:16.993 2019.01.21 23:45:13   BarsCalculated=6275

There is no timer in the indicator.

And what should I do to get correct calculation, trying to use while in EA before waiting for value in buffer doesn't help - hangs.

 
Hello, I wanted to implement a trailing stop with step in my EA. I couldn't find anything other than the template in Kodobase and Macd Sample. Maybe there are other options?
Reason: