Download MetaTrader 5

How to initialize if EA starts in sunday

To add comments, please log in or register
one_monk
53
one_monk  

Hello,

I have a problem when my testing interval starts in Sunday. I used current time for starting point (marked in yellow). If the EA starts in Sunday I do not accept data. When the EA is already started - no problems.

Can somebody gave me a solution?


CopyBuffer(Handler, 0, TimeCurrent(), 100, Array)

Code2219 or probably 2319
216
Code2219 or probably 2319  

one_monk:

...If the EA starts in Sunday I do not accept data. When the EA is already started - no problems...

re-stating your question will help a lot.

if you wanna know whether it's Sunday or not , this could help : [important note : TimeCurrent is called inside OnTick]
bool first_tick_ever = true; // better be a global variable
bool Trades_allowed = true; // same recommendation

if(first_tick_ever)
{
        MqlDateTime TickTime;
        TimeToStruct(TimeCurrent(), TickTime);
        if(TickTime.day_of_week == 0) Trades_allowed = false;
        first_tick_ever = false;
}
one_monk
53
one_monk  

I make my own solution. This code must be putted in OnInit function.

    datetime Time = TimeCurrent();

    for(;;)

      {

        if(Time == TimeCurrent())

          {

            Sleep(1000);

          }

        else

          {

            break;

          }

      }

Code2219 or probably 2319
216
Code2219 or probably 2319  
one_monk:

I make my own solution. This code must be putted in OnInit function.

    datetime Time = TimeCurrent();

    for(;;)

      {

        if(Time == TimeCurrent())

          {

            Sleep(1000);

          }

        else

          {

            break;

          }

      }

using infinite loops, and  TimeCurrent() and Sleep() inside OnInit is not a good choice. your solution attempts all :)

one_monk
53
one_monk  
Why? Where is the problem?
Code2219 or probably 2319
216
Code2219 or probably 2319  
one_monk:
Why? Where is the problem?
TimeCurrent returns the time of last tick of any pair active in MarketWatch window.
which if called outside OnTick, the time returned by TimeCurrent can be for up to two days ago. (or more if terminal is disconnected.)
and OnInit has a limited time window to finish. (5 seconds IIRC) so a possible infinite loop and Sleep function (and now also probably the iTime, iOpen, iHigh.... ) are not good ideas to be used in OnInit.
Code2219 or probably 2319
216
Code2219 or probably 2319  
if you state your problem exactly (giving a few weekday and times examples) it will be easier to suggest a solution
one_monk
53
one_monk  

Ahhhhaaaaaa, I understand. My solution work on backtest but in run mode will crash my EA if it is started in weekend. I simply add a check for mode and I believe now all is ok.

Thanks for explanation Code2219 or probably 2319!

To add comments, please log in or register