Changing width scale according zoom scale

 
Hello everyone,
I desire to auto scale the width of an histogram buffer according the zoom scale's candle.

My problem is it only works when i change the time frame.
I want it to be directly after zooming in/out.

I have put the function in : OnInit + OnCalculate + OnChartEvent ... but, it doesn't work instantly.

Here is my code :
int TLR_Hist_Width()
      {
        // Zoom +/-
        long ChartScale=ChartGetInteger(0,CHART_SCALE);

        if(ChartScale==5)
            {
                TLR_NR0_Width=12;
            }
        if(ChartScale==4)
            {
                TLR_NR0_Width=6;
            }
        if(ChartScale==3)
            {
                TLR_NR0_Width=4;
            }
        if(ChartScale==2)
            {
                TLR_NR0_Width=2;
            }
        if(ChartScale==1)
            {
                TLR_NR0_Width=1;
            }
        if(ChartScale==0)
            {
                TLR_NR0_Width=0;
            }
        return(0);
      }
    //+---------------------------------------------------------------+
Please help.
Regards.
 
Thierry Ramaniraka:
Hello everyone,
I desire to auto scale the width of an histogram buffer according the zoom scale's candle.

My problem is it only works when i change the time frame.
I want it to be directly after zooming in/out.

I have put the function in : OnInit + OnCalculate + OnChartEvent ... but, it doesn't work instantly.

Here is my code :
Please help.
Regards.

You'd be better off using a switch expression

int TLR_Hist_Width()
{
   switch((int)ChartGetInteger(0, CHART_SCALE)){
      case 5: return 12;
      case 4: return 6;
      case 3: return 4;
      case 2: return 2;
   }
   return 1;
}

Regarding your main question, you haven't provided enough code/context for help. 

 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexStyle(index,EMPTY,EMPTY,TLR_Hist_Width());
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_CHART_CHANGE)
     SetIndexStyle(index,EMPTY,EMPTY,TLR_Hist_Width());
  }
//+------------------------------------------------------------------+
 
nicholi shen:

You'd be better off using a switch expression

Regarding your main question, you haven't provided enough code/context for help. 

Ernst Van Der Merwe:

Thank you very much both of you.
It works perfectly.

Kind regards.

 
Rob Hayne #: I have the same request. How do I place the correct code into this indicator so that as I zoom, the histogram width changes?
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. int width=0;
    int OnInit(){
       width = (int)   ChartGetInteger(0,CHART_SCALE);
    //--- indicator buffers mapping
       SetIndexBuffer(0,…);     SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY, width);
       ⋮
    }
    int OnCalculate(…){
       if(ChartGetInteger(0,CHART_SCALE) != width)  OnInit();
       ⋮
Reason: