A mouse click’s location can be used to draw arrows and to freely draw trend lines. But how can we automatically attach trend lines to price bars with mouse clicks?

In this article:

Using mouse clicks to create trend lines in PowerLanguage

Before mouse clicks can be processed, the ProcessMouseEvents attribute needs to be set to true (MultiCharts Wiki, 2012a). Then any left or right mouse click on a data series’ chart area will update the mouse click keywords. These include MouseClickPrice (for the click’s price level) and MouseClickBarNumber, which returns the click’s bar number.

This click information can be used to draw trend lines on the price chart. One trend line keyword is TL_New_s(), which draws a line between a start and end point (MultiCharts Wiki, 2012b).

Drawing magnet-like trend lines with mouse clicks in MultiCharts

Let’s see what the programming example does by combining mouse clicks with TL_New_s(). Starting with a chart like:

Drawing magnet-like trend lines in MultiCharts - before

After a few clicks with Control the chart looks like:

Drawing magnet-like trend lines in MultiCharts - after

Programmatically drawing trend lines in PowerLanguage

The programming code is the following:


[ProcessMouseEvents = true];
[RecoverDrawings = false];

Variables:
    bar_num(0),
    IntraBarPersist beginTimeS(0),
    IntraBarPersist beginDate(0),
    IntraBarPersist beginPrice(0),
    endPrice(0),
    trendLineID(0);

if (MouseClickCtrlPressed or MouseClickShiftPressed) then 
    bar_num = (CurrentBar + MaxBarsBack) - MouseClickBarNumber;

// A mouse click with Control generates the line begin point
if (MouseClickCtrlPressed = true) then begin

    beginTimeS = Symbol_Time_s[bar_num];
    beginDate  = Symbol_Date[bar_num];

    if (MouseClickPrice > Symbol_High[bar_num]) then
        beginPrice = Symbol_High[bar_num]
    else 
        beginPrice = Symbol_Low[bar_num];

end;

// A mouse click with Shift draws the line
if (beginPrice <> 0) and (MouseClickShiftPressed) then begin

    if (MouseClickPrice > Symbol_High[bar_num]) then
        endPrice = Symbol_High[bar_num]
    else
        endPrice = Symbol_Low[bar_num];

    trendLineID = TL_New_s(beginDate, beginTimeS, beginPrice,
        Symbol_Date[bar_num],
        Symbol_Time_s[bar_num],
        endPrice);

    TL_SetSize(trendLineID, 2);

    TL_SetColor(trendLineID, RGB(0, 201, 87));

end;

We first set ProcessMouseEvents to true (to enable mouse click processing) and RecoverDrawings to false, which prevents intra-bar generated trend lines from being removed on the next script calculation (MultiCharts Wiki, 2014). Otherwise, the previously created trend line will be removed when a new click happens.

Then several variables are created, of which three are marked IntraBarPersist. This specifies that these variables should maintain their value during intra-bar script calculations; otherwise, they’ll only store the value assigned on bar close (MultiCharts Wiki, 2012c).

An if statement then evaluates whether Control or Shift were pressed during the click. When one of these was, the bar_num variable is given a value indicating how many bars ago (relative to the current bar) the click happened. For this we subtract the clicked on bar number (MouseClickBarNumber) from the number of bars on the chart (CurrentBar + MaxBarsBack). By knowing how many bars ago the click happened, we can access the price data from that specific bar.

Creating the MultiCharts line’s starting point for a click with Control

The next part is an if statement that checks if Control was pressed during the click, in which case MouseClickCtrlPressed returns true. We then store the time and date of the clicked on bar in the beginTimeS and beginDate variables, respectively. That price bar data is retrieved by combining the bar_num variable with the Symbol_ prefixed keywords. These keywords allow access to any price bar on the chart, regardless of the MaxBarsBack value (MultiCharts Blog, 2012).

A nested if statement then evaluates if the click’s price (MouseClickPrice) was above the bar’s high where the click happened (Symbol_High[bar_num]). When it was, this high is stored in beginPrice; otherwise, the bar’s low is stored. This makes the line’s begin point either the bar’s high or low, which creates those ‘magnet-like’ trend lines that connect themselves to the bar.

Drawing the trend line after a mouse click with Shift in PowerLanguage

The ensuing if statement evaluates two conditions: whether beginPrice is unequal to zero and if the Shift key was held down during the click (MouseClickShiftPressed). By checking if beginPrice isn’t zero, we prevent trying to plot a trend line when we don’t have the starting point yet.

The code inside that if statement begins with another if statement, similar to the one for a mouse click with Control: when the mouse click happened above the bar’s high, this high is stored in the endPrice variable; otherwise, the bar’s low gets stored.

At that point we have all the data needed to draw a trend line, which is done with TL_New_s(). Six parameters are required by this keyword (MultiCharts Wiki, 2012b): the date, time (with seconds), and price for both the line’s starting point and the ending point.

For the begin point we use beginDate, beginTimeS, and beginPrice — variables that were given a value when a click with Control happened. For the line’s ending point, we use the date of the currently clicked on bar region (Symbol_Date[bar_num]), that bar’s time (Symbol_Time_s[bar_num]), and the endPrice variable which we gave a value just a moment earlier.

Adjusting the size and colour of a PowerLanguage trend line

TL_New_s() returns a object-specific ID number, needed to modify the trend line after it has been drawn (MultiCharts Wiki, 2012b). This number is assigned to the trendLineID variable and used with the TL_SetSize() keyword. This keyword requires two parameters: the trend line ID and a number between 0 and 6 that indicates the new size of the trend line (MultiCharts Wiki, 2012d). By setting the new trend line size to 2, it’s made a bit bigger than the default size (0).

Next the colour of the trend line is changed with TL_SetColor(). Again we use the trendLineID variable to identify the trend line. The other parameter is the RGB() keyword, which returns a colour number that corresponds to a combination of red, green, and blue component values (MultiCharts Wiki, 2012e). The RGB value used here is emerald green.

Mouse clicks can also be used to freely draw trend lines, plot arrows, or for creating text boxes.

Summary

Mouse click processing is possible when ProcessMouseEvents is set to true. The TL_New_s() keyword draws trend lines on the price chart, and its begin and end points can be determined with MouseClickBarNumber and the Symbol_ prefixed keywords. With RecoverDrawings set to false, intra-bar created trend lines aren’t removed with the next script calculation.


References

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

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 13). TL_New_s. Retrieved on January 15, 2015, from http://www.multicharts.com/trading-software/index.php/TL_New_s

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

MultiCharts Wiki (2012d, January 26). TL_SetSize. Retrieved on January 13, 2015, from http://www.multicharts.com/trading-software/index.php/TL_SetSize

MultiCharts Wiki (2012e, February 19). RGB. Retrieved on January 15, 2015, from http://www.multicharts.com/trading-software/index.php/RGB

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