With mouse clicks in MultiCharts we can retrieve the price and date and time of where the click took place. But how do we retrieve the data series area that was clicked on?

In this article:

Handling mouse clicks on MultiCharts’ data series

Mouse clicks on a chart can be processed in PowerLanguage indicators and strategies by adding the ProcessMouseEvents attribute (MultiCharts Wiki, 2012a). After a click, the MouseClickDataNumber keyword holds the data series number of the chart area that was clicked on (MultiCharts Wiki, 2012b), starting with 1 for the first data series.

When the script is calculated due to other reasons than a mouse click, MouseClickDataNumber returns 4,294,967,295 (coincidentally the maximum value of an unsigned long variable; Liberty & Cadenhead, 2011). For example:


[ProcessMouseEvents = true];

if (LastBarOnChart_s) then begin

    if (MouseClickCtrlPressed) then 
        Print("MouseClickDataNumber with click: ",
            MouseClickDataNumber)
    else 
        Print("MouseClickDataNumber without click: ",
            MouseClickDataNumber);

end;

//> MouseClickDataNumber with click:    1.00
//> MouseClickDataNumber with click:    2.00
//> MouseClickDataNumber without click: 4294967295.00

Due to these different MouseClickDataNumber values, it’s important to know if a script is calculated by a mouse click or not.

Processing mouse clicks for on data series’ charts in MultiCharts

So, with MouseClickDataNumber we can determine which data series was clicked on:


[ProcessMouseEvents = true];

if (MouseClickDataNumber = 1) then 
    Print("A mouse click on the first data series.")
else if (MouseClickDataNumber = 2) then
    Print("A mouse click on the second data series.");

if (MouseClickDataNumber = 3) then begin

    if (MouseClickCtrlPressed = true) then
        Print("A Ctrl + click on data 3.")
    else 
        Print("A click on data 3 without the Ctrl key.");

end;

//> A mouse click on the first data series.
//> A mouse click on the the second data series.
//> A click on data 3 without the Ctrl key.
//> A Ctrl + click on data 3.

We first add the ProcessMouseEvents attribute and set it to true. Then a cascaded if statement uses MouseClickDataNumber to determine whether a click happened on the first or second data series. For both cases this information is outputted with Print().

The second part first uses an if statement to check whether MouseClickDataNumber equals 3. When that is true, we know a click occurred on the third data series.

A nested if-else statement subsequently uses the MouseClickCtrlPressed keyword to check if the Control key was pressed down during the click (MultiCharts Wiki, 2012c). This information is then printed to the Output Window with Print(). For more on the Control and Shift keys, see combining keyboard keys with mouse clicks.

Using mouse click data to access a chart’s data series

With the Data() keyword data series can be accessed by specifying the data number (MultiCharts Wiki, 2012d). Since MouseClickDataNumber returns the data series number, both can be combined to access data series through mouse clicks. So, with a chart like:

Multiple data series on a MultiCharts chart

These three data series can be accessed as follows:


[ProcessMouseEvents = true];

if (MouseClickCtrlPressed and MouseClickShiftPressed) then begin

    Print("Click on data ", NumToStr(MouseClickDataNumber, 0),
        ", symbol: ", Symbol Data(MouseClickDataNumber),
        ", close: ", NumToStr(Close Data(MouseClickDataNumber), 5));

end;

//> Click on data 1, symbol: EUR/USD, close: 1.21775
//> Click on data 2, symbol: GBP/USD, close: 1.55458
//> Click on data 3, symbol: EUR/CAD, close: 1.41612

This if statement evaluates whether both the Control key (MouseClickCtrlPressed) and Shift key (MouseClickShiftPressed) were pressed down during the click on the chart.

When both keywords return true, Print() outputs three details. First is the data series on which the click took place (MouseClickDataNumber), formatted with NumToStr() to display zero decimals. Second is the data series symbol by combining the Symbol keyword with the data series that was clicked on (Data(MouseClickDataNumber)). The third and last part retrieves the current closing price of the data series where the click happened (Close Data(MouseClickDataNumber)), formatted to 5 decimals with NumToStr().

Other applications of the clicked-on data series number include creating a text box with mouse click data.

Summary

Mouse clicks are programmatically processed after setting the ProcessMouseEvents attribute to true. Then on each click MouseClickDataNumber returns the data series number of the chart area that was clicked on (starting with 1 for the first data series).


References

Liberty, J. & Cadenhead, R. (2011). Sams Teach Yourself C++ in 24 Hours. IN (USA): Sams/Pearson Education.

MultiCharts Wiki (2012a, February 24). ProcessMouseEvents. Retrieved on December 27, 2014, from http://www.multicharts.com/trading-software/index.php/ProcessMouseEvents

MultiCharts Wiki (2012b, February 24). MouseClickDataNumber. Retrieved on December 27, 2014, from http://www.multicharts.com/trading-software/index.php/MouseClickDataNumber

MultiCharts Wiki (2012c, February 24). MouseClickCtrlPressed. Retrieved on December 27, 2014, from http://www.multicharts.com/trading-software/index.php/MouseClickCtrlPressed

MultiCharts Wiki (2012d, February 19). Data. Retrieved on December 27, 2014, from http://www.multicharts.com/trading-software/index.php/Data