Your interactive trading laboratory!
 • 
21 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.
PeakTroughCalculator
Namespace: Quantacula.Core
Parent: Object

The PeakTroughCalculator is a utility class the calculates peaks and troughs in the source time series data. Peaks and troughs are calculated by observing when the data moves by a certain reversal amount, which can be expressed as either percent or point value. The result is a list of PeakTrough objects available via the PeakTroughs property.

Due to the nature of this calculation, the detection of peaks and troughs always happens a little after the fact. The resulting PeakTrough instances provide both the point at which the peak trough began (the PeakTroughIndex property) as well as the point at which it was detected (the DetectedAtIndex property).

Constructor

PeakTroughCalculator
public PeakTroughCalculator(BarHistory source, double reversal, PeakTroughReversalTypes reversalType = PeakTroughReversalTypes.Percent)
public PeakTroughCalculator(TimeSeries highs, TimeSeries lows, double reversalAmount, PeakTroughReversalTypes reversalType = PeakTroughReversalTypes.Percent)
public PeakTroughCalculator(TimeSeries source, double reversal, PeakTroughReversalTypes reversalType = PeakTroughReversalTypes.Percent)

The class supports three constructors.

  • The first takes a single BarHistory object, and the peaks and troughs are calculated based on high and low prices
  • The second takes two TimeSeries objects, which should be the desired highs and lows
  • The third takes a single TimeSeries object on which the peaks and troughs are calculated
Example Code
using Quantacula.Backtest;
using Quantacula.Core;
using System.Drawing;

namespace Quantacula
{
	public class MyModel1 : UserModelBase
	{
		//compare two methods of determining peak/troughs, using closing price only vs using highs/lows
		public override void Initialize(BarHistory bars)
		{
			PeakTroughCalculator ptClose = new PeakTroughCalculator(bars.Close, 5.0, PeakTroughReversalTypes.Percent);
			RenderPeakTroughs(ptClose, Color.Red);
			PeakTroughCalculator ptHighLow = new PeakTroughCalculator(bars, 5.0, PeakTroughReversalTypes.Percent);
			RenderPeakTroughs(ptHighLow, Color.Blue);
		}

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

		//render most recent peak/trough
		private void RenderPeakTroughs(PeakTroughCalculator ptc, Color color)
		{
			if (ptc.PeakTroughs.Count < 2)
				return;
			PeakTrough pt = ptc.PeakTroughs[ptc.PeakTroughs.Count - 1];
			PeakTrough pt2 = ptc.GetPeakTrough(pt.PeakTroughIndex);
			DrawLine(pt.PeakTroughIndex, pt.Value, pt2.PeakTroughIndex, pt2.Value, color, LineStyles.Thick, "Price");
		}
	}
}


Divergences

HasFallingPeaks
public bool HasFallingPeaks(int idx)

Returns true if the peak detected as of the specified index (idx) in the source data has a lower value than the previous peak.


HasFallingTroughs
public bool HasFallingTroughs(int idx)

Returns true if the trough detected as of the specified index (idx) in the source data has a lower value than the previous trough.


HasRisingPeaks
public bool HasRisingPeaks(int idx)

Returns true if the peak detected as of the specified index (idx) in the source data has a higher value than the previous peak.


HasRisingTroughs
public bool HasRisingTroughs(int idx)

Returns true if the trough detected as of the specified index (idx) in the source data has a higher value than the previous trough.


PeakState
public int PeakState(int idx)

As of the specified index (idx) in the source data, returns 1 if the most recently detected peak has a higher value than the previous peak, and -1 if it has a lower value. If the values are equal, or one or both peaks are not available, returns 0.


TroughState
public int TroughState(int idx)

As of the specified index (idx) in the source data, returns 1 if the most recently detected trough has a higher value than the previous trough, and -1 if it has a lower value. If the values are equal, or one or both troughs are not available, returns 0.



Peaks & Troughs

GetPeak
public PeakTrough GetPeak(int idx)

Returns the most recent peak (instance of the PeakTrough class) detected as of the specified index in the source data, or null if not available.


GetPeakTrough
public PeakTrough GetPeakTrough(int idx)

Returns the most recent peak or trough (instance of the PeakTrough class) detected as of the specified index in the source data, or null if not available.


GetTrough
public PeakTrough GetTrough(int idx)

Returns the most recent trough (instance of the PeakTrough class) detected as of the specified index in the source data, or null if not available.


PeakTroughs
public List<PeakTrough> PeakTroughs

Access the list of peaks and troughs that were generated. These are instances of the PeakTrough class.



Previous Peaks & Troughs

GetPrevPeak
public PeakTrough GetPrevPeak(PeakTrough pt)

Returns the first peak that occurred prior to the PeakTrough specified in the pt parameter, or null if not available.


GetPrevTrough
public PeakTrough GetPrevTrough(PeakTrough pt)

Returns the first trough that occurred prior to the PeakTrough specified in the pt parameter, or null if not available.



TrendLines

GetLowerTrendLine
public TrendLine GetLowerTrendLine(int idx, int numTroughs)

Returns the lower Trendline at the specified index (idx), calculated using the most recent numTroughs Troughs detected at that index.

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);

			//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)
		{
		}
	}
}

GetUpperTrendLine
public TrendLine GetUpperTrendLine(int idx, int numPeaks)

Returns the upper Trendline at the specified index (idx), calculated using the most recent numPeaks Peaks detected at that index.

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);

			//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)
		{
		}
	}
}