The MultiCharts .NET Command Line can be used to add an indicator to the chart. But how can we add a bunch of indicators to the chart?

Adding a group of indicators to the price chart with the Command Line

The .iind Command Line command adds an indicator to the price chart (MultiCharts Wiki, 2014). This command can be executed from the code with ChartCommands.CommandLine() (PowerLanguage .NET Help, n.d.). For an overview of .iind’s parameters, see adding an indicator with the Command Line.

A convenient way to add multiple indicators to the chart is with a collection. That way the different indicator names are organised into a ‘container’ (so we don’t overlook one) and repetitive code is kept to a minimum.

In C#, a List<T> is a convenient collection type because it’s numerically indexed (so elements can be accessed by number) and resizes dynamically (Dorman, 2010), meaning a list’s capacity doesn’t need to be managed by the programmer. Furthermore, List<T> is generic and thus can hold all types, whether these are int, string, or any other object (Stellman & Greene, 2010).

Adding a list of indicators to a MultiCharts .NET chart: before and after

To see the effect of the programming example, if we start with a chart like the following:

Adding a list of MultiCharts .NET indicators - before

Adding the example indicator (discussed below) has the following effect:

Adding a list of MultiCharts .NET indicators - after

Adding multiple indicators programmatically in MultiCharts .NET

To achieve this, we start with the following:

List<String> indicators = new List<String>();
private bool indicatorsAdded;

protected override void Create()
{
    indicators.Add("Volume");
    indicators.Add("Mov_Avg_Adaptive");
    indicators.Add("Mov_Avg_Exponential");
    indicators.Add("Keltner_Channel");

    indicatorsAdded = false;
}

Here a List<String> (a generic list of string variables) is declared and initialised, followed by declaring the indicatorsAdded Boolean variable. This variable is used later on to prevent that the indicators are being added repeatedly.

In the Create() method, executed once immediately after the creation of the script object (MultiCharts, 2013), the indicators list has several elements added. This is done with the list’s Add() method, which adds an object to the end of the list (Dorman, 2010). These four string values need to match an existing indicator’s name exactly (naming errors can be prevented by verifying if an indicator’s name exists).

After adding those elements to the list, the indicatorsAdded variable is given its initial value of false.

Adding a list of indicators programmatically to a MultiCharts .NET price chart

The StartCalc() method, executed once at the beginning of a calculation cycle (MultiCharts, 2013), adds the indicators to the chart:

protected override void StartCalc()
{
    if (indicatorsAdded == false)
    {
        for (int i = 0; i < indicators.Count; i++)
        {
            ChartCommands.CommandLine(
                String.Format(".iind name={0}", indicators[i]));
        }

        indicatorsAdded = true;
    }
}

This if statement evaluates whether the indicatorsAdded variable is equal to false. When it is, the for loop inside the if statement is executed.

This loop begins at zero (i = 0), is increased with 1 after each iteration (i++), and continues as long as i is less than the length of the list (returned by the list’s zero-based Count property).

In the loop, the ChartCommands.CommandLine() method is executed. String.Format() is passed to this method so that the .iind command string can be generated with a {0} placeholder. That placeholder is replaced by a list element that contains an indicator’s name (indicators[i]). This way, the command to add an indicator to the chart is dynamically generated.

When the loop is completed, all indicators are added to the chart and the indicatorsAdded variable is set to true. That ensures that the programming code inside the if statement is only executed once.

Summary

Indicators are added programmatically to the price chart by passing the .iind command and its parameters into the ChartCommands.CommandLine() method. By using a List<T> collection that holds the indicator names, multiple indicators can be conveniently added to the chart.

Complete code of the MultiCharts .NET indicator

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using System.Collections.Generic;       // For the List collection

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

        List<String> indicators = new List<String>();
        private bool indicatorsAdded;

        protected override void Create()
        {
            indicators.Add("Volume");
            indicators.Add("Mov_Avg_Adaptive");
            indicators.Add("Mov_Avg_Exponential");
            indicators.Add("Keltner_Channel");

            indicatorsAdded = false;
        }

        protected override void StartCalc()
        {
            if (indicatorsAdded == false)
            {
                for (int i = 0; i < indicators.Count; i++)
                {
                    ChartCommands.CommandLine(
                        String.Format(".iind name={0}", indicators[i]));
                }

                indicatorsAdded = true;
            }
        }

        protected override void CalcBar() { }
    }
}

References

Dorman, S. (2010). Sams Teach Yourself Visual C# 2010 in 24 Hours. Indianapolis, IN (USA): Sams/Pearson Education.

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 November 18, 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

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O’Reilly Media.