Besides the PowerLanguage keywords for Shift and Control, for things like accessing a bar’s price data it’s also helpful to know the clicked-on bar number. So how can we retrieve that programmatically?

In this article:

Dealing with MultiCharts’ mouse clicks programmatically

Mouse clicks on a chart are processed by PowerLanguage when the ProcessMouseEvents attribute is enabled (MultiCharts Wiki, 2012a).

After a click in a data series’ chart area, the MouseClickBarNumber keyword returns the bar number (starting with 1 for the first bar) that was clicked on (MultiCharts Wiki, 2012b). To have MouseClickBarNumber return the bar’s number, a click doesn’t need to be on a price bar but can also be above or below it. When a script is not calculated due to a mouse click, MouseClickBarNumber returns -1.

Outputting the bar number that was clicked on in MultiCharts

A basic example of MouseClickBarNumber is:


[ProcessMouseEvents = true];

if (MouseClickCtrlPressed and not MouseClickShiftPressed) then begin

    Print(NumToStr(CurrentBar + MaxBarsBack, 0),
        " bars on the chart. This click happened on #", 
        NumToStr(MouseClickBarNumber, 0),
        ".");

end;

//> 224 bars on the chart. This click happened on #194.
//> 224 bars on the chart. This click happened on #209.
//> 224 bars on the chart. This click happened on #173.
//> 224 bars on the chart. This click happened on #167.
//> 224 bars on the chart. This click happened on #138.

Here we first enable mouse click processing with ProcessMouseEvents. Then an if statement evaluates whether a click happened with the Control key pressed down (then MouseClickCtrlPressed returns true) and Shift not pressed down. This latter is tested by combining the not keyword with MouseClickShiftPressed. That way, it returns true when MouseClickShiftPressed is false.

The code inside the if statement contains a Print() statement that outputs two values, both formatted with NumToStr() to display zero decimals. The first are the total number of bars on the chart, which is returned by the current bar number (CurrentBar) together with the MaxBarsBack offset (see MultiCharts Wiki, 2012c). The second value that’s outputted is the clicked-on bar number (MouseClickBarNumber).

Access price bar data from any bar that is clicked on

With MouseClickBarNumber we can access price data from any bar that was clicked on, like so:


[ProcessMouseEvents = true];

Variables:
    bar_num(0);

if (MouseClickShiftPressed = true) then begin

    Print("Clicked on bar #", NumToStr(MouseClickBarNumber, 0), 
        ".");

    bar_num = (CurrentBar + MaxBarsBack) - MouseClickBarNumber;

    Print(
        "  Open: ", NumToStr(Symbol_Open[bar_num], 0),
        ", High: ", NumToStr(Symbol_High[bar_num], 0),
        ", Low: ", NumToStr(Symbol_Low[bar_num], 0),
        ", Close: ", NumToStr(Symbol_Close[bar_num], 0),
        ", Volume: ", NumToStr(Symbol_Ticks[bar_num], 0),
        ".");

end;

//> Clicked on bar #187.
//>   Open: 3183, High: 3190, Low: 3181, Close: 3189, Volume: 42034.
//> Clicked on bar #197.
//>   Open: 3200, High: 3212, Low: 3186, Close: 3186, Volume: 15866.
//> Clicked on bar #204.
//>   Open: 3151, High: 3160, Low: 3127, Close: 3152, Volume: 66413.
//> Clicked on bar #213.
//>   Open: 3162, High: 3168, Low: 3158, Close: 3164, Volume: 24954.

After enabling mouse clicks and creating the bar_num variable, an if statement evaluates whether Shift was pressed during a mouse click. When it was, the code inside the if statement is executed.

That code starts with outputting the bar number that was clicked on. To retrieve that bar’s price data, we need to know how many bars ago (relative to the current bar) that historical bar is. We determine that by subtracting the click’s bar number (MouseClickBarNumber) from the bar number on which the script is evaluated (CurrentBar + MaxBarsBack). So, if the script’s current bar number is 200 and the click happened on bar 175, price data from 25 bars ago is needed. This calculated value is stored in the bar_num variable.

The price data of the clicked-on bar is retrieved in the subsequent Print() statement. Each of the price bar components are accessed with bar_num and a keyword prefixed with Symbol_. These keywords allow for accessing any bar from the script, regardless of the maximum number of bars a script can reference (see MultiCharts Blog, 2012).

A mouse click’s bar number can also be used with drawing trend lines that ‘snap to’ bars and for plotting arrows with mouse clicks.

Summary

Mouse clicks can be processed when the ProcessMouseEvents attribute is set to true. After a click on the chart, MouseClickBarNumber returns the bar number belonging to the click’s region, starting with 1 for the first bar on the chart.


References

MultiCharts Blog (2012, February 13). MULTICHARTS 8.0 BETA 1– WHAT’S NEW. Retrieved on December 30, 2014, from http://www.multicharts.com/traders-blog/?p=540

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

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