The Command Line can be used to add trading strategies to the chart or display the strategy’s performance report. But how can we enable or disable automated trading with programming code?

Turn auto-trading programmatically on or off in MultiCharts .NET

The .at_toggle Command Line command enables or disables automated trading on the current chart (MultiCharts Wiki, 2014). This command can be executed programmatically with the ChartCommands.CommandLine() method (PowerLanguage .NET Help, n.d.).

Since .at_toggle (Automated trading toggle) has no parameters, using it is simple:

ChartCommands.CommandLine(".at_toggle");

With automated trading off, executing this statement turns it on; if auto-trading is on, .at_toggle turns it off. The ChartCommands.CommandLine() method can execute this command from both trading strategies and indicators.

Since all enabled trading signals on a chart are considered one strategy (Henry MultiCharts, 2014), .at_toggle will enable or disable auto-trading for all those signals.

The .at_toggle command is useful for risk management by turning off automated trading programmatically. It’s less helpful for turning automated trading on since the legal warning still pops-up with .at_toggle (see Andrew MultiCharts, 2013). For example:

Enable auto-trading in MultiCharts .NET

Enable or disable automated trading with mouse clicks in MultiCharts .NET

In the example, automated trading on the chart is enabled or disabled with mouse clicks:

protected override void OnMouseEvent(MouseClickArgs arg)
{
    if (arg.buttons == MouseButtons.Left && arg.keys == Keys.Control)
    {
        if (Environment.IsAutoTradingMode)
        {
            Output.WriteLine("Disabling automated trading.");

            ChartCommands.CommandLine(".at_toggle");
        }
        else
        {
            Output.WriteLine("Enabling automated trading.");

            ChartCommands.CommandLine(".at_toggle");
        }
    }
}

//> Enabling automated trading.
//> Disabling automated trading.
//> Enabling automated trading.
//> Disabling automated trading.

The first if statement in the OnMouseEvent() method evaluates two variables: arg.buttons (that holds the mouse button that clicked on the chart) and arg.keys, which contains the keyboard key that was pressed down during the click. The if statement’s code block is executed when these are the left mouse button and Control key.

An if-else statement then evaluates the Environment.IsAutoTradingMode property, which returns true when automated trading is enabled on the chart (MultiCharts, 2013). When that is the case, a message saying that automated trading will be disabled is outputted. Then the ChartCommands.CommandLine() method is called with the .at_toggle command to turn auto-trading off.

Should automated trading already be disabled (the else portion of the if-else statement), again a message is outputted to the Output Window and ChartCommands.CommandLine() with .at_toggle is subsequently used to turn auto-trading on. This way, we can turn automated trading on or off by left clicking with Control on the chart.

The .at_toggle command can, for example, be used in combination with adding a strategy to the chart or used to turn automated trading off when there is a real-time data gap.

Summary

The .at_toggle Command Line command enables or disables automated trading on the active chart, and is executed programmatically with ChartCommands.CommandLine(). The Environment.IsAutoTradingMode property can be used to determine whether auto-trading is currently on or off.

Complete MultiCharts .NET trading strategy example

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;
using System.Windows.Forms;     // For Keys and MouseButtons enumerations

namespace PowerLanguage.Strategy
{
    [MouseEvents(true)]
    public class Example_EnableAutoTradingProgrammatically : SignalObject
    {
        public Example_EnableAutoTradingProgrammatically(object _ctx) : base(_ctx) { }

        protected override void CalcBar() { }

        protected override void OnMouseEvent(MouseClickArgs arg)
        {
            if (arg.buttons == MouseButtons.Left && arg.keys == Keys.Control)
            {
                if (Environment.IsAutoTradingMode)
                {
                    Output.WriteLine("Disabling automated trading.");

                    ChartCommands.CommandLine(".at_toggle");
                }
                else
                {
                    Output.WriteLine("Enabling automated trading.");

                    ChartCommands.CommandLine(".at_toggle");
                }
            }
        }

        //> Enabling automated trading.
        //> Disabling automated trading.
        //> Enabling automated trading.
        //> Disabling automated trading.
    }
}

References

Andrew MultiCharts (2013, June 25). Bulletproofing broker-to-MC connection issue – forum discussion. Retrieved on December 19, 2014, from http://www.multicharts.com/discussion/viewtopic.php?f=1&t=24888#p77603

Henry MultiCharts (2014, August 27). Getting MC to send an email alert – forum discussion. Retrieved on December 18, 2014, from http://www.multicharts.com/discussion/viewtopic.php?f=5&t=9890#p107711

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

MultiCharts Wiki (2014, August 18). MultiCharts Work Area: Understanding Command Line. Retrieved on December 16, 2014, from http://www.multicharts.com/trading-software/index.php/MultiCharts_Work_Area#Understanding_Command_Line

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