Your interactive trading laboratory!
10 users online

Quantacula Help

C# API Reference
Development Blog
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.

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.
Working with Trend Lines in Code
Published by Q Glitch on 11/21/2019

In this article we'll focus on how to work with trend lines in a C# coded Quantacula Model. The Quantacula framework includes a class called TrendLine in the QuantaculaCore library that is helpful. Here we'll be using TrendLine functionality that was added as of Q194.

Let's use the following code as an example, and then walk through what it's doing step by step. All of this Model's code is contained in the Initialize method.

public override void Initialize(BarHistory bars)
	//plot the zigzag scheme
	ZigZagHL zz = new ZigZagHL(bars, 5, PeakTroughReversalTypes.Percent, false);
	PlotIndicator(zz, Color.FromArgb(32, zz.Color));

	//find the most recent two troughs
	PeakTroughCalculator ptc = new PeakTroughCalculator(bars, 5, PeakTroughReversalTypes.Percent);
	if (ptc.PeakTroughs.Count < 4)
	PeakTrough trough1 = ptc.GetTrough(bars.Count - 1);
	PeakTrough trough2 = ptc.GetTrough(trough1.PeakTroughIndex);
	//annotate them on the chart
	DrawBarAnnotation("T1", trough1.PeakTroughIndex, false, Color.Maroon, 12);
	DrawBarAnnotation("T2", trough2.PeakTroughIndex, false, Color.Maroon, 12);

	//create a trendline connecting the two troughs
	TrendLine tl = new TrendLine(trough2.PeakTroughIndex, trough2.Value, trough1.PeakTroughIndex, trough1.Value);

	//plot it, extending right
	DrawLine(tl.Index1, tl.Value1, tl.Index2, tl.Value2, Color.Maroon, LineStyles.Thick, "Price", false, true);

	//show how to obtain a trendline value at any point on the chart, by pushing those values into a TimeSeries
	TimeSeries tsTrendLine = new TimeSeries(bars.DateTimes);
	for (int n = tl.Index1; n < bars.Count; n++)
		double tlValue = tl.ExtendTo(n);
		tsTrendLine[n] = tlValue;
	PlotTimeSeries(tsTrendLine, "TrendLine Values", "TL", Color.Peru, PlotStyles.ThickLine);

Plotting a ZigZag

The first section of code creates and plots a ZigZagHL indicator. The indicator isn't used in the rest of the code, it's just included to help visualize the peaks and troughs we will be basing things on.

What is a Trend Line?

A trend line should connect two or more important points on the chart. There are numerous ways to identify potentially important points. In this example, we look for two troughs in the chart where prices rose 5% or more from a bottom.

To easily get the list of troughs (and peaks for that matter) based on this criteria, we create an instance of the PeakTroughCalculator class. In the constructor, you pass the source data (bars), the desired reversal amount (5) and the reversal type (either percent or point). After it's constructed, the PeakTroughCalculator will contain a list of the peaks and troughs that occurred in the data based on those specifications. They are expressed as instances of the PeakTrough class, and made available in the PeakTroughs property, which is a List<PeakTrough>.

Getting the Troughs

We use a helper method of PeakTroughCalculator to get the two most recent troughs. The method is:

PeakTrough GetTrough(int idx)

GetTrough returns the most recent trough that was detected as of the specified index into the source data. We first call GetTrough passing the index of the last bar of data to get the most recent trough. Then we call GetTrough again, this time passing the index of the trough that we obtained previously. This gives us the trough that was detected immediately prior that one.

We now have the two most recent troughs upon which we can base a trend line.

Create and Plot a Trend Line

We'll use the aforementioned TrendLine class to help us out here. The next section of code creates a TrendLine instance, passing the index (which represents the X-Axis) and the value (Y-Axis) of the two most recent troughs as constructor parameters.

Next, the code calls DrawLine to plot the line on the chart. The last two parameters of DrawLine control whether to extend the plotted line left and/or right as it's plotted. We pass true in the last parameter, causing the drawn line to be extended to the right edge of the chart.

Tip: When working with C# coded Models, you can click a class or method name in the code to bring it up in the integrated QDocs. In QDocs you can read details and examples about how to use the various elements of the Quantacula C# framework.

Accessing Trend Line Values

Now that we have a TrendLine instance, it's easy to access the values of the trend line at any point in the historical data. Use the TrendLine method ExtendTo to return a trend line value at any arbitrary index on the chart.

To illustrate this point, the example code create a new empty TimeSeries named tsTrendLine, then runs through a loop that begins at the trend line's start point and ends at the last bar of historical data. It calls ExtendTo for each index, and pushes that value into the TimeSeries. The code then calls PlotTimeSeries to plot the resulting values in a new chart pane.


There you have it, an easy way to create, plot, and interrogate trend lines in your C# coded models. This can be the basis for more sophisticated extensions such as chart pattern recognition or calculation of support/resistance levels.

enter image description here