ChartSetSymbolPeriod() and a problem with the message queue

Back to topics list To post a new topic, please log in or register
avatar
372
mar 2015.10.21 19:02
 

Hi coders,

here is a little test script I made. My problem is that I would like to switch timeframes and do something when the timeframe has changed. But the problem is that ChartSetSymbolPeriod() is not executed immediately and so everything after that will be executed on the timeframe before. Does someone has an idea how to solve that problem?

void OnStart()
  {
//---
   ChartSetSymbolPeriod(0, _Symbol, PERIOD_MN1);
   ChartScreenShot(0, "test.gif", 1280, 720);  
  }

 This script always saves a screenshot of the chart before switching to the MN1.

avatar
1219
ggekko 2015.10.21 19:18 #
 
mar:

Hi coders,

here is a little test script I made. My problem is that I would like to switch timeframes and do something when the timeframe has changed. But the problem is that ChartSetSymbolPeriod() is not executed immediately and so everything after that will be executed on the timeframe before. Does someone has an idea how to solve that problem?

 This script always saves a screenshot of the chart before switching to the MN1.

"Changes the symbol and period of the specified chart. The function is asynchronous, i.e. it sends the command and does not wait for its execution completion. The command is added to chart message queue and executed only after all previous commands have been processed."

https://docs.mql4.com/chart_operations/chartsetsymbolperiod

avatar
1110
Ovo 2015.10.21 20:18 #
 
ggekko:

"Changes the symbol and period of the specifiedchart. The function is asynchronous, i.e. it sends the command and doesnot wait for its execution completion. The command is added to chartmessage queue and executed only after all previous commands have beenprocessed."

https://docs.mql4.com/chart_operations/chartsetsymbolperiod

Well, finally it is asynchronous, but in the beginning it really was synchronous, i.e. it ran deinit(), changed the period, then init() and then it continued with a command following the ChartSetSymbolPeriod. A complete disaster.
avatar
372
mar 2015.10.21 20:21 #
 
@ggekko: Yes, I read that. I also mentioned in my question that it is not executed immediately. So I know I can't use ChartSetSymbolPeriod(). But do you have an idea how to solve this problem?
avatar
1219
ggekko 2015.10.21 21:12 #
 
mar:
@ggekko: Yes, I read that. I also mentioned in my question that it is not executed immediately. So I know I can't use ChartSetSymbolPeriod(). But do you have an idea how to solve this problem?
Check whether the timeframe has been changed or not . If yes, then make the screenshot.
avatar
372
mar 2015.10.21 21:29 #
 
In this case it would work but if I use it in a more complex indicator it's not so easy to implement. I think I will try it by using the user32.dll.
avatar
736
honest_knave 2015.10.21 22:42 #
 

You need a flag that can survive the reinit. Example using an EA:

#property strict
   
bool FirstRun=true;

int OnInit()
  {
   EventSetTimer(3);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

void OnTick()
  {
  }

void OnTimer()
  {
   if(FirstRun)
     {
      FirstRun=false;
      Print("Changing timeframe");
      ChartSetSymbolPeriod(0,NULL,PERIOD_MN1);
     }
   else
     {
      EventKillTimer();
      Print("Taking screenshot");
      ChartScreenShot(0,"Screenshot.png",1024,768);
     }  
  }

 

 

avatar
372
mar 2015.10.22 16:36 #
 
I never worked with a timer but I'll give it a try. Thanks you!
avatar
53
amir_avatar 2015.10.22 18:46 #
 

alternatively, you can just wait until it is changed (in case you don't mind to wait here and loose the asynchronity)

void OnStart()  
{
//---   
ChartSetSymbolPeriod(0, _Symbol, PERIOD_MN1);   
while (ChartSymbol(0) != _Symbol && ChartPeriod(0) != PERIOD_MN1)
{
   Sleep(1500); 
}
ChartScreenShot(0, "test.gif", 1280, 720);   
}
avatar
13006
WHRoeder 2015.10.23 18:24 #
 
That won't work Amir, on chart change you go through a deinit/init cycle witch won't happen in the while loop.
avatar
53
amir_avatar 2015.10.24 00:42 #
 
I know that, WHRoeder, I thought that what Ovo said earliear in this thread - "Well, finally it is asynchronous, but in the beginning it really was synchronous, i.e. it ran deinit(), changed the period, then init() and then it continued with a command following the ChartSetSymbolPeriod. A complete disaster." - meant that it doesn't happen anymore. If it still runs through you are right of course.
Back to topics list  

To add comments, please log in or register