Search Framework:
PeakTrough
Namespace: WealthLab.Indicators
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, PeakTroughReversalType.Percent, false);
			PlotIndicator(zz);

			//calculate peaks and troughs based on high/lows
			PeakTroughCalculator ptc = new PeakTroughCalculator(bars, swingPct, PeakTroughReversalType.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, WLColor.Blue);
				DrawLine(daiBar, bars.Close[daiBar], pt.XIndex, bars.Close[daiBar], WLColor.Blue, 2);

				double change = bars.Close[daiBar] / pt.YValue - 1;
				double bump = pt.Type == PeakTroughType.Peak ? 0.995 : 1.005;
				DrawText(change.ToString("0.00%"), pt.XIndex, bars.Close[daiBar] * bump, WLColor.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, PeakTroughReversalType.Percent);

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

				//add a line to detection bar
				DrawLine(pt.PeakTroughIndex, pt.Value, pt.DetectedAtIndex, pt.Value, WLColor.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 PeakTroughType Type

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

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

namespace WealthScript
{
	public class PeakTroughTypeExample : 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, PeakTroughReversalType.Percent, false);
			PlotIndicator(zz);

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

			//color detection bar based on type
			foreach (PeakTrough pt in ptc.PeakTroughs)
			{
				WLColor clr = pt.Type == PeakTroughType.Peak ? WLColor.Blue : WLColor.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, PeakTroughReversalType.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, WLColor.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)
		{
		}
	}
}