Problem: chart won't scroll to the right after changing chart scale using custom indicator

torboxz
12
torboxz  

Hi,

I need some help. This problem is purely cosmetic and me being lazy (sorry).

When I'm looking the chart, I want to zoomed in the H4, D1 and W1 timeframe.

I can do that using

ChartSetInteger(ChartID(),CHART_SCALE,5);

When I'm at H4, I want it to show the line chart using

ChartSetInteger(ChartID(),CHART_MODE,2);

Lastly, when I'm looking at H1 and below, I want to zoom out to the third scale

ChartSetInteger(0,CHART_SCALE,2);

I put all the code in the OnInit() section so that every time I change the timeframe, the chart change the way I wanted.

It is all good when going up the timeframe (M1 to MN1) but the problem appears when going down the timeframe.

On higher timeframe, there are only a few candle bars on display because of CHART_SCALE,5. When I change to the lower time frame, the scale changes but the chart keeps the previous bar counts so the chart is shifted the left which I don't want.

Even though the problem corrected itself after receiving a tick (because of chart autoscroll is on), waiting for a tick to come takes too long.

Disabling and reenabling chart shift or autoscroll (in the code) does not help (or I'm not using it the right way?). WindowBarsPerChart() and CHART_VISIBLE_BARS is read only. WindowRedraw() is doing nothing too.

Any idea or solution is greatly appreciated. Thanks.

Here is the code that I am using

int OnInit()
  {
   if(ChartPeriod()==PERIOD_MN1 ||ChartPeriod()==PERIOD_W1 ||ChartPeriod()==PERIOD_D1)
     {
      ChartSetInteger(0,CHART_SCALE,5);
      ChartSetInteger(ChartID(),CHART_MODE,1);//to be safe
     }
   else if(ChartPeriod()==PERIOD_H4)
     {
      ChartSetInteger(0,CHART_SCALE,5);
      ChartSetInteger(ChartID(),CHART_MODE,2);
     }
   else if(ChartPeriod()==PERIOD_H1 ||ChartPeriod()==PERIOD_M30 ||ChartPeriod()==PERIOD_M15 ||ChartPeriod()==PERIOD_M5 ||ChartPeriod()==PERIOD_M1)
     {
      ChartSetInteger(0,CHART_SCALE,2);
      ChartSetInteger(ChartID(),CHART_MODE,1);
     }
//---
   return(INIT_SUCCEEDED);
  }
Keith Watford
Moderator
28986
Keith Watford  
torboxz
12
torboxz  
Keith Watford:
Use ChartNavigate()
Thanks! It works!