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

The TrendLine class represents a line defined by two index/value coordinates. TrendLine instances are commonly generated by PeakTroughCalculator methods such as GetLowerTrendLine and GetUpperTrendLine, but you can also create one directly from two arbitrary points. When constructed from a collection of PeakTrough instances, the TrendLine represents the best-fit linear regression line through those points rather than simply connecting the first and last PeakTrough.

Constructors
TrendLine
public TrendLine(
List<PeakTrough> pts,
bool semiLog = false)
public TrendLine(
int idx1,
double val1,
int idx2,
double val2)

The first constructor creates a TrendLine from a collection of PeakTrough instances. The PeakTroughs are first sorted by XIndex. WealthLab then calculates a best-fit regression line through the points. If semiLog is false, a standard linear best-fit calculation is used. If semiLog is true, a logarithmic Y-axis best-fit calculation is used. The resulting TrendLine's Index1 and Index2 correspond to the X indices of the first and last PeakTroughs. Value1 and Value2 contain the regression line's values at those two indices. The constructor also calculates Deviation, which measures how closely the supplied PeakTroughs fit the regression line. The second constructor creates a TrendLine directly from the two supplied coordinates. In this case, Deviation remains at its default value of 0.



Members
Deviation
public double Deviation

Returns the average percentage distance between the PeakTrough points used to construct the TrendLine and the calculated best-fit regression line. For each PeakTrough, WealthLab calculates:

Abs(PeakTroughValue - RegressionValue) * 100 / PeakTroughValue

Deviation is the average of these percentage differences. A lower Deviation indicates that the PeakTroughs lie more closely along the calculated TrendLine. When the TrendLine is created directly from two index/value coordinates, Deviation defaults to 0.


ExtendTo
public double ExtendTo(
int idx,
bool useLog = false)

Extends the TrendLine to an arbitrary X-axis index and returns the corresponding Y-axis value. Set useLog to true to extend the line using logarithmic Y-axis calculations. Normally, useLog should correspond to the type of TrendLine being represented. For example, if the TrendLine was constructed using semiLog: true, use logarithmic extension as well.


Index1
public int Index1

The X-axis index of the TrendLine's first, or left, point. For a TrendLine created from PeakTroughs, this corresponds to the XIndex of the earliest supplied PeakTrough.


Index2
public int Index2

The X-axis index of the TrendLine's second, or right, point. For a TrendLine created from PeakTroughs, this corresponds to the XIndex of the latest supplied PeakTrough.


IsFalling
public bool IsFalling

Returns true if the TrendLine is falling from left to right. Equivalent to:

Value1 > Value2

Returns false for a flat or rising TrendLine.


IsRising
public bool IsRising

Returns true if the TrendLine is rising from left to right. Equivalent to:

Value2 > Value1

Returns false for a flat or falling TrendLine.


Slope
public double Slope(bool useLog = false)

Returns the slope of the TrendLine. For a normal linear TrendLine, the slope is calculated as:

(Value2 - Value1) / (Index2 - Index1)

If useLog is true, the logarithmic slope is calculated as:

Log10(Value2 / Value1) / (Index2 - Index1)

The logarithmic form is useful when working with semi-logarithmic price charts or TrendLines constructed with semiLog: true.


ToString
public override string ToString()

Returns a string representation of the TrendLine containing its two coordinates. The format is:

[Index1;Value1][Index2;Value2]

The values are formatted to eight decimal places.


Value1
public double Value1

The Y-axis value of the TrendLine's first, or left, point. When the TrendLine is constructed from PeakTroughs, this is the best-fit regression value at Index1, not necessarily the actual value of the first PeakTrough.


Value2
public double Value2

The Y-axis value of the TrendLine's second, or right, point. When the TrendLine is constructed from PeakTroughs, this is the best-fit regression value at Index2, not necessarily the actual value of the final PeakTrough.