Situation
You want to know how to set the options of a graphic or text plot programmatically in MultiCharts .NET as soon as the plot is created in the indicator’s Create() method.

Plotting in MultiCharts .NET

Plotting in a MultiCharts .NET indicator requires three things: declaring the plot object, creating (meaning, initialising) this plot object in the Create() override method, and setting the value of the plot (MultiCharts, 2013). During this second step, the plot options can be defined.

See plotting options in MultiCharts .NET to learn more about the different types of plots.

Setting plot options: the overloaded PlotAttributes struct

When a plot object is created with the AddPlot() method, the settings of the plot (such as the colour and plot type) can be set with the PlotAttributes struct (see MultiCharts, 2013; PowerLanguage .NET Help, n.d.).

The constructor of the PlotAttributes struct is overloaded (see PowerLanguage .NET Help, n.d.), meaning that this object can be created in different ways by providing different arguments (see Liberty & MacDonald, 2009). The examples below show these different ways to use PlotAttributes.

The AddPlot() method can also be used without the PlotAttributes argument (PowerLanguage .NET Help, n.d.), which creates a plot with the default options.

P.S. A struct is a value type similar to classes, but cannot inherit from other classes or structs (Liberty & MacDonald, 2009). It can be thought of as a lightweight version of a C# class.

Creating a basic plot with MultiCharts .NET’s PlotAttributes

The first PlotAttributes() constructor takes no arguments:

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    public class Example_PlotAttributes : IndicatorObject
    {
        private IPlotObject examplePlot;

        public Example_PlotAttributes(object _ctx) : base(_ctx) { }
        
        protected override void Create()
        {
            examplePlot = AddPlot(new PlotAttributes());
        }
        
        protected override void CalcBar()
        {
            examplePlot.Set(Bars.Close[0]);
        }
    }
}

Note: changing plot settings in the code will not change the plotted indicator: to see the new plot settings, remove and re-add the indicator (see Henry MultiCharts, 2014).

When PlotAttributes() is used without arguments (as it is done here in the Create() override method), a solid line plot with default options is created in MultiCharts .NET.

Specifying the plot number and name

Another way is to set the plot number and/or plot name when creating a plot (PowerLanguage .NET Help, n.d.):

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    public class Example_PlotAttributes : IndicatorObject
    {
        private IPlotObject numberedPlot, namedPlot, nameNumberPlot;

        public Example_PlotAttributes(object _ctx) : base(_ctx) { }

        protected override void Create()
        {
            numberedPlot   = AddPlot(new PlotAttributes(1));
            namedPlot      = AddPlot(new PlotAttributes("Bar high"));
            nameNumberPlot = AddPlot(new PlotAttributes("Bar close", 3));
        }

        protected override void CalcBar()
        {
            numberedPlot.Set(Bars.Low[0]);
            namedPlot.Set(Bars.High[0]);
            nameNumberPlot.Set(Bars.Close[0]);
        }
    }
}

The first plot object, numberedPlot in line 16, uses the overloaded PlotAttributes() constructor that allows setting the plot number. This also names this plot ‘Plot1’ in the MultiCharts .NET Data Window and Format Study window.

The second plot (namedPlot in line 17) sets the name of the plot to ‘Bar high’, while the third plot (nameNumberPlot, line 18) sets the plots name to ‘Bar close’ and its plot number to 3.

The plot number determines the plot order on the chart, in the Format Study window, and in the Hint and Data Window: plots with a higher number are displayed above plots with a lower number (MultiCharts Support, personal communication, July 29, 2014). Plot numbers don't have another use.

Specifying plot type and colour in MultiCharts .NET

Another version of PlotAttributes() is setting the plot type and colour (PowerLanguage .NET Help, n.d.):

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    public class Example_PlotAttributes : IndicatorObject
    {
        private IPlotObject dottedPlot;

        public Example_PlotAttributes(object _ctx) : base(_ctx) { }

        protected override void Create()
        {
            dottedPlot = AddPlot(new PlotAttributes("Close", EPlotShapes.Point, Color.RoyalBlue));
        }

        protected override void CalcBar()
        {
            dottedPlot.Set(Bars.Close[0]);
        }
    }
}

Here dottedPlot (line 16) is given a plot name of ‘Close’ and set to a line with separate dots (EPlotShapes.Point) in blue (Color.RoyalBlue).

To learn more about the available plot types in MultiCharts .NET and the EPlotShapes enumeration, see the different types of plots in MultiCharts .NET.

Specifying the plot style, width, and colours in MultiCharts .NET

The most extensive PlotAttributes() constructor has the following seven parameters (PowerLanguage .NET Help, n.d.):

  • The name of the plot;
  • The plot type, specified by an EPlotShapes enumerator;
  • The colour of the plot;
  • The background colour of the plot (only applicable to the Market Scanner; MultiCharts Wiki, 2012);
  • The plot width, which ranges from 1 to 14;
  • The plot style, either set by an EPlotStyle enumerator (see image below) or an integer value.
  • And a Boolean variable specifying whether to show the plot’s price marker on the y-axis (true) or not (false).

The different EPlotStyle enumerators for setting the style of a plot in MultiCharts .NET:

The different plot style options in MultiCharts .NET

An example of this comprehensive PlotAttributes() constructor is the following:

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    public class Example_PlotAttributes : IndicatorObject
    {
        private IPlotObject linePlot;

        public Example_PlotAttributes(object _ctx) : base(_ctx) { }

        protected override void Create()
        {
            linePlot = AddPlot(new PlotAttributes("Close", EPlotShapes.Line, Color.Orange, 
                Color.Black, 3, EPlotStyle.Solid, true));
        }

        protected override void CalcBar()
        {
            linePlot.Set(Bars.Close[0]);
        }
    }
}

P.S. Only solid lines (i.e., EPlotStyle.Solid) can have their line width changed (see Kirillov, 2007). For other line types, this PlotAttributes() argument has no effect.

In lines 16-17 a plot is created with the ‘Close’ plot name that is plotted as a line (EPlotShapes.Line) in orange (Color.Orange) and with a black Market Scanner cell background (Color.Black). The plot line has a width of 3, is solid (EPlotStyle.Solid), and displays the price marker (true).

Key points:

  • The overloaded PlotAttributes() constructor allows creating a plot with different options with the AddPlot() method in the Create() override method;
  • PlotAttributes() with no arguments creates a default line plot;
  • PlotAttributes(int), PlotAttributes(string), and PlotAtrributes(string, int) can be used to set the plot number and/or plot name;
  • PlotAttributes(string, EPlotShapes, Color) allows setting the plot name, type, and colour;
  • PlotAttributes(string, EPlotShapes, Colour, Colour, int, EPlotStyle, bool) allow setting the plot name, type, foreground colour, Market Scanner background colour, width, style, and price marker.
References

Henry MultiCharts (2014, 18 June). Plotting Questions forum discussion. Retrieved on 29 July, 2014, from http://www.multicharts.com/discussion/viewtopic.php?f=19&t=46745#p105907

Kirillov, A. (2007, February 8). New Bug?: Can't change line width property (of Plot1) forum discussion. Retrieved on July 29, 2014, from http://www.multicharts.com/discussion/viewtopic.php?f=1&t=3089#p10240

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

MultiCharts (2013). MultiCharts .NET Programming Guide (version 1.0). Retrieved from http://www.multicharts.com/downloads/MultiCharts.NET-ProgrammingGuide-v1.0.pdf

MultiCharts Wiki (2012, February 19). SetPlotBGColor. Retrieved on July 29, 2014, from http://www.multicharts.com/trading-software/index.php/SetPlotBGColor

PowerLanguage .NET Help (n.d.). Retrieved on May 16, 2014, from http://www.multicharts.com/downloads/PowerLanguage.NET.chm