After a mouse click on the price chart, the click’s price and date and time coordinates can be used to find out in which chart area the click took place. But how can a mouse click location be determined relative to a certain chart point?

In this article:

Mouse clicks and their relative location in MultiCharts

Before mouse clicks can be processed in PowerLanguage, the ProcessMouseEvents attribute needs to be enabled (MultiCharts Wiki, 2012a). After that any click in the chart area of a data series will update MouseClickPrice with the click’s price level and MouseClickDateTime with the click’s date and time (MultiCharts Wiki, 2012b, 2012c).

To determine a click’s relative position, its location can be compared to a fixed chart point, such as a price chart corner. The values of chart edges can be retrieved with the GetAppInfo() keyword (MultiCharts Wiki, 2014):

  • GetAppInfo(aiHighestDispValue) returns the highest displayed price value in the current chart view, while GetAppInfo(aiLowestDispValue) returns the lowest.
  • GetAppInfo(aiLeftDispDateTime) returns the DateTime value of the leftmost bar in the current chart view, GetAppInfo(aiRightDispDateTime) the DateTime of the rightmost bar.

The image below shows which GetAppInfo() argument returns which chart value (most price bars were removed for clarity):

Explanation of MultiCharts PowerLanguage's GetAppInfo

Retrieving a chart’s corner with GetAppInfo in PowerLanguage

In the example below, a mouse click’s distance to the lower left chart corner is determined:


[ProcessMouseEvents = true];

Variables:
    leftCornerPrice(0),
    leftCornerDateTime(0),
    priceDifference(0),
    timeDifference(0),
    timeDiffMinutes(0),
    timeDiffHours(0);

if (MouseClickCtrlPressed) then begin

    leftCornerPrice    = GetAppInfo(aiLowestDispValue);
    leftCornerDateTime = GetAppInfo(aiLeftDispDateTime);

    // Outputting the location of the corner and click
    Print("The chart's left corner:", NewLine, Spaces(3),
        "Price: ", NumToStr(leftCornerPrice, 5), 
        ", time & date: ",
        FormatDate("d-M", leftCornerDateTime),
        FormatTime(" HH:mm:ss", leftCornerDateTime));

    Print("Current click location: ", NewLine, Spaces(3),
        "Price: ", NumToStr(MouseClickPrice, 5),
        ", time & date: ",
        FormatDate("d-M", MouseClickDateTime),
        FormatTime(" HH:mm:ss", MouseClickDateTime));

    // Calculating the differences between the corner and the click
    priceDifference = MouseClickPrice - leftCornerPrice;
    timeDifference  = MouseClickDateTime - leftCornerDateTime;
    timeDiffHours   = timeDifference / ELTimeToDateTime(100);
    timeDiffMinutes = timeDifference / ELTimeToDateTime(1);

    Print("Distance towards the left corner:", NewLine, Spaces(3),
        "Price: ", NumToStr(priceDifference, 5),
        ", hours: ", NumToStr(timeDiffHours, 1),
        " or in minutes: ", NumToStr(timeDiffMinutes, 0));

    Print("* * * * *");

end;

//> The chart's left corner:
//>   Price: 1.17574, time & date: 8-1 04:35:00
//> Current click location: 
//>   Price: 1.17727, time & date: 9-1 02:35:00
//> Distance towards the left corner:
//>   Price: 0.00152, hours: 22.0 or in minutes: 1320
//> * * * * *
//> The chart's left corner:
//>   Price: 1.17574, time & date: 8-1 04:35:00
//> Current click location: 
//>   Price: 1.18218, time & date: 9-1 07:05:00
//> Distance towards the left corner:
//>   Price: 0.00644, hours: 26.5 or in minutes: 1590
//> * * * * *
//> The chart's left corner:
//>   Price: 1.17574, time & date: 8-1 04:35:00
//> Current click location: 
//>   Price: 1.18340, time & date: 9-1 22:05:00
//> Distance towards the left corner:
//>   Price: 0.00766, hours: 41.5 or in minutes: 2490
//> * * * * *

We begin with setting ProcessMouseEvents to true so that mouse clicks can be processed and then go on to create several variables.

An if statement subsequently evaluates whether the Control key was held down during the click, in which case MouseClickCtrlPressed returns true. The code inside the if statement is executed when this is the case.

That code begins with assigning GetAppInfo(aiLowestDispValue) to the leftCornerPrice variable and GetAppInfo(aiLeftDispDateTime) to the leftCornerDateTime variable. With both these variables combined we have the chart coordinates of the lower left corner.

Outputting the chart’s corner and mouse click’s location

Then two Print() statements output the chart’s lower left corner as well as the current click location. Both outputs are highly similar and use NewLine to insert a new line (carriage return), Spaces() for indentation, and NumToStr() to convert numerical values to a string with a certain amount of decimals.

The two Print() statements also format DateTime values into a readable text. For the date we use FormatDate() with the d-M format string: d for the day of the month and M for the month of the year (MultiCharts Wiki, 2012d). Both d and M don’t display a leading zero for single-digits.

FormatTime() generates the time into a readable format with the HH:mm:ss format string: HH stands for the hours in 24-hour format, mm for the minutes, and ss for the seconds (MultiCharts Wiki, 2012e). All three of these include a leading zero for single-digits.

Calculating the relative distance of a mouse click in MultiCharts

Next the relative distance between the mouse click and the chart’s corner is calculated. We first subtract leftCornerPrice from the mouse click’s price level (MouseClickPrice) and store this difference in the priceDifference variable. The same is done for the click’s DateTime value (MouseClickDateTime), and that computed value is stored in the timeDifference variable.

To display the relative time difference in number of hours, we divide timeDifference (which holds a DateTime value) by the DateTime value of 1 hour. This last value is generated with ELTimeToTimeDate(), which converts a time in HHmm format to DateTime (MultiCharts Wiki, 2012f). By using this keyword with a value of 100, we get the DateTime value of 1 hour.

After this calculated value is stored in timeDiffHours, the value for timeDiffMinutes is determined similarly except that here we divide by the DateTime value of 1 minute (ELTimeToDateTime(1)).

Then the Print() keyword outputs both the price and time differences between the mouse click’s location and the current lower left chart corner. To keep a click’s output separate, the last statement outputs several asterisks (*).

Other uses of GetAppInfo() and a chart’s edges are determining the chart area where a click occurred.

Summary

Mouse clicks can be used programmatically after ProcessMouseEvents is set to true. Once a click happens on the chart, MouseClickPrice returns the click’s price and MouseClickDateTime the click’s time and date. With GetAppInfo() the current chart axis’ values can be determined and compared to the click’s location.


References

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

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

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

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

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

MultiCharts Wiki (2012f, February 13). ELTimeToDateTime. Retrieved on January 10, 2015, from http://www.multicharts.com/trading-software/index.php/ELTimeToDateTime

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