With mouse clicks we can, for example, plot arrows at mouse click locations. But how can we use mouse clicks to draw trend lines programmatically?

In this article:

Combining trend lines with mouse clicks in MultiCharts

Before mouse clicks can be processed in PowerLanguage, the ProcessMouseEvents attribute needs to be enabled (MultiCharts Wiki, 2012a). Then, once a click on a data series’ chart area occurs, several mouse click keywords are updated with the click’s information. These include the click’s price level (MouseClickPrice) and the click’s date and time value (MouseClickDateTime).

This mouse click data can then be used to draw a trend line. One keyword that creates such lines is TL_New_DT(), which draws a line between the specified price and DateTime values of the begin and end points (MultiCharts Wiki, 2013).

Drawing trend lines with mouse clicks in PowerLanguage

For an idea of how mouse clicks and TL_New_DT() can work, let’s see what the programming example does. Starting with a chart like the following:

Drawing MultiCharts trend lines with mouse clicks - before

After clicking a few times with Control or Shift held down, the chart looks like:

Drawing MultiCharts trend lines with mouse clicks - after

Programmatically drawing trend lines with mouse clicks

Drawing trend lines with mouse clicks can be done as follows:


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

Variables:
    IntraBarPersist startPointDT(0),
    IntraBarPersist startPointPrice(0),
    lineID(0);

// Store the line's starting point with a click + Control 
if (MouseClickCtrlPressed) then begin

    startPointDT    = MouseClickDateTime;
    startPointPrice = MouseClickPrice;

end;

// Draw the line with a click + Shift and when the starting point exists
if (startPointDT <> 0) and (MouseClickShiftPressed) then begin

    // Create the trend line
    lineID = TL_New_DT(startPointDT, startPointPrice,
        MouseClickDateTime, MouseClickPrice);

    // Adjust the trend line
    TL_SetSize(lineID, 2);

    if (startPointPrice > MouseClickPrice) then
        TL_SetColor(lineID, red)
    else 
        TL_SetColor(lineID, green);

end;

We start with enabling ProcessMouseEvents so that mouse clicks can be processed. To prevent trend lines from being removed on the next script calculation, the RecoverDrawings attribute needs to be set to false (MultiCharts Wiki, 2014). Otherwise, an intra-bar generated trend line will be removed when the next mouse click occurs.

Then a couple of variables are created, of which two are marked IntraBarPersist. This keyword specifies that the variable should hold its value with intra-bar script calculations; otherwise, it will only store the value it gets assigned on the close of the bar (MultiCharts Wiki, 2012b).

Creating the trend line’s starting point with mouse clicks

Next an if statement evaluates whether the Control key was pressed during a click (MouseClickCtrlPressed). When it was, both MouseClickDateTime and MouseClickPrice are stored in the startPointDT and startPointPrice variables, respectively. This way, each click with Control saves the starting point for a trend line.

These starting point values are used when a click with Shift happens (MouseClickShiftPressed), which is evaluated in the next if statement. That if statement also checks whether startPointDT is unequal to zero. This way we only proceed when a Control click has stored the starting point in the variable; otherwise, without a starting point we couldn’t draw a trend line. When both conditions evaluate to true, the code inside this if statement is executed.

Plotting trend lines in PowerLanguage on the MultiCharts price chart

The code inside that if statement begins with drawing the trend line on the chart with TL_New_DT(). This keyword requires four parameters (MultiCharts Wiki, 2013): the DateTime and price value of the line’s begin point and the DateTime and price value of the line’s end point.

For the line’s begin point we use the startPointDT and startPointPrice variables that we assigned a value earlier. The line’s end point is based on the current mouse click’s DateTime (MouseClickDateTime) and price value (MouseClickPrice).

The TL_New_DT() keyword returns an object-specific ID number that is needed to modify the trend line after it has been drawn (MultiCharts Wiki, 2013). In order to do that, we store the ID in the lineID variable.

Adjusting the size and colour of a trend line in PowerLanguage

That lineID variable is then used to change the line’s size with the TL_SetSize() keyword, which requires the trend line ID and a number between 0 and 6 to indicate the trend line’s new size (MultiCharts Wiki, 2012c).

An if statement then adjusts the line’s colour based on whether it slopes upward or downward. When the line’s starting point (startPointPrice) is higher than the ending point (MouseClickPrice), the line slants downward and is coloured red with TL_SetColor(); otherwise, it’s given a green colour.

Another use of trend lines and mouse clicks are trend lines that snap to price bars. Mouse clicks can also be used to draw text boxes on the chart or plot arrows.

Summary

Mouse click processing requires that ProcessMouseEvents is set to true. The TL_New_DT() keyword draws trend lines on the chart, and MouseClickPrice and MouseClickDateTime can be used to set the trend line’s begin and end point. When RecoverDrawings is set to false, intra-bar generated drawings aren’t removed with the next script calculation.


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

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

MultiCharts Wiki (2013, June 5). TL_New_DT. Retrieved on January 13, 2015, from http://www.multicharts.com/trading-software/index.php/TL_New_Dt

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