The Command Line can change a chart’s data series, for example by changing the resolution programmatically. But how to cycle through different resolutions?

Changing the chart resolution programmatically in MultiCharts .NET

The .csy Command Line command changes a data series on the chart (MultiCharts Wiki, 2014), and commands like this are executed programmatically with the ChartCommands.CommandLine() method (PowerLanguage .NET Help, n.d.). For more on .csy, like the optional dnum (data series number) parameter, see changing the chart symbol with the Command Line.

Changing a data series’ resolution is done with .csy’s res parameter (MultiCharts Wiki, 2014). But switching to another resolution based on the current one requires getting the current resolution first. For the primary data series, Bars.Info.Resolution.Type returns the resolution type while Bars.Info.Resolution.Size returns the resolution size (see Henry MultiCharts, 2013).

Switching to chart resolutions programmatically in MultiCharts .NET

The example changes to another resolution with mouse clicks:

protected override void StartCalc()
{
    Output.WriteLine("Current resolution: {0} {1}",
        Bars.Info.Resolution.Size,
        Bars.Info.Resolution.Type);
}

protected override void OnMouseEvent(MouseClickArgs arg)
{
    if (arg.buttons == MouseButtons.Left && arg.keys == Keys.Control)
    {
        switch (Bars.Info.Resolution.Type)
        {
            case EResolution.Hour:
                Output.WriteLine("  Switching to a 3 point chart");
                ChartCommands.CommandLine(".csy res=3 point");
                break;

            case EResolution.Points:
                Output.WriteLine("  Switching to a 30 minute chart");
                ChartCommands.CommandLine(".csy res=30 minutes");
                break;

            default:
                Output.WriteLine("  Switching to a 1 hour chart");
                ChartCommands.CommandLine(".csy res=1 hour");
                break;
        }
    }
}

//> Current resolution: 1 Hour
//>     Switching to a 3 point chart
//> Current resolution: 3 Points
//>   Switching to a 30 minute chart
//> Current resolution: 30 Minute
//>     Switching to a 1 hour chart
//> Current resolution: 1 Hour

In the StartCalc() method, executed once at the start of a script calculation (MultiCharts, 2013), the time frame information is printed to the Output Window. This information consists out of the resolution size (Bars.Info.Resolution.Size) and the resolution type (Bars.Info.Resolution.Type).

The OnMouseEvent() method, which is used for processing mouse clicks (see PowerLanguage .NET Help, n.d.), begins with an if statement that has two expressions: whether the click on the chart was a left mouse button (MouseButtons.Left) and if the keyboard key pressed during the click was the Control key (Keys.Control).

‘Switching on’ the chart’s resolution in MultiCharts .NET

When both expressions are true, the enumeration returned by Bars.Info.Resolution.Type is evaluated in a switch statement and one of three cases has its code executed.

The first case is for an hourly resolution (EResolution.Hour). It prints a message to the Output Window followed by using the ChartCommands.CommandLine() method with .csy to change the primary data series to a 3 point chart.

The second case deals with point charts (EResolution.Points) and uses .csy to change the resolution to a 30 minute chart. The last case, the default case, will switch any other time frame to a 1 hour chart with the .csy command and its res parameter passed into the ChartCommands.CommandLine() method.

For more on .csy and its parameters, see changing the chart symbol programmatically. See changing the resolution with the Command Line for a list of all possible values for the res parameter.

Summary

The .csy command together with the res parameter change the resolution of a data series when passed into the ChartCommands.CommandLine() method. The Bars.Info.Resolution.Size and Bars.Info.Resolution.Type properties return the current resolution of the first data series.

Complete code of the MultiCharts .NET indicator

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using System.Windows.Forms;     // For MouseButtons and Keys enumerations

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

        protected override void CalcBar() { }

        protected override void StartCalc()
        {
            Output.WriteLine("Current resolution: {0} {1}",
                Bars.Info.Resolution.Size,
                Bars.Info.Resolution.Type);
        }

        protected override void OnMouseEvent(MouseClickArgs arg)
        {
            if (arg.buttons == MouseButtons.Left && arg.keys == Keys.Control)
            {
                switch (Bars.Info.Resolution.Type)
                {
                    case EResolution.Hour:
                        Output.WriteLine("  Switching to a 3 point chart");
                        ChartCommands.CommandLine(".csy res=3 point");
                        break;

                    case EResolution.Points:
                        Output.WriteLine("  Switching to a 30 minute chart");
                        ChartCommands.CommandLine(".csy res=30 minutes");
                        break;

                    default:
                        Output.WriteLine("  Switching to a 1 hour chart");
                        ChartCommands.CommandLine(".csy res=1 hour");
                        break;
                }
            }
        }

        //> Current resolution: 1 Hour
        //>     Switching to a 3 point chart
        //> Current resolution: 3 Points
        //>   Switching to a 30 minute chart
        //> Current resolution: 30 Minute
        //>     Switching to a 1 hour chart
        //> Current resolution: 1 Hour
    }
}

References

Henry MultiCharts (2013, December 13). MultiCharts .NET FAQ – forum discussion. Retrieved on November 27, 2014, from http://www.multicharts.com/discussion/viewtopic.php?f=19&t=45848#p100800

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