After a click on the chart, mouse click information like the click’s price level can be used in the PowerLanguage code. But how can we trigger alerts based on mouse clicks?

In this article:

Using mouse clicks and alerts in MultiCharts PowerLanguage

Mouse clicks need to be enabled with the ProcessMouseEvents attribute before they’re usable in PowerLanguage (MultiCharts Wiki, 2012a). Clicks on the data series’ chart area will then update the mouse click keywords, including MouseClickPrice (the click’s price level), MouseClickShiftPressed and MouseClickCtrlPressed, which signal whether the Shift or Control keys were used with the click.

Alerts are generated programmatically with the Alert() keyword (MultiCharts Wiki, 2012b). Each time this keyword is executed, the notification window appears (if enabled in the script’s settings) and the alert is logged to the Order and Position Tracker window.

Note that alerts can only be generated in PowerLanguage when they are manually enabled in the ‘Alert’ tab of the script’s settings, like so:

Enabling alerts in MultiCharts

MultiCharts alerts programmatically generated by mouse clicks

Before examining the code, let’s see what it does. With alerts turned on (see image above), a mouse click with Control on the chart can generate notifications like the following:

MultiCharts alerts triggered with PowerLanguage code

Generating alerts programmatically based on mouse clicks

These alerts are generated with the following code:


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

// Disable the alert
if (MouseClickShiftPressed) then 
    alertActive = false;

// 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

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

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

    end;

end;

prevPrice = Close;

We start with enabling ProcessMouseEvents. Then three variables are created, all marked IntraBarPersist. This keyword specifies that the variable should also keep its value during the price bar; without IntraBarPersist a variable only remembers the value it had when the bar closed (MultiCharts Wiki, 2012c). Since both our mouse clicks and alerts can happen intra-bar, these variables need to maintain their intra-bar values.

Next an if statement evaluates MouseClickCtrlPressed, which returns true when the click occurred with the Control key. When that happens, the click’s price level (MouseClickPrice) is stored in the priceAlert variable and the alertActive true/false variable is set to true. This way, each click with Control updates the price alert value and switches the alert on.

This alertActive variable can also be switched off, which is done with the if statement that checks if the Shift key (MouseClickShiftPressed) was used with the click. With this code, previously set alerts are disabled before they’ve been triggered.

Then another if statement tests whether both the alertActive variable and the CheckAlert keyword evaluate to true. This keyword returns a value of true when alerts are manually enabled and when the bar is the last on the chart (MultiCharts Wiki, 2012d). This causes the if statement’s code to be only executed when a click with Control enabled alertActive and when the user manually enabled the alert settings for the script.

A nested if-else statement is evaluated when both of these conditions are true. In it, the Close keyword is used to access the current real-time tick. For historical bars this keyword returns the bar’s closing price, but with real-time bars it returns the current tick price (when the indicator is set to ‘Update on every tick’ in the ‘Properties’ tab).

The first part of this if-else statement checks whether the current price (Close) is greater than priceAlert and if prevPrice is less than or equal to priceAlert. This prevPrice variable is updated in the last line of the example so that it contains the price from the previous script calculation. These two conditions evaluate to true when the current tick crossed above the price alert level.

When that happens, the Alert() keyword is executed. The alert message is generated with the Text() keyword that combines a regular quoted string with the priceAlert variable formatted to five decimals with NumToStr(). Then alertActive is set to false so that the alert is only triggered once.

The second part of the if-else statement (the else if portion) is similar to the previous, except that we now evaluate whether the current price has dropped below the alert’s price level.

The very last statement of the example assigns the current price (Close) to the prevPrice variable. By doing so, this variable holds the previous price during the next script calculation.

Summary

Mouse clicks are processable when ProcessMouseEvents is set to true. MouseClickPrice returns the click’s price level, which can be used to trigger alerts. Programmatically triggering these notifications is done with Alert(). However, alerts only work when they’re manually enabled in the script’s settings.


References

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

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

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

MultiCharts Wiki (2012d, February 7). CheckAlert. Retrieved on January 18, 2015, from http://www.multicharts.com/trading-software/index.php/CheckAlert