This article summarises the most important points of mouse clicks in MultiCharts PowerLanguage. Follow the article links for examples and more information.

In this article:

Working with mouse clicks in PowerLanguage

Before mouse clicks can be processed in an indicator or trading strategy, the ProcessMouseEvents attribute needs to be enabled (MultiCharts Wiki, 2012a):


[ProcessMouseEvents = true];

Then each left or right mouse click on the chart area of a data series triggers a script calculation on the last bar. This way the current mouse click data can be processed in the script with the mouse click keywords.

Mouse click PowerLanguage keywords: using keyboard keys

Those keywords include the keyboard keywords: MouseClickCtrlPressed returns true when a mouse click happened with the Control key (MultiCharts Wiki, 2012b), while MouseClickShiftPressed does the same for the Shift key (MultiCharts Wiki, 2012c). They can be used as follows:


[ProcessMouseEvents = true];

if (MouseClickCtrlPressed or MouseClickShiftPressed) then begin

    Print("* * * *");

    if (MouseClickCtrlPressed) then 
        Print("Control was pressed down");

    if (MouseClickShiftPressed) then
        Print("Shift was pressed down");

    if (MouseClickCtrlPressed and not MouseClickShiftPressed) then
        Print("Control was pressed down, but Shift wasn't");

    if (not MouseClickCtrlPressed and MouseClickShiftPressed) then
        Print("Control wasn't pressed down, but Shift was");

    if (MouseClickCtrlPressed and MouseClickShiftPressed) then
        Print("Both Control and Shift were pressed down");

end;

Mouse click PowerLanguage keywords: the clicked-on bar

The MouseClickBarNumber keyword returns the bar number that was clicked on (MultiCharts Wiki, 2012d), like so:


[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;

With MouseClickDataNumber we retrieve the clicked-on data series (MultiCharts Wiki, 2012e). For example:


[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.");

Mouse click PowerLanguage keywords: a click’s chart location

A mouse click location is returned by MouseClickPrice (for the click’s price level) and MouseClickDateTime, which returns a click’s date and time (MultiCharts Wiki, 2012f, 2012g). Their usage looks like:


[ProcessMouseEvents = true];

if (MouseClickCtrlPressed = true) then begin

    Print("Click's price: ", NumToStr(MouseClickPrice, 2),
        ", DateTime value: ", NumToStr(MouseClickDateTime, 2));

end;

With this mouse click data we can determine where a click took place:

A click’s location can also be used to create drawings:

Mouse clicks and the calculation reason in PowerLanguage

A script’s calculation reason is retrieved with the GetAppInfo() keyword and its aiCalcReason argument (MultiCharts Wiki, 2014). This keyword allows for determining which mouse button was pressed, like so:


[ProcessMouseEvents = true];

Variables:
    leftClick(False),
    rightClick(False);

leftClick  = (GetAppInfo(aiCalcReason) = CalcReason_MouseLClick);
rightClick = (GetAppInfo(aiCalcReason) = CalcReason_MouseRClick);

if (leftClick = true) then
    Print("A left click happened")
else if (rightClick) then 
    Print("A right click occurred");

GetAppInfo(aiCalcReason) can also be used to only calculate a script partly with a mouse click, like so:


[ProcessMouseEvents = true];

if (GetAppInfo(aiCalcReason) <> CalcReason_MouseLClick) and 
    (GetAppInfo(aiCalcReason) <> CalcReason_MouseRClick) then
        #return;

// All code below #return is only executed when a
// left or right mouse click occurred

Print("Clicked on price ", NumToStr(MouseClickPrice, 5));

Mouse clicks and other PowerLanguage features

Mouse clicks can be combined with other MultiCharts features, such as executing Command Line commands:


[ProcessMouseEvents = true];

if (MouseClickShiftPressed) then 
    CommandLine(".rld int=1 day")
else if (MouseClickCtrlPressed) then 
    CommandLine(".rld int=2 weeks, res=30 min");

Or using mouse clicks to generate alerts:


[ProcessMouseEvents = true];

Variables:
    IntraBarPersist priceAlert(0),
    IntraBarPersist alertActive(false),
    IntraBarPersist prevPrice(0);

// Enable alert and register alert price 
if (MouseClickCtrlPressed) then begin

    priceAlert  = MouseClickPrice;
    alertActive = true;

end;

// Generate the alert
if (alertActive and CheckAlert) then begin

    if (Close > priceAlert) and 
        (prevPrice <= priceAlert) then begin

        Alert(Text("Price rose above the alert's price of ", 
            NumToStr(priceAlert, 5)));
        alertActive = false;

    end;

end;

prevPrice = Close;

We can even submit market orders with mouse clicks and keyboard keys, like so:


[ProcessMouseEvents = true];
[IntrabarOrderGeneration = true];

// Open a position with a mouse click with Control
if (MouseClickCtrlPressed) then begin

    if (MouseClickPrice > High) then 
        SellShort ("ES click") 1 contract next bar at market;

end;

// Close an open position with a mouse click + Control + Shift
if (MouseClickShiftPressed) then begin

    if (MarketPosition(0) = -1) then 
        BuyToCover ("XS click") all contracts next bar at market;   

end;

Summary

This concludes the chapter on mouse clicks in MultiCharts. See all MultiCharts PowerLanguage programming articles to explore other topics.


References

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

MultiCharts Wiki (2012b, February 24). MouseClickCtrlPressed. Retrieved on January 3, 2015, from http://www.multicharts.com/trading-software/index.php/MouseClickCtrlPressed

MultiCharts Wiki (2012c, February 24). MouseClickShiftPressed. Retrieved on January 3, 2015, from http://www.multicharts.com/trading-software/index.php/MouseClickShiftPressed

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

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

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

MultiCharts Wiki (2012g, February 24). MouseClickDateTime. Retrieved on January 1, 2015, from http://www.multicharts.com/trading-software/index.php/MouseClickDateTime

MultiCharts Wiki (2014, September 8). GetAppInfo. Retrieved on January 6, 2015, from http://www.multicharts.com/trading-software/index.php/GetAppInfo