Your interactive trading laboratory!
 • 
10 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.
New Trendline Functionality
Published by Q Glitch on 10/10/2019

Quantacula Studio Q191 includes new methods in the framework to generate and work with trendlines. The new functionality is built into the utility class PeakTroughCalculator. As its name implies, PeakTroughCalculator generates a series of peaks and troughs based on TimeSeries source data, and a specified reversal type and amount.

Once we have a collection of peaks and troughs, we can do many useful things, such as detect chart patterns, and generate trendlines. We added two new methods to PeakTroughCalculator:

  • GetLowerTrendLine
  • GetUpperTrendLine

The methods return an instance of the new Trendline class. Each method takes two parameters:

  • idx (int) - the index into the source data pointing to where you want the trendline identified.
  • numTroughs/numPeaks (int) - the number of troughs (for lower trendlines) or peaks (for upper trendlines) to use to generate the trendline.

The resulting TrendLine object has properties that return the positions of the trendline's two points (Index1, Value1), (Index2, Value2). It also has a useful property named Deviation, which expresses how well the trendline fits to the troughs/peaks that generated it. It is calculated by taking the average percentage distance each trough/peak is from the generated trendline. We'll likely add more helper properties to Trendline in subsequent releases to make it easier to work with its slope, angle, etc.

Putting this all together, here is some sample code that does the following:

  • Creates and plots a ZigZagHL indicator so we can see the peaks and troughs we'll be working with.
  • Creates an instance of a PeakTroughCalculator using a reversal amount of 3%.
  • Generates and plots the most recent upper and lower Trendline.
  • Displays the two Trendlines' Deviation at the upper left side of the chart.
  • Uses the Trendline's ExtendTo method to extend the lower trendline to the right edge of the chart, and plots the extension using a thick line.

Below the sample code, you can see a chart of AON, where the peaks, troughs, and trendlines highlight a wedge chart pattern.

using QuantaculaBacktest;
using QuantaculaCore;
using QuantaculaIndicators;
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);

		//extend lower trendline to end of chart
		double y = bottom.ExtendTo(bars.Count - 1);
		DrawLine(bottom.Index2, bottom.Value2, bars.Count - 1, y, Color.Red, LineStyles.Thick);

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