Concept to wait until the chart opened is properly set up

 

Hello guys,

I am testing in custom indicator to get screenshots of all currency pairs and all time frames in a chart open. I used CharOpen() function. But some price charts take too long time to load prices and bar data. I only got the "Loading" message in screenshot. I use Sleep(5000); to wait for a moment. But it is not enough especially when Metatrader 5 app is just started. So, I'm wondering to hold till the chart is properly set up. Sorry for my English. Thank you.

 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Kyaw_Swar: I am testing in custom indicator to get screenshots of all currency pairs and all time frames in a chart open. I used CharOpen() function. But some price charts take too long time to load prices and bar data. I only got the "Loading" message in screenshot. I use Sleep(5000); to wait for a moment. But it is not enough especially when Metatrader 5 app is just started. So, I'm wondering to hold till the chart is properly set up. 

The Sleep() function has no effect in Indicators.

The Sleep() function can't be called for custom indicators, because indicators are executed in the interface thread and must not slow down it. The function has the built-in check of EA halt flag every 0.1 seconds.

Documentation on MQL5: Common Functions / Sleep
Documentation on MQL5: Common Functions / Sleep
  • www.mql5.com
Sleep - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Kyaw_Swar:

Hello guys,

I am testing in custom indicator to get screenshots of all currency pairs and all time frames in a chart open. I used CharOpen() function. But some price charts take too long time to load prices and bar data. I only got the "Loading" message in screenshot. I use Sleep(5000); to wait for a moment. But it is not enough especially when Metatrader 5 app is just started. So, I'm wondering to hold till the chart is properly set up. Sorry for my English. Thank you.

One proper way to do that is to build an indicator that takes the screenshot in OnCalculate ().

This function will be called once the chart is set up.

The indicator should remove itself from the chart after taking a screenshot and saving it.

Now you need a second program, can also be an indicator, that opens the chart with ChartOpen () and injects the screenshot indicator into the newly opened chart.

The screenshot indicator could also close the chart again, in OnDeinit ().

Another way is, if you plan to take multiple screenshots, to use OnChartEvent with custom events.

This would be a more advanced way of doing it.

You again open the chart and inject the screenshot indicator into the chart.Then you send a custom event to the newly opened chart.

Inside the screenshot indicator, you need a function to create the screenshot.

Now here is a "twist", as you might initially receive the custom event before the OnCalculate was called for the first time.

So what you need to do is, if OnCalculate was not called jet but you have received the request for the screenshot, you need to wait for the call to OnCalculate. Then, as it gets called you call the take screenshot function.

In subsequent Events requesting a screenshot, you can check if OnCalculate has already been called once, and take the screenshot immediately.

This requires you to have two global scope status variables, one for OnCalculate called, and one for "take a screenshot" event received but not processed jet.


This whole concept will give you a nonblocking method of taking screenshots of charts from a master program that controls the screenshot gathering.

In case you need also feedback from the screenshot indicators, you can make them send custom events back to the master program, once they have taken the screenshot.

As said, second method is more advanced, and the better solution.
Reason: