PeakTrough
Namespace: WealthLab.Core
Parent: Object

The PeakTrough class represents a peak or trough that was detected in time series data by the PeakTroughCalculator utility class.

Members
DetectedAtIndex
public int DetectedAtIndex

The index where the peak or trough was actually detected. Since peaks and troughs are detected after reversals, this will always be later than the actual index of the peak or trough apex/nadir itself.

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

namespace WealthScript
{
	public class DetectedAtIndexExample : 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 = 5.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);

			//1. color the detection bar
			//2. draw a line back to the pt bar
			//3. calculate the % change from the pt to the current close
			foreach (PeakTrough pt in ptc.PeakTroughs)
			{
				int daiBar = pt.DetectedAtIndex;
				SetBarColor(bars, daiBar, Color.Blue);
				DrawLine(daiBar, bars.Close[daiBar], pt.XIndex, bars.Close[daiBar], Color.Blue, 2);

				double change = bars.Close[daiBar] / pt.YValue - 1;
				double bump = pt.Type == PeakTroughTypes.Peak ? 0.995 : 1.005;
				DrawText(change.ToString("0.00%"), pt.XIndex, bars.Close[daiBar] * bump, Color.Black, 10);
			}

		}

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

PeakTroughIndex
public int PeakTroughIndex

The index of the peak's actual apex or the trough's actual nadir.

Remarks

  • Property XIndex returns Index and can be used interchangeably.
Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthScript3
{
	public class PeakTroughIndexExample : UserStrategyBase
	{
		PeakTroughCalculator _ptc;

		public override void Initialize(BarHistory bars)
		{
			//control variables
			double swingPct = 5.0;

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

			foreach (PeakTrough pt in _ptc.PeakTroughs)
			{
				//highlight where the peaks and troughs occurred
				SetBackgroundColor(bars, pt.PeakTroughIndex, Color.FromArgb(40, Color.Blue));

				//add a line to detection bar
				DrawLine(pt.PeakTroughIndex, pt.Value, pt.DetectedAtIndex, pt.Value, Color.Purple, 2);
			}
		}

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

Type
public PeakTroughTypes Type

Specifies whether the object represents a PeakTroughTypes.Peak or a PeakTroughTypes.Trough.

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

namespace WealthScript
{
	public class PeakTroughTypesExample : 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 = 5.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);

			//color detection bar based on type
			foreach (PeakTrough pt in ptc.PeakTroughs)
			{
				Color clr = pt.Type == PeakTroughTypes.Peak ? Color.Blue : Color.Fuchsia;
				SetBarColor(bars, pt.XIndex, clr);
			}			
		}

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

Value
public double Value

The apex value of the peak or nadir value of the trough.

Remarks

  • Property YValue returns Value and can be used interchangeably.
Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthScript
{
	public class PeakTroughValueExample : 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;

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

			//draw the zigzag lines ourselves
			PeakTrough lastPT = ptc.PeakTroughs[ptc.PeakTroughs.Count - 1];

			for (int i = ptc.PeakTroughs.Count - 2; i > 1; i--)
			{
				PeakTrough nextPT = ptc.PeakTroughs[i];
				DrawLine(lastPT.XIndex, lastPT.Value, nextPT.XIndex, nextPT.Value, Color.Blue, 2);
				lastPT = nextPT;
			}

		}

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