4.3 Plotting on the Chart

From MultiCharts
Revision as of 04:26, 6 May 2013 by Roman MultiCharts (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Plot and PlotPaintBar

There are two types of plots in MultiCharts .Net: graphic and text. And both are inherited from IPlotObjectBase:

public interface IPlotObjectBase<T>
	Color BGColor { get; set; } //Backgroung color
	string Name { get; }//Plot name (set when the object is created)
	void Reset();//Not to draw the plot on this bar
	void Set(T val);//Set value on the current bar 
	void Set(T val, Color color); //Set value and color
	void Set(T val, KnownColor color); //Set value and color

Graphic Plots

Executed by the following interface:

public interface IPlotObject : IPlotObjectBase<double>
	IPlotAttributesArray<Color> Colors { get; }//Plot color
	IPlotAttributesArray<double> Values { get; }//Plot value
	IPlotAttributesArray<int> Widths { get; }//Thickness
	void Set(int barsAgo, double val);//Set value with displacement
	void Set(double val, Color color, int width);//Set value, color, thickness
	void Set(double val, KnownColor color, int width);//Set value, color, thickness
	void Set(int barsAgo, double val, Color color); //Set value, color with displacement
	void Set(int barsAgo, double val, KnownColor color); //Set value, color with displacement
	void Set(int barsAgo, double val, Color color, int width); //Set value, color, thickness with displacement
	void Set(int barsAgo, double val, KnownColor color, int width); //Set value, color, thickness with displacement


  • barsAgo = 0 for the current bar.
  • barsAgo > 0 displacement to the history(barsAgo = 1 – the previous bar.)
  • barsAgo < 0 – displacement to the future(barsAgo = -1 –the next bar).

Graphic indicators are created in the Create() study class method through the following methods:

AddPlot() or AddPlot(PlotAttributes info).

In PlotAttributes plot style can be set:

PlotAttributes(int plotNum);// Plot number (0-99)
PlotAttributes(string name);// Plot string name
PlotAttributes(string name, int plotNum);//Plot name and number
PlotAttributes(string name, EPlotShapes type, Color fgColor);//Name, type and color
PlotAttributes(string name, EPlotShapes type, Color fgColor, Color bgColor, int width, int style, bool showLastPriceMarker); 
//Name, type, color, background color, thickness, style, show/hide plot marker on the chart.

Plot types:

public enum EPlotShapes

Line = 0,0_Line.png

Histogram = 1,1_Histogram.png

Point = 2,2_point.png

Cross = 3,3_cross.png

BarHigh = 4, High of the bar (for PaintBar), BarLow is obligatory to use it

BarLow = 5, Low of the bar (for PaintBar), BarHigh is obligatory to use it 5_barlow.png

LeftTick = 6, tick to the left from the bar (for PaintBar)6_lefttick.png

RightTick = 7, tick to the right from the bar (for PaintBar)7_righttick.png

BarHigh, BarLow, LeftTick, RightTick allow to draw a complete bar. To build a bar OHLC type, four plots are necessary.

The Style property (line styles): LineStyles.png

Tip: If it is necessary to paint bars in the indicator, then click the checkbox Same As Symbol in dialog window of indicator settings, or for the indicator class, set the [SameAsSymbol(true)] attribute.


This is an example of the indicator that paints uptrend bars in blue and downtrend bars in red:

public class SB_Up_Dn : IndicatorObject {
	public SB_Up_Dn(object _ctx):base(_ctx){}
	private IPlotObject plotH;
	private IPlotObject plotL;
	private IPlotObject plotO;
	private IPlotObject plotC;
	protected override void Create() {
		plotH = AddPlot(new PlotAttributes("", EPlotShapes.BarHigh, Color.Red));
		plotL = AddPlot(new PlotAttributes("", EPlotShapes.BarLow, Color.Red));
		plotO = AddPlot(new PlotAttributes("", EPlotShapes.LeftTick, Color.Red));
		plotC = AddPlot(new PlotAttributes("", EPlotShapes.RightTick, Color.Red));
	protected override void CalcBar(){
		if (Bars.Close[0] > Bars.Open[0]){
			plotH.Set(Bars.High[0], Color.Blue);
			plotL.Set(Bars.Low[0], Color.Blue);
			plotO.Set(Bars.Open[0], Color.Blue);
			plotC.Set(Bars.Close[0], Color.Blue);
		if (Bars.Close[0] < Bars.Open[0]){
			plotH.Set(Bars.High[0], Color.Red);
			plotL.Set(Bars.Low[0], Color.Red);
			plotO.Set(Bars.Open[0], Color.Red);
			plotC.Set(Bars.Close[0], Color.Red);

Now the indicator will be drawn, not on a separate subchart, but right above the symbol that it is applied to.


Text plots

Used to display text information in the indicator status line and are represented by the following interface:

public interface IPlotObjectStr : IPlotObjectBase<string>

Let’s illustrate it with an example. The indicator will output values to the status line: “UP” when Close > Open and “DN” when < Open :

public class UpDn : IndicatorObject {
	public UpDn(object _ctx):base(_ctx){}
	private IPlotObjectStr plot_str;
	protected override void Create() {
		plot_str = AddPlot(new StringPlotAttributes(1));
	protected override void CalcBar(){
		plot_str.Set("FLAT", Color.Cyan);
		if (Bars.OpenValue < Bars.CloseValue) 
			plot_str.Set("UP", Color.Yellow);
		if (Bars.OpenValue > Bars.CloseValue) 
			plot_str.Set("DN", Color.Red);

The result: UpDnUp.png


There are three types of drawings in MultiCharts .NET, which can be created from a study: TrendLine, Text, and Arrow. Every drawing type is created by the corresponding factory, which can be accessed through the following properties:

        ITextContainer DrwText { get; }
        ITrendLineContainer DrwTrendLine { get; }
        IArrowContainer DrwArrow { get; }

Each property allows accessing the collection of drawings of a particular type for the given script. All of the drawings are created by calling the Create () method.


protected override void CalcBar()
            if (Bars.CurrentBar == 1)
                ChartPoint top = new ChartPoint(Bars.TimeValue, Bars.HighValue);
                ChartPoint bottom = new ChartPoint(Bars.TimeValue, Bars.LowValue);
                DrwTrendLine.Create(bottom, top);
                DrwArrow.Create(bottom, false);
                ITextObject textObj = DrwText.Create(top, "1");
                textObj.VStyle = ETextStyleV.Above;

This script will mark the first bar of the data series with one trendline from Low to High, then it will draw an arrow below the bar and a text with the bar number above it.

The Create() method will form a corresponding type of drawing and bring back the reference to the interface which can be used to manipulate the object: it can be moved and its properties and styles can be changed.

We specify a point on the chart to which the drawing is attached when creating a drawing (two points for the trendline: start and end points). When creating a drawing there is also an opportunity to specify the data stream number and the drawing will be created according to its scale. Finally, if the study has a separate subchart, it is possible to specify that the drawing should be plotted on the subchart, where the study is applied. When arrow objects are created, it is possible to specify the arrow direction (up/down), and when text objects are created it is possible to specify the displayed text.

In every drawing collection there is a possibility to get access to the active object on the chart. Active property returns the reference to the corresponding object type. For example:

IArrowObject activeObj = DrwArrow.Active;
if (activeObj != null) { /* do something */ }

Drawing is considered to be an active object on the chart, if it was created or moved last, or was selected by mouse click.

Object movement is done by changing its coordinates. For a trendline object, they are Begin and End properties, and for text objects and arrows, it is Location property.

All types of objects have the Color property. Script can manage the following individual object properties:

  1. Trendline has ExtRight and ExtLeft properties. If both properties are set as false they will provide a segment, if both are set as true they will provide a straight line and when one property is set as false and the other is set as true a beam be provided. Property size is used to get/set the line thickness. Alert property is used to enable/disable the signals of the trendline cross by the price data series. Property Style represents line style (solid, dashed, dotted, dashed2, dashed3). There is also an arbitrary trendline point price receiving method:
    double PriceValue(DateTime dateTime);<br>

  2. BSColor property of the text object allows you to get/set a background color.
    HStyle and VStyle properties are used for text position management, the horizontal and vertical text position, regarding the text drawing point (Location). Border property is used to enclose the text into a frame. FontName property sets the font. Font styles are applied through the SetFont() method. Style availability can be checked through the HaveFont() method. Text property returns/sets the text.
  3. The direction of arrow objects can be received/set through Direction property. Size property determines the size of the arrow. Text property allows you to get/set the text to an arrow object. FontName, TextSize, TextColor, TextBGColor properties and HaveFont(), SetFont() methods are used to manage text attributes.

Drawing removal can be done through the Delete() method for the corresponding object.

Note. If a drawing was created as the result of the script calculation made not at the bar Close, it will be DELETED after the calculation. To change this behavior (which may be considered strange by many programmers) and to not delete such drawings the following study type attribute should be set: