With a click on a MultiCharts price chart we can retrieve the click’s bar number, which can be used to access that bar’s price data. But how can the price value of a mouse click be retrieved?

In this article:

Working with price click information in MultiCharts

Mouse clicks on a chart’s data series are processed programmatically when the ProcessMouseEvents attribute is enabled (MultiCharts Wiki, 2012a). After a click the MouseClickPrice keyword returns the click’s price level (MultiCharts Wiki, 2012b).

When the script is calculated without a mouse click (like when a new price update occurs), MouseClickPrice returns 0. So, you likely want to only reference this keyword when a script is calculated by a mouse click.

MouseClickPrice returns the y-axis price level of any data series’ chart area that was clicked on, whether this being the first, second, or other data series. When working with multiple data series, the MouseClickDataNumber returns the clicked-on data series (MultiCharts Wiki, 2012c).

Accessing the price of a click location in MultiCharts

An basic example of MouseClickPrice is:


[ProcessMouseEvents = true];

if (MouseClickCtrlPressed = true) then begin

    Print("Bar #: ", NumToStr(MouseClickBarNumber, 0),
        ", price: ", NumToStr(MouseClickPrice, 2));

end;

//> Bar #: 220, price: 2087.45
//> Bar #: 223, price: 2085.89
//> Bar #: 251, price: 2084.18
//> Bar #: 238, price: 2083.79

The ProcessMouseEvents attribute is set to true here to enable mouse click processing. Then an if statement checks whether a mouse click happened on the price chart while the Control keyboard key was pressed down. In that case, MouseClickCtrlPressed equals true and the code inside the if statement is executed.

That code block consists out of a Print() statement that outputs the clicked-on bar number (MouseClickBarNumber) formatted with NumToStr() to display zero decimals and the clicked-on price (MouseClickPrice) formatted with two decimals.

Calculating the price difference between two chart points

MouseClickPrice values can be stored in a variable and used for other applications, like calculating the absolute price difference between two points:


[ProcessMouseEvents = true];

Variables:
    IntraBarPersist prevClickPrice(0),
    priceDiffAbs(0);

if (MouseClickCtrlPressed) then begin

    if (prevClickPrice = 0) then
        Print("There's no previous price yet.")

    else begin

        priceDiffAbs = AbsValue(MouseClickPrice - prevClickPrice);

        Print("Click: ", NumToStr(MouseClickPrice, 2),
            ". Previous: ", NumToStr(prevClickPrice, 2),
            ". Difference: ", NumToStr(priceDiffAbs, 2));

    end;

    prevClickPrice = MouseClickPrice;    
end;

//> There's no previous price yet.
//> Click: 2084.54. Previous: 2085.32. Difference: 0.78
//> Click: 2082.87. Previous: 2084.54. Difference: 1.67
//> Click: 2088.76. Previous: 2082.87. Difference: 5.89
//> Click: 2087.88. Previous: 2088.76. Difference: 0.89
//> Click: 2087.77. Previous: 2087.88. Difference: 0.11

Two variables are used here. prevClickPrice stores the previous price that was clicked on. Since this variable is updated with each script calculation (and not just on bar close), it’s marked IntraBarPersist to retain its value; otherwise, the variable will only retain its value assigned on bar close (MultiCharts Wiki, 2012d). The other variable, priceDiffAbs, will be used to hold the absolute price difference between two points.

Then an if statement evaluates whether the Control key was pressed down during the mouse click (MouseClickCtrlPressed). When it was, the code inside the if statement is executed. This begins with an if-else statement that evaluates prevClickPrice. When this variable is zero, we know the current click is the first and so a price difference cannot yet be calculated.

When prevClickPrice isn’t zero (the else portion), the difference between the current mouse click (MouseClickPrice) and the previous (prevClickPrice) is determined and assigned to the priceDiffAbs variable. This subtraction is performed in the AbsValue() keyword to get the absolute value (see MultiCharts Wiki, 2012e).

Mouse click information is then outputted with Print() to display the current price click, the previous price click value, and the absolute value. Each is displayed with two decimals with NumToStr().

The last statement inside the topmost if statement assigns the current mouse click price to the prevClickPrice variable. That way this variable holds the previous mouse click price in the script’s next calculation.

Other uses of MouseClickPrice include submitting market orders with mouse clicks and freely draw trend lines with mouse clicks. For working with a chart’s x-axis’ values, see capturing the date and time of a mouse click.

Summary

Mouse clicks on a price chart can be processed programmatically when the ProcessMouseEvents attribute set to true. After a mouse click, MouseClickPrice returns the price level at which the click occurred.


References

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). MouseClickPrice. Retrieved on December 28, 2014, from http://www.multicharts.com/trading-software/index.php/MouseClickPrice

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

MultiCharts Wiki (2012d, August 11). IntraBarPersist. Retrieved on January 23, 2015, from http://www.multicharts.com/trading-software/index.php/IntraBarPersist

MultiCharts Wiki (2012e, February 9). AbsValue. Retrieved on December 28, 2014, from http://www.multicharts.com/trading-software/index.php/AbsValue