EA gets restarted when timeframe changes

 

I have coded my first EA and am now running it on a demo account. While doing this I noticed an effect which is undesired:

I have a chart opened (e.g. EURUSD, H1) and have started the EA on this chart. Autotrading is enabled and the EA seems to be doing what it is supposed to be doing.

In order to see the trades in more detail I change the chart's timeframe from H1 to M15. To my surprise do I see that EA runs its OnDeinit() on the H1 chart, exits, restarts and runs OnInit() on the M10 chart.

This is not what I wanted. I wanted my EA to continue running uninterrupted. My EA does not depend on the timeframe of the chart, so the functionality of the EA does not change when I change any of the chart settings.


My question: what setting, or code, should I use to have the EA uninterrupted when I change  the chart's timeframe?

 
WindmillMQL:

I have coded my first EA and am now running it on a demo account. While doing this I noticed an effect which is undesired:

I have a chart opened (e.g. EURUSD, H1) and have started the EA on this chart. Autotrading is enabled and the EA seems to be doing what it is supposed to be doing.

In order to see the trades in more detail I change the chart's timeframe from H1 to M15. To my surprise do I see that EA runs its OnDeinit() on the H1 chart, exits, restarts and runs OnInit() on the M10 chart.

This is not what I wanted. I wanted my EA to continue running uninterrupted. My EA does not depend on the timeframe of the chart, so the functionality of the EA does not change when I change any of the chart settings.


My question: what setting, or code, should I use to have the EA uninterrupted when I change  the chart's timeframe?

perhaps make sure your ea know if it already has open orders on the current symbol...

 
WindmillMQL:

I have coded my first EA and am now running it on a demo account. While doing this I noticed an effect which is undesired:

I have a chart opened (e.g. EURUSD, H1) and have started the EA on this chart. Autotrading is enabled and the EA seems to be doing what it is supposed to be doing.

In order to see the trades in more detail I change the chart's timeframe from H1 to M15. To my surprise do I see that EA runs its OnDeinit() on the H1 chart, exits, restarts and runs OnInit() on the M10 chart.

This is not what I wanted. I wanted my EA to continue running uninterrupted. My EA does not depend on the timeframe of the chart, so the functionality of the EA does not change when I change any of the chart settings.


My question: what setting, or code, should I use to have the EA uninterrupted when I change  the chart's timeframe?

Changing TF will always reload EAs and indis. 

Just use another chart to observe other TFs

 
Fernando Morales: Changing TF will always reload EAs and indis. 
  1. On MT4, EAs are not reloaded. They just go through a deInit/init cycle. Indicators are reloaded. (Don't know about MT5 but expect the same.)

  2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

    This is the same. Fix your code to handle it, or to undo the change when position(s) are open. Or leave the chart alone.

 
Fernando Morales:

Changing TF will always reload EAs . 

Is there no way to prevent this, or block this?

I would consider it a risk if an EA is running on a chart and somebody, by accident, changes the TF and thus causes the EA to restart.

 
WindmillMQL:

Is there no way to prevent this, or block this?

I would consider it a risk if an EA is running on a chart and somebody, by accident, changes the TF and thus causes the EA to restart.

Yes, code the EA so that it is not affected by changing the time-frame.

 
WindmillMQL:

Is there no way to prevent this, or block this?

I would consider it a risk if an EA is running on a chart and somebody, by accident, changes the TF and thus causes the EA to restart.

This is the OnDeinit() function I use that keeps EA setting and running data intact stored as GV

void OnDeinit(const int reason) 
{
        // Deleting all text on-chart comments 
        Comment("");
        
        //=============================================================
        // Borramos todos los objetos de la interfaz
        ObjectsDeleteAll( 0, NameEA );
                
        //=============================================================
        // Defining the EA exiting reason
        string ExitMsg = getUninitReasonText(_UninitReason);
                        
        //=============================================================
        // Según el motivo de la terminación elgimos borrar las GV o no
        switch( _UninitReason )
        {
                case 2:         // Program has been recompiled
                case 3:         // Symbol or chart period has been changed
                case 5:         // Input parameters have been changed by a user
                case 6:         // Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
                case 9:         // Terminal shutdown
                
                        Print("####---> Keeping EA data in memory");
                        break;

                        
           default:
             GlobalVariablesDeleteAll( NameEA );
             Print("####---> Deleting EA data from memory");
        }
        
        //EventKillTimer();

        //--- The second way to get the uninitialization reason code 
        if ( DEBUG ) Print(__FUNCTION__,"::  UninitReason: ", ExitMsg);
}

 
Fernando Morales:

This is the OnDeinit() function I use that keeps EA setting and running data intact stored as GV

This sounds interesting. So you store certain data upon OnDeinit(). I assume that you load that data on the next OnInit()?

I had not heard of this possibility. How do I know which parameters to store, where to store them (database?), and how to reload the data?

Sorry for the many questions. I'm new to programming an EA and am still learning the ropes.

 
Keith Watford:

Yes, code the EA so that it is not affected by changing the time-frame.

How do I do this? My EA is not affected as it does not use the time frame of the chart. But it is stopped and restarted by MetaTrader when changing the time frame of the chart.
 
WindmillMQL:
How do I do this? My EA is not affected as it does not use the time frame of the chart. But it is stopped and restarted by MetaTrader when changing the time frame of the chart.

There is no correct simple answer as it depends on the code.

 
WindmillMQL:

I have coded my first EA and am now running it on a demo account. While doing this I noticed an effect which is undesired:

I have a chart opened (e.g. EURUSD, H1) and have started the EA on this chart. Autotrading is enabled and the EA seems to be doing what it is supposed to be doing.

In order to see the trades in more detail I change the chart's timeframe from H1 to M15. To my surprise do I see that EA runs its OnDeinit() on the H1 chart, exits, restarts and runs OnInit() on the M10 chart.

This is not what I wanted. I wanted my EA to continue running uninterrupted. My EA does not depend on the timeframe of the chart, so the functionality of the EA does not change when I change any of the chart settings.


My question: what setting, or code, should I use to have the EA uninterrupted when I change  the chart's timeframe?

Set in your EA code the timeframe that the EA will use.

Maybe you should make strict timeframes with the ChartSetSymbolPeriod function.

Reason: