Search Framework:
TrendLine
Namespace: WealthLab.Indicators
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.

Constructors
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 WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthLab
{
	public class MyStrategy1 : UserStrategyBase
	{
		//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, PeakTroughReversalType.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalType.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, WLColor.Red, 2, LineStyle.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, WLColor.Green, 2, LineStyle.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, WLColor.Red, 2, LineStyle.Solid);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), WLColor.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), WLColor.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, bool useLog = false)

Extends a Trendline to an arbitrary point on the X-Axis (idx), and returns the Y-axis value of the Trendline at that point. Pass true for useLog if the line is to be extended for Y-axis log scaling.

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

namespace WealthLab
{
	public class MyStrategy1 : UserStrategyBase
	{
		//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, PeakTroughReversalType.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalType.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, WLColor.Red, 2, LineStyle.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, WLColor.Green, 2, LineStyle.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, WLColor.Red, 2, LineStyle.Solid);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), WLColor.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), WLColor.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 WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthLab
{
	public class MyStrategy1 : UserStrategyBase
	{
		//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, PeakTroughReversalType.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalType.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, WLColor.Red, 2, LineStyle.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, WLColor.Green, 2, LineStyle.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, WLColor.Red, 2, LineStyle.Solid);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), WLColor.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), WLColor.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 WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthLab
{
	public class MyStrategy1 : UserStrategyBase
	{
		//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, PeakTroughReversalType.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalType.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, WLColor.Red, 2, LineStyle.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, WLColor.Green, 2, LineStyle.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, WLColor.Red, 2, LineStyle.Solid);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), WLColor.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), WLColor.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.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;
using System;

namespace WealthLab
{
	public class FallingTroughs : UserStrategyBase
	{
		SMA _sma;
		PeakTroughCalculator _ptc;
		List<string> _usedLines;

		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//control variables
			StartIndex = 200;
			double swingPct = 5.0;

			_usedLines = new List<string>();

			_sma = SMA.Series(bars.Close, 200);
			PlotIndicatorLine(_sma);

			//plot ZigZagHL indicator, it's based on the peak/troughs we're using
			ZigZagHL zz = new ZigZagHL(bars, swingPct, PeakTroughReversalType.Percent, false);
			PlotIndicatorLine(zz, WLColor.Blue, 1);

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

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//buy at or below the 200 dma if the last 2 troughs are falling
				TrendLine bottom = _ptc.GetLowerTrendLine(idx, 2);

				if (bottom != null && bottom.IsFalling)
				{
					string lineKey = String.Empty;

					//don't buy this dip more than once					
					lineKey = bottom.Index1.ToString() + "," + bottom.Index2.ToString();

					if (!_usedLines.Contains(lineKey))
					{
						_usedLines.Add(lineKey);
						DrawLine(bottom.Index1, bottom.Value1, bottom.Index2, bottom.Value2, WLColor.Green, 2, LineStyle.Dashed);

						//essential a buy at market if price is below the sma
						PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, _sma[idx]);
					}
				}
			}
			else
			{
				Position p = LastPosition;

				//sell after 5 bars
				if(idx + 1 - p.EntryBar >= 5)
					ClosePosition(p, OrderType.Market);
			}
		}
	}
}

IsRising
public bool IsRising

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

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;
using System;

namespace WealthLab
{
	public class RisingPeaks : UserStrategyBase
	{
		SMA _sma;
		PeakTroughCalculator _ptc;
		List<string> _usedLines;

		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//control variables
			StartIndex = 200;
			double swingPct = 5.0;

			_usedLines = new List<string>();

			_sma = SMA.Series(bars.Close, 200);
			PlotIndicatorLine(_sma);

			//plot ZigZagHL indicator, it's based on the peak/troughs we're using
			ZigZagHL zz = new ZigZagHL(bars, swingPct, PeakTroughReversalType.Percent, false);
			PlotIndicatorLine(zz, WLColor.Blue, 1);

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

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//buy at the 200 dma if the last 2 peaks are rising
				TrendLine top = _ptc.GetUpperTrendLine(idx, 2);

				if (top != null && top.IsRising)
				{
					string lineKey = String.Empty;

					//don't buy this dip more than once					
					lineKey = top.Index1.ToString() + "," + top.Index2.ToString();

					if (!_usedLines.Contains(lineKey))
					{
						_usedLines.Add(lineKey);
						DrawLine(top.Index1, top.Value1, top.Index2, top.Value2, WLColor.Green, 2, LineStyle.Dashed);

						//essential a buy at market if price is below the sma
						PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, _sma[idx]);
					}
				}
			}
			else
			{
				Position p = LastPosition;

				//sell after 5 bars
				if(idx + 1 - p.EntryBar >= 5)
					ClosePosition(p, OrderType.Market);
			}
		}
	}
}

Value1
public double Value1

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

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

namespace WealthLab
{
	public class MyStrategy1 : UserStrategyBase
	{
		//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, PeakTroughReversalType.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalType.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, WLColor.Red, 2, LineStyle.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, WLColor.Green, 2, LineStyle.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, WLColor.Red, 2, LineStyle.Solid);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), WLColor.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), WLColor.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 WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthLab
{
	public class MyStrategy1 : UserStrategyBase
	{
		//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, PeakTroughReversalType.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalType.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, WLColor.Red, 2, LineStyle.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, WLColor.Green, 2, LineStyle.Dashed);

			//extend lower trendline to end of chart
			double y = bottom.ExtendTo(bars.Count - 1);
			DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, WLColor.Red, 2, LineStyle.Solid);

			//display deviations
			DrawHeaderText("Top Trendline Deviation: " + top.Deviation.ToString("N2"), WLColor.Black, 12);
			DrawHeaderText("Bottom Trendline Deviation: " + bottom.Deviation.ToString("N2"), WLColor.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)
		{
		}
	}
}