Mouse clicks can be used to draw arrows, freely draw trend lines, or trend lines that ‘snap to’ price bars. But how to draw text boxes that display information from different data series?

In this article:

Mouse clicks and text boxes in MultiCharts PowerLanguage

Before mouse clicks can be used in PowerLanguage, the ProcessMouseEvents attribute needs to be enabled (MultiCharts Wiki, 2012a). A click on a data series’ chart area then calculates the script and updates the mouse click keywords, including MouseClickDataNumber (for a click’s data number) and MouseClickBarNumber (the clicked-on bar number).

By using the click information we can create a text box on the price chart. One keyword that creates such drawings is Text_New_DT(), which displays a text at the specified DateTime and price value (MultiCharts Wiki, 2013).

Drawings can only be made on the data series that the script is based on (MultiCharts Support, personal communication, January 16, 2015). We therefore cannot programmatically make a text box on both a first and second data series.

So for this article’s example, the indicator is based on the second data series. To set that, right-click on the indicator’s name in the Status Line and select ‘Format [script name]‘. Then go to the ‘Properties’ tab and look for the ‘Base Study On’ setting:

The 'Base study on' setting in MultiCharts

Creating text boxes based on mouse clicks in MultiCharts

Before discussing the code, let’s see what it does when starting with a chart like:

Creating text boxes with mouse clicks in MultiCharts - before

A mouse click with Control on either data series creates a text box on the second data series, as follows:

Creating text boxes with mouse clicks in MultiCharts - after

PowerLanguage code for mouse click’s text boxes

We achieve the above effect with the following code:


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

Variables:
    bar_num(0),
    textMessage(""),
    pricePoint(0),
    textBoxID(0);

if (MouseClickCtrlPressed or MouseClickShiftPressed) then begin

    bar_num = (CurrentBar Data(MouseClickDataNumber) + 
        MaxBarsBack) - MouseClickBarNumber;

    // Determine the text's price location
    pricePoint = (Symbol_High[bar_num] - Symbol_Low[bar_num]) * 2;

    if (MouseClickCtrlPressed) then 
        pricePoint = Symbol_Close[bar_num] + pricePoint
    else 
        pricePoint = Symbol_Close[bar_num] - pricePoint;

    // Generating the text
    textMessage = Text(SymbolName Data(MouseClickDataNumber), 
        NewLine, "High: ",
        NumToStr(Symbol_High[bar_num] Data(MouseClickDataNumber), 5),
        NewLine, "Low: ",
        NumToStr(Symbol_Low[bar_num] Data(MouseClickDataNumber), 5));

    // Create and adjust text box
    textBoxID = Text_New_DT(MouseClickDateTime, 
        pricePoint, textMessage);

    Text_SetSize(textBoxID, 8);
    Text_SetBGColor(textBoxID, black);
    Text_SetColor(textBoxID, white);

end;

We start with enabling ProcessMouseEvents so that mouse clicks can be processed. Then the RecoverDrawings attribute is set to false. That prevents intra-bar generated text boxes from being removed on the next script calculation (MultiCharts Wiki, 2014).

After creating a couple of variables, an if statement evaluates whether either Control or Shift (MouseClickCtrlPressed and MouseClickShiftPressed) were pressed down during the click. The code inside the if statement is executed when this is the case.

Determining the bar number and price location of a mouse click

We then determine how many bars ago (relative to the last bar for the clicked-on data series) the click happened. For this the mouse click’s bar number (MouseClickBarNumber) is subtracted from the number of bars of the data series. This last is arrived at by adding the indicator’s MaxBarsBack value to the clicked-on data series’ current bar (CurrentBar Data(MouseClickDataNumber)). By the way, this approach doesn’t work well when the data series’ don’t align due to different time frames and/or session settings.

Now that we know how many bars ago the click happened, that bar’s price data can be retrieved. The bar_num variable is first used for determining the range of the clicked-on bar, which is stored in the pricePoint variable. The Symbol_ prefixed keywords are used to access this bar’s high and low since they can access any price bar on the chart (MultiCharts Blog, 2012). This range is multiplied by two so that the text box is placed some distance away from the bar.

Next an if-else statement checks if the Control key was pressed during the click. When it was, that bar’s range (stored in the pricePoint variable) is added to that bar’s close and reassigned to pricePoint. When Control wasn’t pressed (the else portion), we know Shift was (see the very first if statement). The calculated value for pricePoint is then placed below the bar. This way, a click with Control places the text box above the bar, while a click with Shift places it below the bar.

Generating the text and creating a text box in MultiCharts

The next part gives the textMessage string variable the content that will be displayed in the text box. It contains the symbol name of the clicked-on data series (SymbolName Data(MouseClickDataNumber)), the NewLine keyword to insert a new line, and the high and low of the clicked-on data series. These latter values are formatted with NumToStr() to display five decimals.

Then the actual text box is created with Text_New_DT(), which requires three parameters (MultiCharts Wiki, 2013): the DateTime and price value of the text box location and the text that should be displayed. For the first the mouse click’s date and time value (MouseClickDateTime) is used, for the second the pricePoint variable, and for the third we use the textMessage variable.

Adjusting a text box in PowerLanguage

TL_New_DT() returns an ID number that’s text box specific (MultiCharts Wiki, 2013). This number is stored in the textBoxID variable so that the text box can be modified after it has been made.

This ID number is then used to change three things: the text’s font size is set to 8 with Text_SetSize(), the background colour is set to black with Text_SetBGColor(), and the font colour is set to white with Text_SetColor().

Mouse clicks can also be used to create other drawings, such as arrows, freely drawn trend lines, or ‘snappy’ trend lines.

Summary

Mouse click processing requires ProcessMouseEvents to be set to true. Text boxes on the price chart can be created with Text_New_DT(), and their location can be set with MouseClickDateTime and Symbol_ prefixed keywords to access data from any bar. When RecoverDrawings is set to false, intra-bar generated text boxes 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 (2013, June 5). Text_New_DT. Retrieved on January 17, 2015, from http://www.multicharts.com/trading-software/index.php/Text_New_DT

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