Syntax
public static int CheckExtendedTrendline(this WealthScript obj, int bar, ChartPane pane, DataSeries ds, int bar1, int bar2, Color linecolor, int thickness, int consecBars = 1)
public int CheckExtendedTrendline(int bar, ChartPane pane, DataSeries ds, int bar1, int bar2, Color linecolor, int thickness, int consecBars = 1)
Parameter Description
bar | Bar number on which to test the trendline extension for crossing |
pane | The ChartPane where the DataSeries ds is plotted. The Log property of this pane is used to draw the extension properly for the scale. |
ds | The DataSeries whose peaks or troughs define a trendline |
bar1 | The bar of the first (earliest) peak or trough on which the trendline originates |
bar2 | The bar of the second (later) peak or trough |
linecolor | The color to use to draw the trendline |
thickness | Thickness of the line |
consecBars | Optional: the number of consecutive bars that ds must be across the trendline to end and draw it |
Description
This function, created by
Robert Sucher, draws the trendline and its extension with respect to the log scale of the plotted pane. Returns the bar on which ds crosses the trendline for the specified number of consecutive bars, otherwise 0 for no crossing. The draw occurs only once: when the current bar is Bars.count - 1 or on the bar when the trendline is actually crossed.
Important!: After identifying the initial trendline, this method assumes that it is called for each bar until the end of the chart or until the crossing condition is true.
Example
Below is a demo Strategy that shows how to use the function.
Example using C# extension methods:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
namespace WealthLab.Strategies
{
public class ExtendTrendlinesExample : WealthScript
{
StrategyParameter _pctpeaks;
public ExtendTrendlinesExample()
{
_pctpeaks = CreateParameter("Peaks %", 5, 1, 20, 1);
}
protected override void Execute()
{
// Find the last two n% peaks and draw a line to the end of the chart
int n = Bars.Count - 1;
int pb2 = (int)PeakBar.Value(n, High, _pctpeaks.Value, WealthLab.Indicators.PeakTroughMode.Percent);
int pb1 = pb2;
if (pb2 > -1)
{
pb1 = (int)PeakBar.Value(pb2, High, _pctpeaks.Value, WealthLab.Indicators.PeakTroughMode.Percent);
DrawCircle(PricePane, 8, pb2, Highpb2, Color.Blue, LineStyle.Solid, 2, false);
}
if ((pb2 == -1) || (pb1 == -1))
{
DrawLabel(PricePane, "Could not find two " + _pctpeaks.Value.ToString() + "% peaks", Color.Red);
DrawLabel(PricePane, "Try decreasing the Peaks % value", Color.Red);
return;
}
// The trendline is defined. Check if it's crossed...
DrawCircle(PricePane, 8, pb1, Highpb1, Color.Blue, LineStyle.Solid, 2, false);
int consecBarsCrossedTrendline = 2;
for (int bar = pb2 + 1; bar < Bars.Count; bar++)
{
int crossbar = this.CheckExtendedTrendline(bar, PricePane, High, pb1, pb2, Color.Purple, 2, consecBarsCrossedTrendline);
if (crossbar > 0)
{
string msg = Bars.Symbol + ", crossed " + consecBarsCrossedTrendline + " consecutive times at bar: " + crossbar;
DrawLabel(PricePane, msg);
break;
}
}
}
}
}