3.1. How to Create a .NET Indicator

From MultiCharts
Jump to navigation Jump to search

Let’s say that we want to write an indicator showing trend strength (please note that it is just an example and this indicator does not show true trend strength) in the form of a histogram. It will be calculated on the average price difference for a fast and a slow period and expressed in percent from the Close price of the current bar.


To create a new indicator, it is necessary to start PowerLanguage .NET Editor. In the editor main menu, select File, then select New, then New Indicator.


In the dialog window below:

New Indicator


Choose C# language and enter the name of the new indicator, i.e. TrendPower.

The PowerLanguage Editor will automatically create a template of an indicator class with one plot.

Let’s rename it for our convenience:

private IPlotObject power_line;

In the Create() method choose the histogram style, color and add a text marker:

        protected override void Create() {
	power_line = AddPlot(new PlotAttributes("Power", EPlotShapes.Histogram, Color.Cyan));
}

Two inputs will also be necessary for setting the quantity of bars for the calculation of the average price for fast and slow periods:

[Input]
public int fastLength { get; set; }
		
[Input]
public int slowLength { get; set; }

Assume that fastLength = 7, slowLength = 14 and are set as the default values. This is done in the indicator constructor:

public TrendPower(object _ctx):base(_ctx)
{
		fastLength = 7;
		slowLength = 14;
}

Write a private function that calculates average median price for the period:

private double AvgVal( int length )
{
	double aval = 0.0;
	for (int i = 0; i < length; ++i)
		aval += Bars.AvgPrice(i);
	return aval / length;
}

The logic of plot value setting on each bar should be described in the CalcBar() method:

protected override void CalcBar(){
	double afast = AvgVal(fastLength);//Average price for a short period 
	double aslow = AvgVal(slowLength);//Average price for a long period
        //Calculation of trend strength in percent from a close price of the current bar 
	double power = Math.Abs(100*(afast - aslow)/Bars.Close[0]);
        power_line.Set(power);//Plot value setting for the current bar. 
}

As a result, we have an indicator, plotting a trend strength histogram upon two averages:

public class TrendPower : IndicatorObject {
	public TrendPower(object _ctx):base(_ctx)
	{
		fastLength = 7;
		slowLength = 14;
	}
	
	private IPlotObject power_line;
	
	[Input]
	public int fastLength { get; set; }
	
	[Input]
	public int slowLength { get; set; }
		
		
	private double AvgVal( int length )
	{
		double aval = 0.0;
		for (int i = 0; i < length; ++i)
			aval += Bars.AvgPrice(i);
		return aval / length;
	}
		
	
	protected override void Create() {
		power_line = AddPlot(new PlotAttributes("Power", EPlotShapes.Histogram, Color.Cyan));
	}
		
	protected override void CalcBar(){
		double afast = AvgVal(fastLength);
		double aslow = AvgVal(slowLength);
		double power = 	Math.Abs(100*(afast - aslow)/Bars.Close[0]);
		power_line.Set(power);
	}
}

Press F7 button for compilation of the indicator. It can now be plotted on the chart. Choose it in the Insert Study dialog window on the Indicator tab. Chart1.png

Next, we will highlight the areas of the histogram with a strong trend. Once the limits for what is to be considered as a strong trend and what is to be considered as a weak trend and their corresponding colors are determined inputs can be created as follows.

[Input]
public double strongLevel { get; set; }
[Input]
public Color strongColor { get; set;}
[Input]
public Color weakColor { get; set;}

In the indicator logic the plot color will be chosen depending on the trend strength:

protected override void CalcBar(){
			double afast = AvgVal(fastLength);
			double aslow = AvgVal(slowLength);
			double power = 	Math.Abs(100*(afast - aslow)/Bars.Close[0]);
			Color pl_col = weakColor;
			if (power >= strongLevel ) pl_col = strongColor;		
			power_line.Set(power, pl_col);
}

Now the indicator highlights red areas with a strong trend:

Chart2.png