Download MetaTrader 5

How to check if terminal was just started?

To add comments, please log in or register
Marcus Riemenschneider
269
Marcus Riemenschneider  

Hi all,

every morning I start the terminal I get many wrong signals because it takes a couple of seconds until all of my 20 chart windows are refreshed. So my indicators work during the refresh-process what they shouldn't do.

I know that indicators can't wait but is there a possibility to check if the terminal was started for the first time today? I didn't find anything useful in TerminalInfoInteger()...

kypa
823
kypa  
https://www.mql5.com/en/docs/common/getmicrosecondcount
Documentation on MQL5: Common Functions / GetMicrosecondCount
Documentation on MQL5: Common Functions / GetMicrosecondCount
  • www.mql5.com
//| Test function                                                    | //| Script program start function                                    |
whroeder1
17951
whroeder1  
Marcus Riemenschneider: every morning I start the terminal I get many wrong signals because it takes a couple of seconds
  1. You are worrying about a "couple of seconds," and want to spend hours trying to kludge something?
  2. Stop closing the terminal. Problem fixed.
Marcus Riemenschneider
269
Marcus Riemenschneider  

@ kypa: Thanks, that works perfect!

@whroeder1: Stop closing would be a solution. But see this one. Very simple and effective. Just waiting for 10 seconds to load all data into the charts.

//+------------------------------------------------------------------+
//| 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[])
  {
//---
   
   if (GetMicrosecondCount()<10000000) return(0);
kypa
823
kypa  

An extra function call on every tick might not be the best idea, you may consider putting some boolean instead or somehow putting the check in OnInit.

By the way initial false signals might be caused by uninitialized variables resulting in checking if 0==0.

Marcus Riemenschneider
269
Marcus Riemenschneider  

The inital wrong signals are not really false but they are old because the indicator uses old data. When I open the MT4 in the morning the indicator works faster than the chart windows are refreshed and so it works with the last saved data from the previous evening before loading the current data. So I actually have to make the indicator wait until all chart windows are updated. But an indicator can't wait and so I need a way to avoid its execution somehow until all data is loaded.

Do you think it would be a better way to check it in the OnInit()? I don't know what happens if the return parameter is INIT_FAILED. Will this cause the OnInit() to start over again until the return parameter is INIT_SUCCEEDED? If yes this might be the best solution:

int OnInit()
  {
//--- indicator buffers mapping

   if (GetMicrosecondCount()<10000000) return(INIT_FAILED);

//---
   return(INIT_SUCCEEDED);
  }

What do you think?

Edit: I just noticed that it doesn't work in the OnInit(). So I will use the first version in the OnCalculate() because that worked fine.
kypa
823
kypa  
return(INIT_FAILED);

This will probably unload the indicator from chart, try this:

bool ready=false;
while(ready==false) if(GetMicrosecondCount()>1000000) ready=true;

If it doesn't max out the cpu and disrupt terminal starting it should be a better solution.

Alain Verleyen
Moderator
36733
Alain Verleyen  
Marcus Riemenschneider:

The inital wrong signals are not really false but they are old because the indicator uses old data. When I open the MT4 in the morning the indicator works faster than the chart windows are refreshed and so it works with the last saved data from the previous evening before loading the current data. So I actually have to make the indicator wait until all chart windows are updated. But an indicator can't wait and so I need a way to avoid its execution somehow until all data is loaded.

Do you think it would be a better way to check it in the OnInit()? I don't know what happens if the return parameter is INIT_FAILED. Will this cause the OnInit() to start over again until the return parameter is INIT_SUCCEEDED? If yes this might be the best solution:

What do you think?

Edit: I just noticed that it doesn't work in the OnInit(). So I will use the first version in the OnCalculate() because that worked fine.
You should rather fix your original code to avoid to use old data, rather than trying to apply a bad workaround (what will happen if it needs more than 1 second ?).
Marcus Riemenschneider
269
Marcus Riemenschneider  

But how should I fix it?

I look back in a loop for the last fractal high on M15. If this high was broken, an alert pops up. Assuming price rose during the night. When I open the MT4 in the morning an alert pops up. But this alert is based on the current price compared to the last fractal high stored in the data history and not the actual current fractal high which wasn't found yet because of the missing current data.

So I don't see any chance to modify my code because there is no logical error in it. It's just the lack of fresh data.

I also tried this version but although it seems to be logically correct, it doesn't work either:

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[])
  {
  
  if (Open[0]<TimeCurrent()-PeriodSeconds()) return(0);
Alain Verleyen
Moderator
36733
Alain Verleyen  
Marcus Riemenschneider:

But how should I fix it?

I look back in a loop for the last fractal high on M15. If this high was broken, an alert pops up. Assuming price rose during the night. When I open the MT4 in the morning an alert pops up. But this alert is based on the current price compared to the last fractal high stored in the data history and not the actual current fractal high which wasn't found yet because of the missing current data.

So I don't see any chance to modify my code because there is no logical error in it. It's just the lack of fresh data.

I also tried this version but although it seems to be logically correct, it doesn't work either:

Of course there is a logical error if you get an alert (which I suppose is only needed for live signals) using "old" data.

Anyway, if you need coding help show your code. Nobody can guess how to fix an invisible code.

whroeder1
17951
whroeder1  
  if (Open[0]<TimeCurrent()-PeriodSeconds()) return(0);
This will not help. In #4, #6, timecurrent and open will be what it was and therefor no return. In #7 they both update, and therefor no return.
OnInit (or on load,) as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
12
To add comments, please log in or register