Situation
You want to know how to capture and work with mouse clicks on the price chart in MultiCharts .NET.

Programming example

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true), MouseEvents(true)]
    public class Example_MouseClickEvents : IndicatorObject
    {
        public Example_MouseClickEvents(object _ctx) : base(_ctx) { }

        protected override void StartCalc()
        {
            Output.Clear();
        }

        protected override void CalcBar() { } 

        // The OnMouseEvent method is called every time a mouse click on the chart is registered
        protected override void OnMouseEvent(MouseClickArgs arg)
        {
            string priceFormatted = (arg.data_number == 1) ? 
                arg.point.Price.ToString("F3") : arg.point.Price.ToString("F3");

            Output.WriteLine("Mouse click on bar number {0}, data series {1}." + 
                " Coordinates: price {2}, time {3}",
                    arg.bar_number,
                    arg.data_number,
                    priceFormatted,
                    arg.point.Time.ToString("d-M-y HH:mm:ss"));
        }
    }
}

Output of the programming example

With a chart like the one below:

Example of mouse clicks in MultiCharts .NET

The indicator generates the following text in the Output Window after a few clicks on the chart:

Mouse click on bar number 353, data series 1. Coordinates: price 1,39089, time 14-3-14 06:35:00
Mouse click on bar number 377, data series 1. Coordinates: price 1,38562, time 14-3-14 18:35:00
Mouse click on bar number 355, data series 2. Coordinates: price 142,317, time 14-3-14 07:35:00
Mouse click on bar number 329, data series 2. Coordinates: price 142,531, time 13-3-14 18:35:00
Mouse click on bar number 330, data series 2. Coordinates: price 142,467, time 13-3-14 19:05:00
Mouse click on bar number 330, data series 1. Coordinates: price 1,39287, time 13-3-14 19:05:00
Mouse click on bar number 356, data series 1. Coordinates: price 1,39377, time 14-3-14 08:05:00
Mouse click on bar number 359, data series 2. Coordinates: price 141,416, time 14-3-14 09:35:00
Mouse click on bar number 370, data series 2. Coordinates: price 142,360, time 14-3-14 15:05:00

Processing mouse click events in MultiCharts .NET

Programmatically processing mouse clicks (and information such as mouse button and click location) is quite useful. Mouse clicks can, for example, be used to reload price data, recalculate a script, read and write from text files, and even place limit orders with mouse clicks.

Working with mouse clicks in MultiCharts .NET has two requirements:

  • The MouseEvents attribute needs to be enabled;
  • The OnMouseEvent() method, used for the actual processing of mouse clicks (see PowerLanguage .NET Help, n.d.), needs to be implemented.

In the programming example both of these are used to retrieve mouse click information.

MultiCharts .NET programming example

The example begins with setting MultiCharts .NET class attributes:

[SameAsSymbol(true), MouseEvents(true)]

Setting MouseEvents to true turns mouse click processing on, while the SameAsSymbol attribute set to true displays the indicator on the main price chart instead in a sub-chart.

Clearing the PowerLanguage .NET Editor Output Window

Next is the StartCalc() override method:

protected override void StartCalc()
{
    Output.Clear();
}

This method, executed at the start of every calculation cycle (MultiCharts, 2013), calls the Output.Clear() method to clear the Output Window to prevent cluttering.

By the way, even though the CalcBar() method is empty, it is still implemented (see line 18). Because it is marked abstract in the base class, any subclass is required to implement it (see Liberty & MacDonald, 2009). Not doing so generates the ‘does not implement inherited abstract member’-error message.

Processing mouse events in MultiCharts .NET

The last part is the OnMouseEvent() method:

// The OnMouseEvent method is called every time a mouse click on the chart is registered
protected override void OnMouseEvent(MouseClickArgs arg)
{
    string priceFormatted = (arg.data_number == 1) ?
        arg.point.Price.ToString("F5") : arg.point.Price.ToString("F3");
 
    Output.WriteLine("Mouse click on bar number {0}, data series {1}." +
        " Coordinates: price {2}, time {3}",
            arg.bar_number,
            arg.data_number,
            priceFormatted,
            arg.point.Time.ToString("d-M-y HH:mm:ss"));
}

The OnMouseEvent() method has a MouseClickArgs struct variable, named arg here (see line 21), that provides access to all available mouse click information.

In lines 23-24, the priceFormatted string variable is assigned a value with the conditional operator: if the mouse click occurred on the first data series (arg.data_number == 1), the price click location (arg.point.Price) is formatted with 5 decimals (ToString("F5")); otherwise, the custom format specifier F3 converts the number to a string with 3 decimals. This way prices from the first data series (EUR/USD) and the second data series (EUR/JPY) are formatted with the correct number of decimals.

The Output.WriteLine() method (lines 26-31) outputs mouse click information with four substitution parameters: the bar number (arg.bar_number) and the data series number (arg.data_number) on which the click occurred, the formatted price location of the click (priceFormatted), and the time coordinate of the click (arg.point.Time) DateTime value formatted to a string.

To learn more about mouse clicks, see combining mouse clicks with keyboard shortcuts.

Key points:

  • Working with mouse click events requires setting the MouseEvents attribute to true and implementing the OnMouseEvent() method;
  • The MouseClickArgs variable in the OnMouseEvent() method contains information about the mouse click, such as x and y axis coordinates and on which data series the click took place.
References

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

MultiCharts (April 2013). MultiCharts .NET Programming Guide (version 1.0). Retrieved from http://www.multicharts.com/downloads/MultiCharts.NET-ProgrammingGuide-v1.0.pdf

PowerLanguage .NET Help (n.d.). Retrieved on May 16, 2014, from http://www.multicharts.com/downloads/PowerLanguage.NET.chm

Visit Kodify.net for more helpful coding articles.