After a mouse click on a MultiCharts chart, we can access the bar number and price level from that click. But how do we get a mouse click’s date and time?

In this article:

Working with a mouse click’s time and date in MultiCharts

Processing mouse clicks in PowerLanguage is enabled by setting the ProcessMouseEvents attribute to true (MultiCharts Wiki, 2012a). After a click on the chart’s data series, the MouseClickDateTime keyword returns the closing time of the bar belonging to the clicked-on chart area (MultiCharts Wiki, 2012b). With no click, MouseClickDateTime returns 0.

The x-axis value of MouseClickDateTime is the data series’ bar time and date in DateTime format. The integer part of this number represents the days that have elapsed since January 1, 1900, while the fractional portion represents the time that has elapsed since midnight (MultiCharts Wiki, 2013). This value allows for calculating time differences, but let’s first format DateTime values to a readable format.

Retrieving a mouse click’s DateTime value in MultiCharts

DateTime values can be displayed in a coherent format with FormatTime() and FormatDate(), like this:


[ProcessMouseEvents = true];

Variables:
    dtClick(0);

if (MouseClickCtrlPressed) then begin

    dtClick = MouseClickDateTime;

    Print("Mouse click's DateTime = ",
        NumToStr(dtClick, 5));

    Print(" Readable date of this bar: ",
        FormatDate("d-M-yyyy", dtClick));

    Print(" Readable time of this bar: ",
        FormatTime("HH:mm:ss", dtClick));

end;

//> Mouse click's DateTime = 42003.54167
//>  Readable date of this bar: 30-12-2014
//>  Readable time of this bar: 13:00:00
//> Mouse click's DateTime = 41991.50000
//>  Readable date of this bar: 18-12-2014
//>  Readable time of this bar: 12:00:00
//> Mouse click's DateTime = 41996.85417
//>  Readable date of this bar: 23-12-2014
//>  Readable time of this bar: 20:30:00

After setting ProcessMouseEvents to true, the dtClick variable is created that will hold the DateTime value of a mouse click. Next, an if statement evaluates whether a mouse click happened with the Control keyboard key, in which case MouseClickCtrlPressed returns true.

Inside the if statement, dtClick is assigned the DateTime value of the price bar region where the click occurred (MouseClickDateTime). The Print() statement outputs this value with NumToStr() to display it with five decimals.

Next, both the time and date of dtClick are outputted to a comprehensible format. FormatDate() formats the date component of dtClick with the d-M-yyyy format string, which stands for the day of the month from 1 to 31 (d), the month number with no leading zeros (M), and yyyy for the four-digit year (MultiCharts Wiki, 2012c).

The time part of dtClick is formatted with FormatTime(). Its format string contains HH (hours in 24-hour format with a leading zero for single-digit hours), mm (minutes with a leading zero for single-digit minutes), and ss, which displays the seconds with leading zeros for single-digit seconds (MultiCharts Wiki, 2012d).

Calculating the difference between mouse clicks in MultiCharts

Determining the time difference between two clicks can be done as follows:


[ProcessMouseEvents = true];

Variables:
    IntraBarPersist dtPrevClick(0),
    dtDiffMinutes(0);

if (MouseClickShiftPressed) then begin

    if (dtPrevClick <> 0) then begin 

        Print("Current click @ ",
            FormatTime("HH:mm:ss", MouseClickDateTime),
            " - ",
            FormatDate("d-M-yyyy", MouseClickDateTime));

        Print(" Previous click @ ",
            FormatTime("HH:mm:ss", dtPrevClick),
            " - ",
            FormatDate("d-M-yyyy", dtPrevClick));

        dtDiffMinutes = (MouseClickDateTime - dtPrevClick) / 
            ELTimeToDateTime(1);

        Print(" Time difference between both clicks: ",
            NumToStr(dtDiffMinutes, 0), " minutes.");    

    end;

    dtPrevClick = MouseClickDateTime;

end;

//> Current click @ 14:00:00 - 23-12-2014
//>  Previous click @ 18:30:00 - 23-12-2014
//>  Time difference between both clicks: -270 minutes.
//> Current click @ 13:00:00 - 19-12-2014
//>  Previous click @ 14:00:00 - 23-12-2014
//>  Time difference between both clicks: -5820 minutes.
//> Current click @ 17:00:00 - 22-12-2014
//>  Previous click @ 13:00:00 - 19-12-2014
//>  Time difference between both clicks: 4560 minutes.

After enabling mouse clicks, two variables are created. dtPrevClick will hold the DateTime value of the previous click, and is marked IntraBarPersist since it needs to retain its value during intra-bar calculations (see MultiCharts Wiki, 2012e). The dtDiffMinutes variable is used to hold the time difference (between two clicks) in minutes.

Then an if statement evaluates whether a mouse click with the Shift key occurred (MouseClickShiftPressed). When it did, a nested if statement checks if dtPrevClick has a value unequal to zero. When this variable is not zero, we know it holds the value of the previous click and can be used to calculate the time difference.

But first the current and previous click’s DateTime values are outputted with Print() and the FormatTime() and FormatDate() keywords.

The time difference is calculated by subtracting the previous mouse click’s DateTime value (dtPrevClick) from the current click’s value (MouseClickDateTime). To convert this DateTime value to the number of minutes, we divide it by ELTimeToDateTime(1) which returns the DateTime value of one minute (see MultiCharts Wiki, 2012f).

When the time difference is outputted with Print(), the final statement sets the dtPrevClick variable to the current click’s DateTime value. This way the variable holds the previous value during the next script calculation.

Other uses of a click’s date and time are determining in which chart area a click happened and freely draw trend lines with mouse clicks. See accessing a click’s price level for working with a chart’s y-axis values.

Summary

With ProcessMouseEvents set to true, mouse clicks can be programmatically be processed. After a click on the chart, MouseClickDateTime returns the DateTime value of the chart’s x-axis. This value can be used for date and time calculations, but needs FormatTime() and FormatDate() to be readable.


References

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

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

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

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

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

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

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