ChartOpen + ChartApplyTemplate + ChartClose to Work Together

 

Hi coders, hope you can help a non-programmer myself ...

I have 2 strategies written in 2 EAs. Currently, I load the 2 EAs into 2 different charts but with different coded conditions (same magic), in other words, at any point of time, only one EA is trading and the other EA is in dormant state (both EAs are switching to each others), depending on the coded conditions. The problem now is running 2 EAs will increase my computer's CPU and memory, this is not a good idea. FYI, I plan to add another 2 more new strategies later (total of 4 strategies), I think my computer will get smoke sooner and later, LOL.

I studied the https://docs.mql4.com/chart_operations , I think the ChartOpen, ChartApplyTemplate and ChartClose commands are suitable for me to save my computer's CPU and memory. To understand each of the chart command, I think I can understand 80% how to code it. However, to combine the 3 commands to work together, I don't think I can code it.

This flowchart picture tells you what I intend to achieve i.e. at any point of time, even if I have 100 different strategies, my MT4 terminal will only have 1 chart that loaded with an EA and will never happen > 1 chart, that's all. Hope you can help, thank you.


FlowChart

Chart Operations - MQL4 Reference
Chart Operations - MQL4 Reference
  • docs.mql4.com
Chart Operations - MQL4 Reference
 

I would recommend you to code each strategy its class derived from a parent class where you can e.g. code the trading methods ..

This way you can run all you strategies on one chart. But for this I would use MT5 as MT4 is somewhat deprecated.

If you can't code it ask here: https://www.mql5.com/en/job

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.05.09
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Carl Schreiber #:

I would recommend you to code each strategy its class derived from a parent class where you can e.g. code the trading methods ..

This way you can run all you strategies on one chart. But for this I would use MT5 as MT4 is somewhat deprecated.

If you can't code it ask here: https://www.mql5.com/en/job

Hi Carl, thanks for your response.


My intension is to learn coding, but not looking for a developer to develop / modify indicator / EA ... So far, my EA is working fine, no problem at all. I just need a coder to provide me the relevant mql4 codes so that I can slowly pick up the coding skills. FYI, I only know mql4 but don't know mql5 totally, LOL.

 

Ok, then learn MQL5 of would you like to start learning DOS instead of Windows? (Read: https://www.mql5.com/en/forum/454319)

Here a link: https://www.mql5.com/de/forum/455957#comment_50048608

And here a bunch of links for beginners:

    Before you learn to program in MQL5, learn to search, because there is practically nothing that has not already been programmed for MT4/MT5!
    => Search in the articles: https://www.mql5.com/en/articles
    => Search in the codebase: https://www.mql5.com/en/code
    => Search in general: https://www.mql5.com/en/search or via Google with: "site:mql5.com .." (forgives typos and variants)
    If you place the cursor on an MQL function and press F1, you will see the reference directly, many with examples to copy and paste - the fastest form to code!
    https://www.mql5.com/en/articles/496
    https://www.mql5.com/en/articles/100
    https://www.mql5.com/en/articles/599
    https://www.mql5.com/en/articles/232
    Ord.,Pos.,Deals:https://www.mql5.com/en/articles/211
    https://www.mql5.com/en/search#!keyword=cookbook
    and for debugging: https://www.metatrader5.com/en/metaeditor/help/development/debug
    learn to understand the error messages in the logs!!
    useful links: https://www.mql5.com/en/forum/176023

 
Carl Schreiber #:

Ok, then learn MQL5 of would you like to start learning DOS instead of Windows? (Read: https://www.mql5.com/en/forum/454319)

Here a link: https://www.mql5.com/de/forum/455957#comment_50048608

And here a bunch of links for beginners:

    Before you learn to program in MQL5, learn to search, because there is practically nothing that has not already been programmed for MT4/MT5!
    => Search in the articles: https://www.mql5.com/en/articles
    => Search in the codebase: https://www.mql5.com/en/code
    => Search in general: https://www.mql5.com/en/search or via Google with: "site:mql5.com .." (forgives typos and variants)
    If you place the cursor on an MQL function and press F1, you will see the reference directly, many with examples to copy and paste - the fastest form to code!
    https://www.mql5.com/en/articles/496
    https://www.mql5.com/en/articles/100
    https://www.mql5.com/en/articles/599
    https://www.mql5.com/en/articles/232
    Ord.,Pos.,Deals:https://www.mql5.com/en/articles/211
    https://www.mql5.com/en/search#!keyword=cookbook
    and for debugging: https://www.metatrader5.com/en/metaeditor/help/development/debug
    learn to understand the error messages in the logs!!
    useful links: https://www.mql5.com/en/forum/176023

Noted with thanks.

I've written a set of simple codes to test it in a script :-

   if(ChartApplyTemplate(ChartID(),"EA2.tpl")) {Print("The EA2.tpl applied Successfully");}
      else Print("... Failed to apply EA2.tpl, Error code ",GetLastError());

Seems it is working, but the script does not stop running, I'm facing :-

1) difficult to test second time as the script is yet in operation ...

2) difficult to shut down my MT4 terminal, eventually I have to forcefully closed the terminal.

3) I cannot run other script / EA / indicator because script is yet in operation ...

Could you please help how to add some command codes to stop the script immediately after execution of my "EA2.tpl", thanks

 

Hi

If you have some problems with closing the platform or adding some new script, you might have some endless loops somewhere in your code, so please revise your code. You can simply break the loop when you done everything you needed.

Or you can also remove the “strategy1” from this current chart - you don’t need to close the chart – just add “ExpertRemove()” function when you confirm that the other chart is opened.

If you are worried that you would have too many charts opened later (when you still would be opening new charts, you can simply apply template to existed charts – and do not open new ones.)


Have a nice weekend

 
Marzena Maria Szmit #:

Hi

If you have some problems with closing the platform or adding some new script, you might have some endless loops somewhere in your code, so please revise your code. You can simply break the loop when you done everything you needed.

Or you can also remove the “strategy1” from this current chart - you don’t need to close the chart – just add “ExpertRemove()” function when you confirm that the other chart is opened.

If you are worried that you would have too many charts opened later (when you still would be opening new charts, you can simply apply template to existed charts – and do not open new ones.)


Have a nice weekend

Thanks Marzena.

If you could provide your codes would be the best to a beginner, thanks

 

Hi

I don’t know what your conditions are but it’s exactly what you have in your algorithm, maybe something like that:

if(strategy1_signal){

        bool opened=false;

        int attempt =20 ;//try max 10 times

while(attempt>0 && !IsStopped()){

long cid = ChartOpen(Symbol(), PERIOD_D1);

         if(cid>0) 

            if(!ChartApplyTemplate(cid, “strategy2.tpl”))

               Print("Error applying template: "+IntegerToString(GetLastError()));

                else {

                        opened=true; break;

                }

        attempt--;

}

if(opened) ExppertRemove();

}

Best Regards

Reason: