Horizontal scale

 
i want to use mouse scroll wheels for horizontal scale of chart like tradingview website, is it possible to do?
 

try this indicator . It is jaggy though because once the user clicks you must enable the mouse control and when they release the mouse you must take mouse control back.

#property version   "1.00"
#property indicator_chart_window
int scale=0;
string previous_button="0";
bool AllowZoom=true;
int OnInit()
  {
  AllowZoom=true;
  scale=(int)ChartGetInteger(ChartID(),CHART_SCALE);
  ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_WHEEL,true);
  ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,false);
  ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_MOVE,true);
  previous_button="0";
  return(INIT_SUCCEEDED);
  }

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  if(id==CHARTEVENT_MOUSE_WHEEL&&AllowZoom)
    {
    //wheel up
      if(dparam>0){
      scale++;
      if(scale>5){scale=5;}
      ChartSetInteger(ChartID(),CHART_SCALE,scale);
      ChartRedraw(ChartID());
      }
    //wheel down
      if(dparam<0){
      scale--;
      if(scale<0){scale=0;}
      ChartSetInteger(ChartID(),CHART_SCALE,scale);
      ChartRedraw(ChartID());
      }
    } 
  if(id==CHARTEVENT_MOUSE_MOVE){
  if(sparam=="1"&&previous_button=="0"){
    //block zoom
      AllowZoom=false;
      ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,true);
    }
  if(sparam=="0"&&previous_button=="1"){
    //unblock zoom
      AllowZoom=true;
      ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,false);
    }
  previous_button=sparam;
  }
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots 0
int scale=0;
string previous_button="0";
bool AllowZoom=true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//--- indicator buffers mapping
   AllowZoom=true;
   scale=(int)ChartGetInteger(ChartID(),CHART_SCALE);
   ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_WHEEL,true);
   ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,false);
   ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_MOVE,true);
   previous_button="0";
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
}

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam) {
//---
   if(id==CHARTEVENT_MOUSE_WHEEL&&AllowZoom) {
      //wheel up
      if(dparam>0) {
         scale++;
         if(scale>5) {
            scale=5;
         }
         ChartSetInteger(ChartID(),CHART_SCALE,scale);
         ChartRedraw(ChartID());
         ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,true);
      }
      //wheel down
      if(dparam<0) {
         scale--;
         if(scale<0) {
            scale=0;
         }
         ChartSetInteger(ChartID(),CHART_SCALE,scale);
         ChartRedraw(ChartID());
         ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,true);
      }
   }
   //if(id==CHARTEVENT_MOUSE_MOVE) {
   //   if(sparam=="1"&&previous_button=="0") {
   //      //block zoom
   //      AllowZoom=false;
   //      ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,true);
   //   }
   //   if(sparam=="0"&&previous_button=="1") {
   //      //unblock zoom
   //      AllowZoom=true;
   //      ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,false);
   //   }
   //   previous_button=sparam;
   //}
}
//+------------------------------------------------------------------+
Lorentzos Roussos #
:

try this indicator . It is jaggy though because once the user clicks you must enable the mouse control and when they release the mouse you must take mouse control back.

awesome, I tried to fix it so its moving chart now back and forth

 
Arpit T #:

awesome, I tried to fix it so its moving chart now back and forth

Hmm that won't work no .

The alternative is to shut off the mouse control completely and create a custom one , and let the mouse scroll zoom

I'll give it a shot , sounds fun 
 
Lorentzos Roussos #:

Hmm that won't work no .

The alternative is to shut off the mouse control completely and create a custom one , and let the mouse scroll zoom

I'll give it a shot , sounds fun 

i have tested it works for me smoothly

 
Arpit T #:

i have tested it works for me smoothly

Well if you don't mind it moving then its okay :)

(i'm attaching the custom version if you want to compare)

#property version   "1.00"
#property indicator_chart_window
int scale=0;
string previous_button="0";
bool AllowZoom=true,Dragging=true;
double positionPrice=0.0,pixelsPerBar=0;
datetime positionTime=0;
int positionWindow=0;
int positionX=0;

int OnInit()
  {
  AllowZoom=true;
  scale=(int)ChartGetInteger(ChartID(),CHART_SCALE);
  ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_WHEEL,true);
  ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,false);
  ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_MOVE,true);
  previous_button="0";
  Dragging=false;
  return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  if(id==CHARTEVENT_MOUSE_WHEEL&&AllowZoom)
    {
    //wheel up
      if(dparam>0){
      scale++;
      if(scale>5){scale=5;}
      ChartSetInteger(ChartID(),CHART_SCALE,scale);
      ChartRedraw(ChartID());
      }
    //wheel down
      if(dparam<0){
      scale--;
      if(scale<0){scale=0;}
      ChartSetInteger(ChartID(),CHART_SCALE,scale);
      ChartRedraw(ChartID());
      }
    } 
  if(id==CHARTEVENT_MOUSE_MOVE){
      int x=(int)lparam;
      int y=(int)dparam;
  if(sparam=="1"&&previous_button=="0"){
    //block zoom
      AllowZoom=false;
      //start drag
      Dragging=true;
      int width=(int)ChartGetInteger(ChartID(),CHART_WIDTH_IN_PIXELS,0);
      int wbars=(int)ChartGetInteger(ChartID(),CHART_WIDTH_IN_BARS,0);
      pixelsPerBar=((double)width)/((double)wbars);
      positionX=x;
      ChartXYToTimePrice(ChartID(),x,y,positionWindow,positionTime,positionPrice);
    }
  else if(sparam=="0"&&previous_button=="1"&&Dragging){
    //unblock zoom
      Dragging=false;
      AllowZoom=true;
    }
  else{
  if(Dragging){
  int xoffset=positionX-x;
  int barstonavigate=(int)MathFloor(((double)xoffset)/pixelsPerBar);
  if(barstonavigate!=0){
    ChartNavigate(ChartID(),CHART_CURRENT_POS,barstonavigate);
    positionX=x;
    ChartRedraw();
    }
  }
  }
  previous_button=sparam;
  }

  }
//+------------------------------------------------------------------+
 
Lorentzos Roussos #:

Well if you don't mind it moving then its okay :)

(i'm attaching the custom version if you want to compare)

much better! Thanks 

but now its not possible to place and set fibonacci retracement on chart
 
Arpit T #:

much better! Thanks 

but now its not possible to place and set fibonacci retracement on chart

lol xD xD , right .

attempt 3 

#property version   "1.00"
#property indicator_chart_window
int scale=0;
string previous_button="0";
bool AllowZoom=true,Dragging=true,AllowDrag=true;
double positionPrice=0.0,pixelsPerBar=0;
datetime positionTime=0;
int positionWindow=0;
int positionX=0;

int OnInit()
  {
  AllowZoom=true;AllowDrag=true;
  scale=(int)ChartGetInteger(ChartID(),CHART_SCALE);
  ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_WHEEL,true);
  ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,false);
  ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_MOVE,true);
  ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
  previous_button="0";
  Dragging=false;
  return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  if(id==CHARTEVENT_OBJECT_CREATE)
    {AllowDrag=false;}
  if(id==CHARTEVENT_MOUSE_WHEEL&&AllowZoom)
    {
    //wheel up
      if(dparam>0){
      scale++;
      if(scale>5){scale=5;}
      ChartSetInteger(ChartID(),CHART_SCALE,scale);
      ChartRedraw(ChartID());
      }
    //wheel down
      if(dparam<0){
      scale--;
      if(scale<0){scale=0;}
      ChartSetInteger(ChartID(),CHART_SCALE,scale);
      ChartRedraw(ChartID());
      }
    } 
  if(id==CHARTEVENT_MOUSE_MOVE){
      int x=(int)lparam;
      int y=(int)dparam;
  if(sparam=="1"&&previous_button=="0"&&AllowDrag){
    //block zoom
      AllowZoom=false;
      //start drag
      Dragging=true;
      int width=(int)ChartGetInteger(ChartID(),CHART_WIDTH_IN_PIXELS,0);
      int wbars=(int)ChartGetInteger(ChartID(),CHART_WIDTH_IN_BARS,0);
      pixelsPerBar=((double)width)/((double)wbars);
      positionX=x;
      ChartXYToTimePrice(ChartID(),x,y,positionWindow,positionTime,positionPrice);
    }
  else if(sparam=="0"&&previous_button=="1"&&Dragging){
    //unblock zoom
      Dragging=false;
      AllowZoom=true;
    }
  else if(sparam=="0"&&!AllowDrag){AllowDrag=true;}
  else{
  if(Dragging){
  int xoffset=positionX-x;
  int barstonavigate=(int)MathFloor(((double)xoffset)/pixelsPerBar);
  if(barstonavigate!=0){
    ChartNavigate(ChartID(),CHART_CURRENT_POS,barstonavigate);
    positionX=x;
    ChartRedraw();
    }
  }
  }
  previous_button=sparam;
  }

  }
//+------------------------------------------------------------------+
 
Lorentzos Roussos #:

lol xD xD , right .

attempt 3 

objects are created now, but they are not draggable, 

Files:
 
Arpit T #:

objects are created now, but they are not draggable, 

Hmm , interesting rabbit hole xD . I don't think we can detect this though , i will check.

edit : No we can't unfortunately . Was fun though xD . Only mq can add this mode now.
 
Lorentzos Roussos #:

Hmm , interesting rabbit hole xD . I don't think we can detect this though , i will check.

edit : No we can't unfortunately . Was fun though xD . Only mq can add this mode now.

ok, thanks for your time on helping on this, Best :)