The MultiCharts .NET Command Line can be used to add indicators and trading strategies to the chart. But after strategies have been added, how can we display the ‘Strategy Performance Report’ window programmatically?

Using the MultiCharts .NET Command Line to bring up a strategy’s report

The .rep Command Line command opens the Strategy Performance Report window (MultiCharts Wiki, 2014). Like the other Command Line commands, .rep can be executed programmatically with ChartCommands.CommandLine() (PowerLanguage .NET Help, n.d.).

Since .rep (Strategy Performance REPort) has no parameters, its usage simply is:

ChartCommands.CommandLine(".rep");

Indicators can also execute .rep programmatically, but the ‘Strategy Performance Report’ will not be displayed if there is no trading strategy on the chart.

Calling the Strategy Performance Report in MultiCharts .NET

To see what the programming example does, with a blank chart like the following:

Displaying the MultiCharts .NET strategy report - before

Adding the example indicator will add an indicator and two trading strategies, and display the ‘Strategy Performance Report’:

Displaying the MultiCharts .NET strategy report - after

Adding scripts programmatically to a MultiCharts .NET chart

The example begins with adding the scripts to the chart:

private bool scriptsAlreadyAdded = false;

protected override void StartCalc()
{
    if (scriptsAlreadyAdded == false)
    {
        ChartCommands.CommandLine(".iind name=Bollinger_Bands, bref=50");

        ChartCommands.CommandLine(".isig name1=Bollinger_Bands_LE, " +
            "name2=Bollinger_Bands_SE");

        scriptsAlreadyAdded = true;
    }
}

Here we first declare a Boolean variable (scriptsAlreadyAdded) and initialise it to false. This variable is used later on to prevent the scripts from being added repeatedly.

In the StartCalc() method, executed once at the beginning of every script calculation (MultiCharts, 2013), an if statement verifies if this scriptsAlreadyAdded variable is false. When it is, ChartCommands.CommandLine() is called twice: once with .iind to add an indicator and once with .isig to add trading strategies to the chart.

Then scriptsAlreadyAdded is set to true. Since that invalidates the if statement’s expression, the scripts are only added once.

Calling the Strategy Performance Report programmatically in MultiCharts .NET

Next the ‘Strategy Performance Report’ is called programmatically:

private bool alreadyCalled = false;

protected override void CalcBar()
{
    if (!alreadyCalled && Bars.LastBarOnChart)
    {
        ExecControl.RecalcLastBarAfter(new TimeSpan(0, 0, 0, 0, 500));

        alreadyCalled = true;
    }
}

protected override void OnRecalcLastBarAfterEvent()
{
    ChartCommands.CommandLine(".rep");
}

Here we first declare a Boolean variable (alreadyCalled) and set it to false. This variable is used afterwards to prevent the .rep command from being executed repeatedly.

The if statement in CalcBar() evaluates two expressions. The first uses the logical not operator (!) to see if it’s true that alreadyCalled is false, the second checks whether the current bar is the last on the chart (then Bars.LastBarOnChart returns true).

When both expressions evaluate to true, the RecalcLastBarAfter() method is called with a TimeSpan interval of 500 milliseconds. This method executes the OnRecalcLastBarAfterEvent() method when the time interval has elapsed (PowerLanguage .NET Help, n.d.). And that method, in turn, calls ChartCommands.CommandLine() with the .rep command to bring up the ‘Strategy Performance Report’.

This implementation gives MultiCharts .NET a small amount of time to process the added scripts before the ‘Strategy Performance Report’ is called programmatically.

Summary

The .rep Command Line command brings up the ‘Strategy Performance Report’ window for the chart’s trading strategies. This parameterless command can be executed programmatically with the ChartCommands.CommandLine() method.

Complete MultiCharts .NET indicator example

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

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

        private bool scriptsAlreadyAdded = false;

        protected override void StartCalc()
        {
            if (scriptsAlreadyAdded == false)
            {
                ChartCommands.CommandLine(".iind name=Bollinger_Bands, bref=50");

                ChartCommands.CommandLine(".isig name1=Bollinger_Bands_LE, " +
                    "name2=Bollinger_Bands_SE");

                scriptsAlreadyAdded = true;
            }
        }

        private bool alreadyCalled = false;

        protected override void CalcBar()
        {
            if (!alreadyCalled && Bars.LastBarOnChart)
            {
                ExecControl.RecalcLastBarAfter(new TimeSpan(0, 0, 0, 0, 500));

                alreadyCalled = true;
            }
        }

        protected override void OnRecalcLastBarAfterEvent()
        {
            ChartCommands.CommandLine(".rep");
        }
    }
}

For more information about coding and the C# programming language, see Kodify.net.


References

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

Visit Kodify.net for more helpful coding articles.