With the Command Line we can change a data series or add a new data series to the chart. Now, let’s see how to add indicators to the chart with the Command Line.

Adding an indicator programmatically to the chart

The .iind Command Line command adds an indicator to the price chart (MultiCharts Wiki, 2014). This command is executed programmatically with the ChartCommands.CommandLine() method (PowerLanguage .NET Help, n.d.).

Using .iind (Insert INDicator) looks like:

ChartCommands.CommandLine(".iind name=MACD");

This adds the MACD indicator to the price chart each time this ChartCommands.CommandLine() method is executed.

Options for adding a MultiCharts .NET indicator to the chart

The .iind command has the following parameters (MultiCharts Wiki, 2014):

Parameter Description Parameter example
name Specifies the indicator name, which needs to match exactly. When this parameter is missing, the ‘Insert Study’ window is opened when .iind is executed. name=Mov_Avg_Exponential adds the EMA indicator to the chart.
base Data series number to calculate the indicator on (optional; defaults to 1). To see the data series numbers, right-click on the chart and select ‘Format Instruments’ (look for the ‘Data #’ column): the second data series on the chart doesn’t necessarily is has the number 2. base=3 will base the indicator’s calculations on the third data series.
bref The maximum number of bars the indicator will reference (MaxBarsBack). Optional; defaults to the user’s default value (right-click on an indicator, select ‘Format [indicator name]’ and select the ‘Properties’ tab to see the MaxBarsBack value). bref=100 sets the MaxBarsBack of the indicator to 100.
fmt Specifies whether the ‘Format Study’ window should be displayed when .iind is executed. By default, it’s not shown. Adding the fmt parameter shows the ‘Format Study’ window, while omitting this parameter won’t bring it up.
, The comma separates multiple .iind commands. .iind name=RSI, bref=20

The .iind command can be executed programmatically by both trading strategies and indicators. What cannot be done programmatically is setting the inputs of an indicator or removing an indicator from the chart.

Tip: Each time .iind is executed, an instance of the specified indicator is added to the chart. To prevent ChartCommands.CommandLine() from adding a bunch of indicators with .iind, use an expression that acts as a check, such as a Boolean variable.

Adding a MultiCharts .NET indicator: before and after

To see what the programming example does, if we start with a chart like the following:

Adding a MultiCharts .NET indicator programmatically - before

Adding the example indicator to the chart leads to:

Adding a MultiCharts .NET indicator programmatically - after

Adding a MultiCharts .NET indicator programmatically

This is done with the following code:

private bool emaAdded = false;
private bool momentumAdded = false;

protected override void CalcBar()
{
    if (!emaAdded && (Bars.CurrentBar == 1))
    {
        ChartCommands.CommandLine(".iind name=Mov_Avg_Exponential");

        emaAdded = true;
    }

    if (!momentumAdded && Bars.LastBarOnChart)
    {
        ChartCommands.CommandLine(".iind name=Momentum, base=2, bref=50");

        momentumAdded = true;
    }
}

Here two Boolean variables (emaAdded and momentumAdded) are declared and initialised to false. These are used later on to prevent the same indicator from being added more than once.

The CalcBar() method consists out of two if statements. The first evaluates two expressions: whether the emaAdded variable is false (tested with the logical not operator; !) and if the current bar number is the first on the chart. This value is returned by the Bars.CurrentBar property (MultiCharts, 2013).

Executing the MultiCharts .NET .iind command programmatically

When both of those if statement expressions evaluate to true, the ChartCommands.CommandLine() method is executed. It contains a string argument with .iind for adding the Mov_Avg_Exponential indicator to the chart. Then the emaAdded variable is set to true to invalidate the if statement’s expression. That way, the command is only executed once.

The second CalcBar() if statement works similarly, although here the command is executed on the last bar (when Bars.LastBarOnChart returns true). In this case the .iind command passed into ChartCommands.CommandLine() adds the momentum indicator with a MaxBarsBack value of 50 and with its calculations based on the second data series.

For more on adding scripts programmatically, see adding trading strategies through the Command Line. Faulty .iind commands can be prevented by verifying whether a script exists.

Summary

The .iind Command Line command adds the indicator specified by its name parameter to the chart. This command can be executed programmatically with ChartCommands.CommandLine(). Each time this method is called, a new instance of the indicator is added to the chart.

Complete code of the MultiCharts .NET indicator

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

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

        private bool emaAdded = false;
        private bool momentumAdded = false;

        protected override void CalcBar()
        {
            if (!emaAdded && (Bars.CurrentBar == 1))
            {
                ChartCommands.CommandLine(".iind name=Mov_Avg_Exponential");

                emaAdded = true;
            }

            if (!momentumAdded && Bars.LastBarOnChart)
            {
                ChartCommands.CommandLine(".iind name=Momentum, base=2, bref=50");

                momentumAdded = true;
            }
        }
    }
}

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 12, 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