The MultiCharts .NET Command Line can be used for things like changing the chart symbol and changing a symbol’s data range. But what if we only wanted to change the start or end date of a data series?

Changing a data series’ date range with the Command Line

The .csy Command Line command changes the settings of a chart’s data series (MultiCharts Wiki, 2014) and can be executed programmatically with the ChartCommands.CommandLine() method (PowerLanguage .NET Help, n.d.).

With .csy we can change the data range by specifying dates for its from and to parameters. These dates needs to be in the format day/month/year or month/day/year, depending on your Windows settings (Andrew MultiCharts, 2014).

When only from or to is provided, MultiCharts .NET will guess the value for the missing parameter. Since this can give odd data ranges, a better idea is to define both parameters yourself. But what if only one of these parameters should change?

Outputting the date range of the primary data series on the chart

First, we output the current chart’s data range:

protected override void StartCalc()
{
    Output.WriteLine("Data range is {0} to {1}.",
        Bars.Request.Range.From.ToString("d/M/yyyy"),
        Bars.Request.Range.To.ToString("d/M/yyyy"));
}

In the StartCalc() method, executed once when a script begins calculating (MultiCharts, 2013), the begin date (Bars.Request.Range.From) and end date (Bars.Request.Range.To) of the primary data series are outputted.

Those two DateTime values are formatted here with the "d/M/yyyy" custom format specifiers, which stand for the day of the month (1 to 31; "d"), the month (1 through 12; "M"), and "yyyy" specifies the year as a four-digit number (Dorman, 2010).

Changing a symbol’s data range programmatically in MultiCharts .NET

In the CalcBar() method the data range is changed:

private bool changeRangeOnce = false;

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

        ChartCommands.CommandLine(command);

        Output.WriteLine("Command Line command:");
        Output.WriteLine("    " + command);

        changeRangeOnce = true;
    }
}

//> Data range is 15-11-2014 to 23-11-2014.
//> Command Line command:
//>    .csy from=24-10-2014, to=23-11-2014
//> Data range is 24-10-2014 to 23-11-2014.

We start here with an if statement to check if the current bar is the last bar on the chart (then Bars.LastBarOnChart will return true) and whether the changeRangeOnce variable is false. This latter is tested with the logical not operator (!).

When both expressions are true, the value for the command variable is generated with String.Format(). In that method, composite formatting is used, which combines 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).

The first placeholder is replaced by the current computer date minus 30 days (DateTime.Now.AddDays(-30)) while the second placeholder contains the current end date of the chart (Bars.Request.Range.To). Both DateTime values are formatted with custom format specifiers so that they match the required format of from and to: for my Windows settings, this required format is d/M/yyyy.

When the command string variable is generated, it’s passed to the ChartCommands.CommandLine() method and printed to the Output Window for verification. The last statement sets the changeRangeOnce variable to true so that the if statement expression becomes false. That prevents the data range from changing continuously.

See changing the symbol with the Command Line for more on .csy and its parameters. Another example of .csy’s from and to parameters is loading a minimum amount of trading days on the chart.

Summary

The .csy command changes the data range of a symbol with its from and to parameters. When only one of these parameters is provided, the other value is estimated by MultiCharts .NET. An alternative is to provide both, with one using the current data range retrieved with Bars.Request.Range.From or Bars.Request.Range.To.

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

        protected override void StartCalc()
        {
            Output.WriteLine("Data range is {0} to {1}.",
                Bars.Request.Range.From.ToString("d/M/yyyy"),
                Bars.Request.Range.To.ToString("d/M/yyyy"));
        }

        private bool changeRangeOnce = false;

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

                ChartCommands.CommandLine(command);

                Output.WriteLine("Command Line command:");
                Output.WriteLine("    " + command);

                changeRangeOnce = true;
            }
        }

        //> Data range is 15-11-2014 to 23-11-2014.
        //> Command Line command:
        //>    .csy from=24-10-2014, to=23-11-2014
        //> Data range is 24-10-2014 to 23-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 (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

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