EA won't restart after ChartSetSymbolPeriod

 

Hello all,

Long time lurker, first time poster, as I always find my answer by searching the forum. Not this time though, and I have been scratching my head for a while.

After calling ChartSetSymbolPeriod (either to change Symbol or Period), my EA deinitializes (as it should), my chart is updated with new Symbol/Period (as it should), however my EA does not start again. It is simply deleted from chart without ever restarting.

What I already tried:

1. Calling ChartSetSymbolPeriod from different places in the code (from OnInit, from OnTimer), with same result.

2. Checking for REASON_CHARTCHANGE as first thing in OnDeInit and immediately returning from function (to avoid doing a bunch of stuff within OnDeInit)

3. Placing strategical "Prints" as first thing in OnInit and last thing in OnDeInit, to try to find potential offending code that might be causing this behaviour ("Last thing in OnDeInit" prints OK, but OnInit is never called again).


Any other suggestions are appreciated! Thank you

 
Rogerio Celentano Szterling:

Hello all,

Long time lurker, first time poster, as I always find my answer by searching the forum. Not this time though, and I have been scratching my head for a while.

After calling ChartSetSymbolPeriod (either to change Symbol or Period), my EA deinitializes (as it should), my chart is updated with new Symbol/Period (as it should), however my EA does not start again. It is simply deleted from chart without ever restarting.

What I already tried:

1. Calling ChartSetSymbolPeriod from different places in the code (from OnInit, from OnTimer), with same result.

2. Checking for REASON_CHARTCHANGE as first thing in OnDeInit and immediately returning from function (to avoid doing a bunch of stuff within OnDeInit)

3. Placing strategical "Prints" as first thing in OnInit and last thing in OnDeInit, to try to find potential offending code that might be causing this behaviour ("Last thing in OnDeInit" prints OK, but OnInit is never called again).


Any other suggestions are appreciated! Thank you

if you only have the symbol change in the code does it work ?

int OnInit()
  {
  Print("START "+_Symbol+" "+EnumToString((ENUM_TIMEFRAMES)_Period));
  EventSetMillisecondTimer(66);
  return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  Print("Exit");
  }

void OnTick()
  {

   
  }

void OnTimer()
  {
  EventKillTimer();
  ChartSetSymbolPeriod(0,"GBPUSD",PERIOD_D1);
  }
 
Lorentzos Roussos #:

if you only have the symbol change in the code does it work ?

Thank you for your answer. A simple code such as yours works fine. And I have used ChartSetSymbolPeriod successfully in other EAs.

In this one specific EA, though, it is not working. I can't figure out what's different.

 
Rogerio Celentano Szterling #:

Thank you for your answer. A simple code such as yours works fine. And I have used ChartSetSymbolPeriod successfully in other EAs.

In this one specific EA, though, it is not working. I can't figure out what's different.

Is it trying to switch to the same symbol ? 

i recall in a correlations ea that was the stopping point , it tried to navigate to the current symbol

also if you are calling it from on timer add these after :

EventKillTimer();
return;
your code keeps executing , the symbol change function does not fire instantly btw
 
Lorentzos Roussos #:

Is it trying to switch to the same symbol ? 

i recall in a correlations ea that was the stopping point , it tried to navigate to the current symbol

also if you are calling it from on timer add these after :

your code keeps executing , the symbol change function does not fire instantly btw

Thanks Lorentzos. The issue occurs in any combination:

1) Change to another TF keeping the same symbol;

2) Change to another symbol keeping the same TF;

3) Change both symbol and TF


Tried your OnTimer tip; no luck either. Now I will try to strip my EA of other functions until I get it to work again.

 
Well, it seems that it is some issue with my MT5 terminal, as the problem is not happening in other terminals. I will reinstall it to see if the issue disappears.
 

Unfortunately the issue is still occurring intermittently, in different terminals, and I can't find the reason.

Sometimes it works fine: the EA runs ChartSetSymbolPeriod to change the Symbol and/or TF, then restarts itself, as it should.

Often, though, after changing Symbol and/or TF the EA simply exits (after running OnDeInit normally), without ever restarting.

I am scratching my head for days on this.

If anyone has any other suggestions, I would love to hear them!

 
Rogerio Celentano Szterling #:

Unfortunately the issue is still occurring intermittently, in different terminals, and I can't find the reason.

Sometimes it works fine: the EA runs ChartSetSymbolPeriod to change the Symbol and/or TF, then restarts itself, as it should.

Often, though, after changing Symbol and/or TF the EA simply exits (after running OnDeInit normally), without ever restarting.

I am scratching my head for days on this.

If anyone has any other suggestions, I would love to hear them!

Have you tried putting ChartSetSymbolPeriod() conditionally inside Ontick()?

 
Ryan L Johnson #:

Have you tried putting ChartSetSymbolPeriod() conditionally inside Ontick()?

Hi Ryan, yes, I tried several different spots inside OnTick and OnTimer.

 

For an EA:

Forum on trading, automated trading systems and testing trading strategies

Changing chart period on tick with ChartSetSymbolPeriod

AIRAT SAFIN, 2022.11.18 22:58

void OnTick()                      
{
if ( fmod(TimeLocal(),10)>5)
{
move_to_a_different_TF_1();
}
else
{
move_to_a_different_TF_2();
}
}

void move_to_a_different_TF_1()
{
   ChartSetSymbolPeriod(0, NULL, PERIOD_H1);
}
void move_to_a_different_TF_2()
{
   ChartSetSymbolPeriod(0, NULL, PERIOD_M1);
}



Your code works perfectly
<but market will be closed soon and there will be no ticks>
<<use Tester then or function OnTimer() instead of OnTick()>>


 

Yes, regardless of ChartSetSymbolPeriod() being called from OnTick() or from OnTimer(), the symbol/TF changes correctly, the EA de-initializes, but doesn't restart - in other words, the EA dettaches from the chart.

So the function ChartSetSymbolPeriod() is apparently working, but whatever it is that should trigger the EA to restart isn't - and that's what is puzzling. I can't use the function as it is removing the EA without starting again.