The bar spacing of a MultiCharts .NET price chart can be changed with the ‘Increase bar spacing’ and ‘Decrease bar spacing’ options, both accessible through the ‘Format’ menu. But what if you want to change the bar spacing programmatically?

Changing price bar spacing through the Command Line

Price bar spacing can be changed with the following Command Line commands (MultiCharts Wiki, 2014):

  • Increasing the bar spacing is done with .bsi (Bar Spacing Increase).
  • And .bsd (Bar Spacing Decrease) decreases the bar spacing.

Executing these commands programmatically is done with the ChartCommands.CommandLine() method (PowerLanguage .NET Help, n.d.).

Both .bsi and .bsd have no parameters and change the bar spacing with one step each time they’re executed. The size of this step depends on the bar spacing: with a bar spacing below 10 pixels, the commands change the spacing with 1 pixel; otherwise, multiple pixels.

To see the bar spacing or adjust it manually, right-click on the chart and select ‘Format Window’. Then go to the ‘X - Time Scale’ tab:

Example of MultiCharts .NET bar spacing

Changing the bar spacing with mouse clicks in MultiCharts .NET

One way to change the bar spacing is with mouse click events, like so:

protected override void OnMouseEvent(MouseClickArgs arg)
{
    if (arg.buttons != MouseButtons.Left || arg.keys == Keys.None)
        return;

    if (arg.keys == Keys.Shift)
    {
        Output.WriteLine("Increasing the bar spacing with .bsi.");
        ChartCommands.CommandLine(".bsi");
    }
    else if (arg.keys == Keys.Control)
    {
        Output.WriteLine("Decreasing the bar spacing with .bsd.");
        ChartCommands.CommandLine(".bsd");
    }

    Thread.Sleep(100);

    Output.WriteLine("  Bar spacing is now {0} px.", 
        Environment.BarSpacing);
}

//> Decreasing the bar spacing with .bsd.
//>   Bar spacing is now 9 px.
//> Decreasing the bar spacing with .bsd.
//>   Bar spacing is now 8 px.
//> Decreasing the bar spacing with .bsd.
//>   Bar spacing is now 7 px.
//> Decreasing the bar spacing with .bsd.
//>   Bar spacing is now 6 px.
//> Increasing the bar spacing with .bsi.
//>   Bar spacing is now 7 px.
//> Increasing the bar spacing with .bsi.
//>   Bar spacing is now 8 px.
//> Increasing the bar spacing with .bsi.
//>   Bar spacing is now 9 px.

In this OnMouseEvent() method the first if statement exits the method early with return when the mouse click wasn’t a left click (arg.buttons != MouseButtons.Left) or when no keyboard key was pressed during the click (arg.keys == Keys.None). This way the rest of this method is only executed when there is a left mouse click on the chart with a keyboard key pressed down.

In the if-else statement the click gets processed in one of two ways: a click with the Shift key (arg.keys == Keys.Shift) passes the .bsi Command Line command to the ChartCommands.CommandLine() method. And a click with the Control key (arg.keys == Keys.Control) executes the .bsd command.

Adding a small pause for an asynchronous MultiCharts .NET method

After that, the Thread.Sleep() method is executed with a value of 100, which pauses the current thread for this amount of milliseconds (Albahari & Albahari, 2012). The bar spacing (returned by the Environment.BarSpacing property; PowerLanguage .NET Help, n.d.) is then outputted to the Output Window.

This small Thread.Sleep() delay is needed due to the asynchronous ChartCommands.CommandLine() method; otherwise, we would still output the Environment.BarSpacing value from before executing the .bsi or .bsd command.

Summary

The bar spacing can be changed with the .bsi (increases bar spacing) and .bsd (decreases bar spacing) commands, which can be executed programmatically with the ChartCommands.CommandLine() method. Since this is an asynchronous method, a small pause with Thread.Sleep() is needed when referencing the bar spacing immediately after calling ChartCommands.CommandLine().

Full MultiCharts .NET indicator programming example

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using System.Windows.Forms;     // Added for the MouseButtons and Keys enumerations
using System.Threading;         // Used for the Thread.Sleep() method

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

        protected override void CalcBar() { }

        protected override void OnMouseEvent(MouseClickArgs arg)
        {
            if (arg.buttons != MouseButtons.Left || arg.keys == Keys.None)
                return;

            if (arg.keys == Keys.Shift)
            {
                Output.WriteLine("Increasing the bar spacing with .bsi.");
                ChartCommands.CommandLine(".bsi");
            }
            else if (arg.keys == Keys.Control)
            {
                Output.WriteLine("Decreasing the bar spacing with .bsd.");
                ChartCommands.CommandLine(".bsd");
            }

            Thread.Sleep(100);

            Output.WriteLine("  Bar spacing is now {0} px.", 
                Environment.BarSpacing);
        }

        //> Decreasing the bar spacing with .bsd.
        //>   Bar spacing is now 9 px.
        //> Decreasing the bar spacing with .bsd.
        //>   Bar spacing is now 8 px.
        //> Decreasing the bar spacing with .bsd.
        //>   Bar spacing is now 7 px.
        //> Decreasing the bar spacing with .bsd.
        //>   Bar spacing is now 6 px.
        //> Increasing the bar spacing with .bsi.
        //>   Bar spacing is now 7 px.
        //> Increasing the bar spacing with .bsi.
        //>   Bar spacing is now 8 px.
        //> Increasing the bar spacing with .bsi.
        //>   Bar spacing is now 9 px.
    }
}

References

Albahari, J. & Albahari, B. (2012). C# 5.0 in a Nutshell: The Definitive Reference (5th edition). Sebastopol, CA: O’Reilly Media.

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