On Init

 

Hi,

one question, i noticed that when I ask to do an operation OnInit()...it does the operation when the EA starts, but also when I just change the Timeframe, from 1 minute, to 5 and so on.

Is it possible to ask him to don't do the operation in case I want to change the timeframe?

 
  1. If you change TF or symbol you get a deinit/init cycle.

  2. You should not be doing anything in OnInit other than what is necessary to initialize the EA before the first tick. You shouldn't even try to use any price or server related functions in OnInit as there may be no connection/chart yet:
    1. Terminal starts.
    2. indicators/EAs are loaded.
    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 and prices are now valid.

  3. What do you mean by "an operation?"
 
florenceale:

Hi,

one question, i noticed that when I ask to do an operation OnInit()...it does the operation when the EA starts, but also when I just change the Timeframe, from 1 minute, to 5 and so on.

Is it possible to ask him to don't do the operation in case I want to change the timeframe?

If there is a block of code (operation) in OnInit() you simply don't want to run on subsequent executions of the EA, you could surround that code block with Terminal Global variables.

https://www.mql5.com/en/docs/globals

// This is not complete and untested

if ( GlobalVariableGet(WindowExpertName()) == 1.0 )
{
    Print(StringFormat("EA %s has already run this block of code once.", WindowExpertName()));
}
else
{
    GlobalVariableSet(WindowExpertName(), 1.0);

    {
        // block of code you run once
    }
}

 
  1. What do you mean by "an operation?"

(sorry if my english is not perfect)


I have inserted these functions on Init(),

 ObjectsDeleteAll(0, OBJ_VLINE);
 ObjectsDeleteAll(0, OBJ_ARROW_CHECK);
 ObjectsDeleteAll(0, OBJ_TREND);
 ObjectsDeleteAll(0, OBJ_CHANNEL);
 ObjectsDeleteAll(0, OBJ_ARROW);

because my EA draws lines, arrows etc. And I want every time all clear when it starts. But not if I change the timeframes...I was wondering if it was possible...

 

florenceale:

And I want every time all clear when it starts. But not if I change the timeframes...I was wondering if it was possible...


If you switched the location to Deinit() you can check for how the EA was closed.

If it was because you changed the time frame, don't erase.

void OnDeinit(const int reason)
  {
     if ( reason != REASON_CHARTCHANGE )
     {
        // Clean all drawn objects
        ObjectsDeleteAll(0, OBJ_VLINE);
        ObjectsDeleteAll(0, OBJ_ARROW_CHECK);
        ObjectsDeleteAll(0, OBJ_TREND);
        ObjectsDeleteAll(0, OBJ_CHANNEL);
        ObjectsDeleteAll(0, OBJ_ARROW);
     }
  }
 
Anthony Garot:


If you switched the location to Deinit() you can check for how the EA was closed.

If it was because you changed the time frame, don't erase.


i will try like this, it looks simple. Thanks a lot