ChartSymbol() not working as expected

 

Hello

I have a function in my EA:


void SetAllChartsToPeriod(int _period) {
        long currChart,prevChart=ChartFirst();
        int lim = 30;
        int i=0;
        while(i<lim && prevChart != -1) {
                currChart=ChartNext(prevChart); // Get the new chart ID by using the previous chart ID
                if(currChart<0) break;
                if(StringLen(ChartSymbol(currChart)) != 6) {
                        Print("ERROR: Chart ID |",currChart,"| is found, but ChartSymbol not returning valid symbol");
                        break;          // Have reached the end of the chart list
                }
                Print(i,": SYMBOL[",ChartSymbol(currChart),"] ID =",currChart);
                if(! ChartSetSymbolPeriod(currChart,ChartSymbol(currChart),_period)) {
                        Print(GetLastError()," - ERROR IN SETTING Nth CHART SYMBOL PERIOD AFTER SECOND TRY!");
                } else {
                        Print("ok changed chart ",currChart," to ",_period,", sym", ChartSymbol(currChart),".");
                }
                prevChart=currChart;// let's save the current chart ID for the ChartNext()
                i++;// Do not forget to increase the counter
        }

        //
        // now do the first chart last
        //
        string fchtsym = ChartSymbol(ChartFirst());
        Print("FIRST_CH: SYMBOL[",ChartSymbol(currChart),"] ID =",currChart);
        if(! ChartSetSymbolPeriod(ChartFirst(),fchtsym,_period)) {
                Print(GetLastError(),"ERROR IN SETTING FIRST CHART SYMBOL PERIOD AFTER SECOND TRY!------------------------");
        }
}

I have several buttons on my charts to change the period for all charts.  I click a button, and the whole lot switch, but when I click another button, ChartSymbol() appears to return a null symbol like the chart ID is now not valid, but the print statement appears to correctly list the chart ID in the print statement, and I notice the charts IDs do not change from one invocation of a chart set to the next.

The function returns the error

"ERROR: Chart ID |",currChart,"| is found, but ChartSymbol not returning valid symbol"

when one of the buttons (i.e. a function call with the _period set to one of the PERIOD_xx enumerated period integers) is clicked for the second time.

I think this is a bug as I had these buttons working for months, and then suddenly they stopped working.

Big thanks in advance, particularly for anyone who can recreate this problem.

 

Changing period means you close the current chart and open a new one with the new period. So of course you get a bad symbol, because you now have a bad ID. In addition you try to use ChartNext with the invalid ID.

Get the current chart ID. Get the next chart ID. Change the current chart. Do not look at the current chart/current ID.

 

Thanks WHRoeder1.

In the end I implemented the solution at the top of this thread: https://www.mql5.com/en/forum/140692

Change Timeframe on chart fuction or script
Change Timeframe on chart fuction or script
  • www.mql5.com
Hi is there a way for change Timeframe only on select chart? I found this script but it changes Timeframe on all charts...
Reason: