The Command Line can change a chart’s symbol. But how can we use it to change the data range programmatically?

Changing a price data range with the MultiCharts .NET Command Line

The .csy command can set the data range with its from and to parameters (MultiCharts Wiki, 2014). For more on its parameters, see changing the chart symbol with the Command Line. The .csy command can be executed programmatically with the ChartCommands.CommandLine() method (PowerLanguage .NET Help, n.d.).

Basically, adjusting the data range of the primary data series with .csy looks like:

ChartCommands.CommandLine(".csy from=1/10/2014, to=24/12/2014");

This would change the data range from October 1, 2014, to December 24, 2014. By the way, the date format of from and to depends on your Windows settings and is either day/month/year or month/day/year (Andrew MultiCharts, 2014).

Tip: Only from or to might be set, but using both of them prevents that MultiCharts .NET makes assumptions about the data range, which can lead to odd behaviour (like a data range of one day). For more, see only changing the start or end date with the Command Line.

But hard-coding a symbol’s data range in your script comes with a loss of flexibility: the code will need to be edited and recompiled with each adjustment of the .csy command. A more convenient way is creating a date range with C#’s DateTime.

Set the data range of a MultiCharts .NET chart programmatically

One can, for example, adjust the data range based on the current computer date:

private bool changeDataRangeOnce = true;

protected override void CalcBar()
{
    if (changeDataRangeOnce && Bars.LastBarOnChart)
    {
        string command = String.Format(".csy from={0}, to={1}",
            DateTime.Now.AddDays(-10).ToString("d/M/yyyy"),
            DateTime.Now.ToString("d/M/yyyy"));

        ChartCommands.CommandLine(command);

        Output.WriteLine(command);

        changeDataRangeOnce = false;
    }
}

//> .csy from=11-11-2014, to=21-11-2014

The if statement evaluates if changeDataRangeOnce is true and whether the current bar is the last bar on the chart (in which case Bars.LastBarOnChart returns true).

Inside the if statement, String.Format() is used to generate the command string variable. This method allows for composite formatting, which is a way to combine a regular quoted string (".csy from={0}, to={1}") with placeholders ({0} and {1}) that correspond to the list of variables following the string (Sempf, Sphar, & Davis, 2010).

Creating a date range with C#’s DateTime formatted to a string

The first placeholder ({0}) in the string is replaced with the current date minus 10 days (DateTime.Now.AddDays(-10)) while the second ({1}) contains the current date (DateTime.Now).

Both DateTime values are formatted to a string with the "d/M/yyyy" custom format specifiers, which stand for the day of the month (1 through 31), the month (1 to 12), and the year as a four-digit number, respectively (Dorman, 2010). This way the DateTime values match the required format of the from and to parameters.

The command variable is then passed into the ChartCommands.CommandLine() method and outputted to the Output Window with Output.WriteLine(). Finally, the changeDateRangeOnce variable is set to false so that the .csy command is only executed once.

See changing the chart symbol with the Command Line for more general information on .csy. Only changing the start or end date uses .csy to change a symbol’s data range.

Summary

The .csy command uses the from and to parameters to change the symbol’s data range, which require a date format that matches your Windows date settings. Executing such Command Line commands is done with the ChartCommands.CommandLine() method.

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_ChangeDataRange : IndicatorObject
    {
        public Example_ChangeDataRange(object _ctx) : base(_ctx) { }

        private bool changeDataRangeOnce = true;

        protected override void CalcBar()
        {
            if (changeDataRangeOnce && Bars.LastBarOnChart)
            {
                string command = String.Format(".csy from={0}, to={1}",
                    DateTime.Now.AddDays(-10).ToString("d/M/yyyy"),
                    DateTime.Now.ToString("d/M/yyyy"));

                ChartCommands.CommandLine(command);

                Output.WriteLine(command);

                changeDataRangeOnce = false;
            }
        }

        //> .csy from=11-11-2014, to=21-11-2014
    }
}

References

Andrew MultiCharts (2014, October 6). Command line to change start date – forum discussion. Retrieved on November 20, 2014, from http://www.multicharts.com/discussion/viewtopic.php?f=1&t=47294#p108924

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

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

Sempf, B., Sphar, C., & Davis, S.R. (2010). C# 2010 All-In-One for Dummies. Hoboken, NJ: John Wiley & Sons.