how to make a chart current?

 

Hi, Im new to MQL4 but not to coding. In my EA I'm trying to make a specific chart the current chart. When I activate the trading terminal it starts with a particular chart - EURUSD. This is not the one I want so I open a new chart  (GOLD.) . At this stage _Symbol is still not  set to GOLD. I then do a SymbolSelect, ChartSetInterger  and still _Symbol is not set to GOLD. My issue is when I execute "Comment()" it writes the information on the EURUSD chart. Currently I am testing code in OnTick()  and  find any function I execute using _Symbol is, of course, looking at the wrong information.

Would appreciate some help as I've not been able to discover how to resolve this.

Cheers,

Paul

 

Slightly unclear what you're attempting; as with coding, precise wording is important in discussing a problem..;)

You are running an EA on a current chart, EURUSD. Are you opening a new chart with a different symbol, or changing the symbol of the current chart?

If you're just changing the symbol, don't see why this wouldn't work:

ChartSetSymbolPeriod(0,"GOLD",0);

And check your "Market watch" box to ensure the symbol is correctly spelt and is available.

 
pconrad: When I activate the trading terminal it starts with a particular chart - EURUSD.

No, it starts with whatever was present when previously shut down.

Do not open another chart, change the symbol of that chart (enter → gold). Done.

 

Thanks for the feedback. To clarify the above situation. The only chart / symbol I'm working with (want)  is "Gold." I'm writing an EA and having trouble doing operations on the Chart with symbol Gold. Also, I'm doing this from the debug side of MetaEditor.

When I launch the EA the following happens;

1) the trading terminal appears.

2) a chart with symbol "EURUSD." appears. Every time I start the execution of the program - I get it.  

If I  manually delete all charts (using the Trading Terminal), then create a Gold chart manually , run the  EA from the debugger a new chart - EURUSD appears on top.

3) I step through this code in the debugger;

chartID= ChartID(); // _Symbol is EURUSD
symbol1=Symbol(); // _Symbol is still EURUSD
newsym=ChartSetSymbolPeriod(0,"GOLD.",0);// takes a few seconds before coming true
symbol1=Symbol(); // _Symbol is still EURUSD!!

I'm trying  to make the GOLD chart / symbol the current one. How to  get _Symbol to read "GOLD.". Can't seem to discover how? 

Help appreciated.

Cheers,

Paul

Improperly formatted code corrected by moderator. Please always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
pconrad #:

Thanks for the feedback. To clarify the above situation. The only chart / symbol I'm working with (want)  is "Gold." I'm writing an EA and having trouble doing operations on the Chart with symbol Gold. Also, I'm doing this from the debug side of MetaEditor.

When I launch the EA the following happens;

1) the trading terminal appears.

2) a chart with symbol "EURUSD." appears. Every time I start the execution of the program - I get it.  

If I  manually delete all charts (using the Trading Terminal), then create a Gold chart manually , run the  EA from the debugger a new chart - EURUSD appears on top.

3) I step through this code in the debugger;

I'm trying  to make the GOLD chart / symbol the current one. How to  get _Symbol to read "GOLD.". Can't seem to discover how? 

Help appreciated.

Cheers,

Paul

Improperly formatted code corrected by moderator. Please always use the CODE button (Alt-S) when inserting code.

Is your symbol "GOLD. " ? With the full stop and space...

newsym=ChartSetSymbolPeriod(0,"GOLD.",0)

You could also check to see if a terminal profile opens with EURUSD .
 

Symbol has period and no space. "GOLD."

code executed via debugger...

  chartID= ChartID();// _Symbol is EURUSD
  chartID1= ChartOpen("GOLD.",PERIOD_CURRENT); // new chart ID, chart and gold symbol created
  if (!ChartSetInteger(chartID1,CHART_BRING_TO_TOP,0,true)) Comment(__FUNCTION__+", Error Code = ",GetLastError()); //_Symbol is still EURUSD 
  symbol1=Symbol(); // _Symbol is still EURUSD
  newsym=ChartSetSymbolPeriod(chartID1,"GOLD.",0);// takes a few seconds before coming true
  chartID= ChartID();// _Symbol is EURUSD
  symbol1=Symbol(); // _Symbol is still EURUSD
 
pconrad #:

Symbol has period and no space. "GOLD."

code executed via debugger...

You could try setting the options in the code editor so the chart opens in "GOLD."  (in my case it's GOLD.p)

Also, with your ChartOpen(..), it just opens a new chart with no EA attached so the comment will reflect the parent chart not the new chart.

 

Thank you Andrew4789,

I tried multiple functions (ChartOpen(), ChartSetInteger(), ChartSetSymbolPeriod(),SymbolName()) trying to get _Symbol set to "Gold." - nothing worked! Frustrating!!

Your solution worked fine.

Cheers,

Paul

Reason: