How to get Price data and Time data of mouse position by clicking the mouse?

 

We can use "CHARTEVENT_CLICK" to get X coordinate value and Y coordinate value of mouse position , But How to get Price data and Time data of mouse position by clicking the mouse?

It we move the mouse , we can see Price data and Time data by using "Crosshair,Ctrl+F" , But How to output the data to the variable ?

Is anyone who knows how to achieve this function?

Thank you. 

 
Yu Zhang:

We can use "CHARTEVENT_CLICK" to get X coordinate value and Y coordinate value of mouse position , But How to get Price data and Time data of mouse position by clicking the mouse?

It we move the mouse , we can see Price data and Time data by using "Crosshair,Ctrl+F" , But How to output the data to the variable ?

Is anyone who knows how to achieve this function?

Thank you. 

ChartXYToTimePrice

Converts the X and Y coordinates on a chart to the time and price values.

bool  ChartXYToTimePrice( 
   long           chart_id,     // Chart ID 
   int            x,            // The X coordinate on the chart 
   int            y,            // The Y coordinate on the chart 
   int&           sub_window,   // The number of the subwindow 
   datetime&      time,         // Time on the chart 
   double&        price         // Price on the chart 
   );
 
//+------------------------------------------------------------------+ 
//| ChartEvent function                                              | 
//+------------------------------------------------------------------+ 
void OnChartEvent(const int id,         // Event identifier   
                  const long& lparam,   // Event parameter of long type 
                  const double& dparam, // Event parameter of double type 
                  const string& sparam) // Event parameter of string type 
  { 
//--- the left mouse button has been pressed on the chart 
   if(id==CHARTEVENT_CLICK) 
     { 
      //Print("The coordinates of the mouse click on the chart are: x = ",lparam,"  y = ",dparam);
      datetime time;
      double price;
      int subwindow;
      ChartXYToTimePrice(0,lparam,dparam,subwindow,time,price);
      Print(" Subwindow: ",IntegerToString(subwindow)," Time: ",TimeToString(time,TIME_SECONDS)," Price: ",DoubleToString(price));
        
     } 
//--- the mouse has been clicked on the graphic object 
   if(id==CHARTEVENT_OBJECT_CLICK) 
     { 
      //Print("The mouse has been clicked on the object with name '"+sparam+"'"); 
     } 
  }
 
Marco vd Heijden:
Thank you very much.
 

Hello,

I need to achieve the same thing. Following Marco's code suggestion, what is the "price" variable for a candlestick chart? In other words, if I click on a candle, I'd like to get the components (OHLC) of the candle. I tried  iBarShift with the time returned by  ChartXYToTimePrice. But it doesn't work: the index returned changes according to the click location in the candle or more importantly, is different different when the chart is zoomed.

Regards,
Mark

 
Mark531:

Hello,

I need to achieve the same thing. Following Marco's code suggestion, what is the "price" variable for a candlestick chart? In other words, if I click on a candle, I'd like to get the components (OHLC) of the candle. I tried  iBarShift with the time returned by  ChartXYToTimePrice. But it doesn't work: the index returned changes according to the click location in the candle or more importantly, is different different when the chart is zoomed.

Regards,
Mark

By default, candles change time at the wick, so clicking on the left body or right body gives you different iBarShift() result.

So try adding half a candle's time width when you call iBarShift(), like this:

      int bar = iBarShift(Symbol(),Period(),time+PeriodSeconds()/2,false);
 

It worked like a charm! Thank you!

Now, I'd like to disregard clicks used to scroll the graph by using CHARTEVENT_MOUSE_MOVE.

 
Seng Joo Thio:

By default, candles change time at the wick, so clicking on the left body or right body gives you different iBarShift() result.

So try adding half a candle's time width when you call iBarShift(), like this:

Thank You for this perfect solution.

Reason: