Your interactive trading laboratory!
 • 
15 users online

Quantacula Help

How-To
C# API Reference
Extensions
Development Blog
API-Extensions
Indicator Spotlights

QCommunity Extensions
The open-source GitHub repository of source code for the QCommunity Extensions library. Contains indicators and other extensions submitted by the Quantacula Community. Look for QCommunity indicators when you create a Building Block model, mark the "QCommunity" library check box to expose them.

TASC-Extensions
The open-source GitHub repository of source code for the TASCExtensions Quantacula extension. Contains indicators and other extensions adapted from the Traders' Tips articles in Technical Analysis of Stocks & Commodities magazine.
TrendLine
Namespace: Quantacula.Core
Parent: Object

The TrendLine class represents a trend line within a time series data source. It expresses two points via the properties Index1, Value1 and Index2, Value2. The PeakTroughCalculator contains methods that return TrendLine instances based on the generated peaks and troughs.

Constructor

TrendLine
public TrendLine(List<PeakTrough> pts)
public TrendLine(int idx1, double val1, int idx2, double val2)

The first constructor creates a TrendLine instance based on a list of PeakTroughs. The resulting TrendLine is created by taking a linear regression of the PeakTroughs to obtain the best fitting line. The minimum number of PeakTroughs required for this constructor is 4. This constructor is used by PeakTroughCalculator in its GetLowerTrendLine and GetUpperTrendLine methods.

The second constructor creates a TrendLine based on the specific start and end indices and values specified in the parameters.



Members

Deviation
public double Deviation

The average distance (expressed as percentage) of each of the peak/troughs used to create the Trendline are away from the Trendline's line. The Trendline's line is computed by calculating a linear regression of a series of consecutive peaks or troughs. The lower the Deviation, the closer the Trendline fits to the points used to generate it.

Example Code
using Quantacula.Backtest;
using Quantacula.Core;
using Quantacula.Indicators;
using System.Drawing;

namespace Quantacula
{
	public class MyModel1 : UserModelBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//control variables
			double swingPct = 3.0;

			//plot ZigZagHL indicator, it's based on the peak/troughs we're using
			ZigZagHL zz = new ZigZagHL(bars, swingPct, PeakTroughReversalTypes.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalTypes.Percent);

			//get bottom trendline
			TrendLine bottom = ptc.GetLowerTrendLine(bars.Count - 1, 4);
			if (bottom == null)
				return;
			DrawLine(bottom.Index1, bottom.Value1, bottom.Index2, bottom.Value2, Color.Red, LineStyles.Dashed);

			//get upper trendline
			TrendLine top = ptc.GetUpperTrendLine(bars.Count - 1, 4);
			if (top == null)
				return;
			DrawLine(top.Index1, top.Value1, top.Index2, top.Value2, Color.Green, LineStyles.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, Color.Red, LineStyles.Thick);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), Color.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), Color.Black, 12);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

ExtendTo
public double ExtendTo(int idx)

Extends a Trendline to an arbitrary point on the X-Axis (idx), and returns the Y-Axis value of the Trendline at that point.

Example Code
using Quantacula.Backtest;
using Quantacula.Core;
using Quantacula.Indicators;
using System.Drawing;

namespace Quantacula
{
	public class MyModel1 : UserModelBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//control variables
			double swingPct = 3.0;

			//plot ZigZagHL indicator, it's based on the peak/troughs we're using
			ZigZagHL zz = new ZigZagHL(bars, swingPct, PeakTroughReversalTypes.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalTypes.Percent);

			//get bottom trendline
			TrendLine bottom = ptc.GetLowerTrendLine(bars.Count - 1, 4);
			if (bottom == null)
				return;
			DrawLine(bottom.Index1, bottom.Value1, bottom.Index2, bottom.Value2, Color.Red, LineStyles.Dashed);

			//get upper trendline
			TrendLine top = ptc.GetUpperTrendLine(bars.Count - 1, 4);
			if (top == null)
				return;
			DrawLine(top.Index1, top.Value1, top.Index2, top.Value2, Color.Green, LineStyles.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, Color.Red, LineStyles.Thick);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), Color.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), Color.Black, 12);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

Index1
public int Index1

The index into the underlying source data (TimeSeries) of the first (left) point of the trendline.

Example Code
using Quantacula.Backtest;
using Quantacula.Core;
using Quantacula.Indicators;
using System.Drawing;

namespace Quantacula
{
	public class MyModel1 : UserModelBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//control variables
			double swingPct = 3.0;

			//plot ZigZagHL indicator, it's based on the peak/troughs we're using
			ZigZagHL zz = new ZigZagHL(bars, swingPct, PeakTroughReversalTypes.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalTypes.Percent);

			//get bottom trendline
			TrendLine bottom = ptc.GetLowerTrendLine(bars.Count - 1, 4);
			if (bottom == null)
				return;
			DrawLine(bottom.Index1, bottom.Value1, bottom.Index2, bottom.Value2, Color.Red, LineStyles.Dashed);

			//get upper trendline
			TrendLine top = ptc.GetUpperTrendLine(bars.Count - 1, 4);
			if (top == null)
				return;
			DrawLine(top.Index1, top.Value1, top.Index2, top.Value2, Color.Green, LineStyles.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, Color.Red, LineStyles.Thick);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), Color.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), Color.Black, 12);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

Index2
public int Index2

The index into the underlying source data (TimeSeries) of the second (right) point of the trendline.

Example Code
using Quantacula.Backtest;
using Quantacula.Core;
using Quantacula.Indicators;
using System.Drawing;

namespace Quantacula
{
	public class MyModel1 : UserModelBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//control variables
			double swingPct = 3.0;

			//plot ZigZagHL indicator, it's based on the peak/troughs we're using
			ZigZagHL zz = new ZigZagHL(bars, swingPct, PeakTroughReversalTypes.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalTypes.Percent);

			//get bottom trendline
			TrendLine bottom = ptc.GetLowerTrendLine(bars.Count - 1, 4);
			if (bottom == null)
				return;
			DrawLine(bottom.Index1, bottom.Value1, bottom.Index2, bottom.Value2, Color.Red, LineStyles.Dashed);

			//get upper trendline
			TrendLine top = ptc.GetUpperTrendLine(bars.Count - 1, 4);
			if (top == null)
				return;
			DrawLine(top.Index1, top.Value1, top.Index2, top.Value2, Color.Green, LineStyles.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, Color.Red, LineStyles.Thick);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), Color.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), Color.Black, 12);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

IsFalling
public bool IsFalling

Returns true if the trend line's starting point is less than its ending point.


IsRising
public bool IsRising

Returns true if the trend line's starting point is greater than its ending point.


Value1
public double Value1

The Y-Axis value of the first (left) point of the trendline.

Example Code
using Quantacula.Backtest;
using Quantacula.Core;
using Quantacula.Indicators;
using System.Drawing;

namespace Quantacula
{
	public class MyModel1 : UserModelBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//control variables
			double swingPct = 3.0;

			//plot ZigZagHL indicator, it's based on the peak/troughs we're using
			ZigZagHL zz = new ZigZagHL(bars, swingPct, PeakTroughReversalTypes.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalTypes.Percent);

			//get bottom trendline
			TrendLine bottom = ptc.GetLowerTrendLine(bars.Count - 1, 4);
			if (bottom == null)
				return;
			DrawLine(bottom.Index1, bottom.Value1, bottom.Index2, bottom.Value2, Color.Red, LineStyles.Dashed);

			//get upper trendline
			TrendLine top = ptc.GetUpperTrendLine(bars.Count - 1, 4);
			if (top == null)
				return;
			DrawLine(top.Index1, top.Value1, top.Index2, top.Value2, Color.Green, LineStyles.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, Color.Red, LineStyles.Thick);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), Color.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), Color.Black, 12);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

Value2
public double Value2

The Y-Axis value of the second (right) point of the trendline.

Example Code
using Quantacula.Backtest;
using Quantacula.Core;
using Quantacula.Indicators;
using System.Drawing;

namespace Quantacula
{
	public class MyModel1 : UserModelBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//control variables
			double swingPct = 3.0;

			//plot ZigZagHL indicator, it's based on the peak/troughs we're using
			ZigZagHL zz = new ZigZagHL(bars, swingPct, PeakTroughReversalTypes.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalTypes.Percent);

			//get bottom trendline
			TrendLine bottom = ptc.GetLowerTrendLine(bars.Count - 1, 4);
			if (bottom == null)
				return;
			DrawLine(bottom.Index1, bottom.Value1, bottom.Index2, bottom.Value2, Color.Red, LineStyles.Dashed);

			//get upper trendline
			TrendLine top = ptc.GetUpperTrendLine(bars.Count - 1, 4);
			if (top == null)
				return;
			DrawLine(top.Index1, top.Value1, top.Index2, top.Value2, Color.Green, LineStyles.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, Color.Red, LineStyles.Thick);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), Color.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), Color.Black, 12);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}