How to switch between the charts programmatically?

 

I need to develop an EA that will process several charts visible in the MT4 screen, like EURUSD-H1, USDCAD-D1, etc.

I can switch to each of these charts with such code as:

void Charts() {long curr,prev=ChartFirst();int i=0;

while(i<100) {Print(i,ChartSymbol(prev)," ID =",prev);Print("ChartSymbol=",ChartSymbol(prev));curr=ChartNext(prev); if(curr<0) break;prev=curr;i++;}}

So the names of the Charts are printed in the Experts window of MT4.... but it looks these names are simply seen in the screen. And I cannot transtition into these Charts by Navigate, or other similar commands...


For example, I can switch into the first Chart with the following function:

bool Chart1Goto() {long id=ChartFirst();return ChartSetInteger(id,CHART_BRING_TO_TOP,true);ChartRedraw();} 


But in order to go to the next Charts, when I use the following code..... it only opens the new instances of the same Chart windows:

ChartNext() - does not help as well...


long ChartGoto(string sym,int per) {long ret=0;int Fw=WindowHandle(sym,per);Print("HANDLE=",Fw);if(Fw!=0)ret=ChartOpen(sym,per); return ret;} //open the same chart as a second one...


Any idea? Maybe Lorentzos again?))


Thanks in advance.

Regards


Documentation on MQL5: Chart Operations / ChartFirst
Documentation on MQL5: Chart Operations / ChartFirst
  • www.mql5.com
ChartFirst - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

You can use this command 

ChartSetSymbolPeriod - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Now your biggest issue is knowing where you have been .

So :

  • when your ea is attached first you are looking for a "file" . If it exists you continue scanning if it does not exist you start scanning.
  • The file does not exist : you do the scan of the market watch as you already have and store each symbol in an array
  • Save the file with the number of total symbols on top , the current index second and , the list of symbols from the array next
  • Navigate to the first symbol and anticipate that you may be already in a symbol from the list 
  • Then the ea reloads finds that a file exists reads it , advances the index (second write) and moves on to the next symbol
  • When you reach the last symbol you delete the file and you probably leave a file to indicate you have scanned these symbols 
It is also important to not do anything relating to reading prices in OnInit , use timer instead
Documentation on MQL5: Chart Operations / ChartSetSymbolPeriod
Documentation on MQL5: Chart Operations / ChartSetSymbolPeriod
  • www.mql5.com
ChartSetSymbolPeriod - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Lorentzos Roussos #:

You can use this command 

ChartSetSymbolPeriod - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Now your biggest issue is knowing where you have been .

So :

  • when your ea is attached first you are looking for a "file" . If it exists you continue scanning if it does not exist you start scanning.
  • The file does not exist : you do the scan of the market watch as you already have and store each symbol in an array
  • Save the file with the number of total symbols on top , the current index second and , the list of symbols from the array next
  • Navigate to the first symbol and anticipate that you may be already in a symbol from the list 
  • Then the ea reloads finds that a file exists reads it , advances the index (second write) and moves on to the next symbol
  • When you reach the last symbol you delete the file and you probably leave a file to indicate you have scanned these symbols 
It is also important to not do anything relating to reading prices in OnInit , use timer instead

thanks a lot, it helps a lot. Now it is clear. Regards