Search Framework:
PeakTrough
Namespace: WealthLab.Indicators
Parent: Object

The PeakTrough class represents a peak or trough detected in time series data by the PeakTroughCalculator utility class. It implements the IChartPoint interface, so its XIndex and YValue properties can be used wherever chart points are accepted.

Members
DetectedAtIndex
public int DetectedAtIndex

The index where the peak or trough was actually detected. Because a peak or trough can only be confirmed after a reversal occurs, DetectedAtIndex will generally be later than the actual peak or trough index represented by PeakTroughIndex.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
namespace WealthScript
{
    public class DetectedAtIndexExample : UserStrategyBase
    {
        public override void Initialize(BarHistory bars)
        {
            double swingPct = 5.0;
            ZigZagHL zz = new ZigZagHL(
                bars,
                swingPct,
                PeakTroughReversalType.Percent,
                false);
            PlotIndicator(zz);
            PeakTroughCalculator ptc = new PeakTroughCalculator(
                bars,
                swingPct,
                PeakTroughReversalType.Percent);
            foreach (PeakTrough pt in ptc.PeakTroughs)
            {
                int detectionBar = pt.DetectedAtIndex;
                SetBarColor(bars, detectionBar, WLColor.Blue);
                DrawLine(
                    detectionBar,
                    bars.Close[detectionBar],
                    pt.XIndex,
                    bars.Close[detectionBar],
                    WLColor.Blue,
                    2);
                double change = bars.Close[detectionBar] / pt.YValue - 1.0;
                double bump = pt.Type == PeakTroughType.Peak ? 0.995 : 1.005;
                DrawText(
                    change.ToString("0.00%"),
                    pt.XIndex,
                    bars.Close[detectionBar] * bump,
                    WLColor.Black,
                    10);
            }
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

PeakTroughIndex
public int PeakTroughIndex

The index of the actual apex of a peak or nadir of a trough. The XIndex property returns the same value and can be used interchangeably.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
namespace WealthScript
{
    public class PeakTroughIndexExample : UserStrategyBase
    {
        public override void Initialize(BarHistory bars)
        {
            double swingPct = 5.0;
            PeakTroughCalculator ptc = new PeakTroughCalculator(
                bars,
                swingPct,
                PeakTroughReversalType.Percent);
            foreach (PeakTrough pt in ptc.PeakTroughs)
            {
                SetBackgroundColor(
                    bars,
                    pt.PeakTroughIndex,
                    WLColor.FromArgb(40, WLColor.Blue));
                DrawLine(
                    pt.PeakTroughIndex,
                    pt.Value,
                    pt.DetectedAtIndex,
                    pt.Value,
                    WLColor.Purple,
                    2);
            }
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

TrendLineCache
public Dictionary<string, TrendLine> TrendLineCache

Contains a Dictionary of previously calculated TrendLine instances, keyed by string. This cache can be used to associate calculated trendlines with the PeakTrough and avoid recalculating them.


Type
public PeakTroughType Type

Specifies whether this instance represents a PeakTroughType.Peak or a PeakTroughType.Trough.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
namespace WealthScript
{
    public class PeakTroughTypeExample : UserStrategyBase
    {
        public override void Initialize(BarHistory bars)
        {
            double swingPct = 5.0;
            ZigZagHL zz = new ZigZagHL(
                bars,
                swingPct,
                PeakTroughReversalType.Percent,
                false);
            PlotIndicator(zz);
            PeakTroughCalculator ptc = new PeakTroughCalculator(
                bars,
                swingPct,
                PeakTroughReversalType.Percent);
            foreach (PeakTrough pt in ptc.PeakTroughs)
            {
                WLColor color = pt.Type == PeakTroughType.Peak
                    ? WLColor.Blue
                    : WLColor.Fuchsia;
                SetBarColor(bars, pt.XIndex, color);
            }
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

Value
public double Value

The apex value of a peak or the nadir value of a trough. The YValue property returns the same value and can be used interchangeably.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
namespace WealthScript
{
    public class PeakTroughValueExample : UserStrategyBase
    {
        public override void Initialize(BarHistory bars)
        {
            double swingPct = 3.0;
            PeakTroughCalculator ptc = new PeakTroughCalculator(
                bars,
                swingPct,
                PeakTroughReversalType.Percent);
            if (ptc.PeakTroughs.Count < 2)
                return;
            PeakTrough lastPT =
                ptc.PeakTroughs[ptc.PeakTroughs.Count - 1];
            for (int i = ptc.PeakTroughs.Count - 2; i >= 0; i--)
            {
                PeakTrough nextPT = ptc.PeakTroughs[i];
                DrawLine(
                    lastPT.XIndex,
                    lastPT.Value,
                    nextPT.XIndex,
                    nextPT.Value,
                    WLColor.Blue,
                    2);
                lastPT = nextPT;
            }
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

XIndex
public int XIndex

Implements IChartPoint and returns PeakTroughIndex, the bar index of the peak or trough itself.


YValue
public double YValue

Implements IChartPoint and returns Value, the value of the peak or trough.