Syntax
public HTTrendLine(DataSeries ds, string description)
public static HTTrendLine(DataSeries ds)
Parameter Description
ds |
The source DataSeries, usually AverageSeries |
Description
The Hilbert Transform is a technique used to generate inphase and quadrature components of a de-trended real-valued "analytic-like" signal (such as a Price Series) in order to analyze variations of the instantaneous phase and amplitude. HTTrendline (or MESA Instantaneous Trendline) returns the Price Series value after the Dominant Cycle of the analytic signal as generated by the Hilbert Transform has been removed. The Dominant Cycle can be thought of as being the "most likely" period (in the range of 10 to 40) of a sine function of the Price Series.
Interpretation
The HTTrendline at a specific bar gives the current Hilbert Transform Trendline as instantaneously measured at that bar. In its Series form, the Instantaneous Trendline appears much like a Moving Average, but with minimal lag compared with the lag normally associated with such averages for equivalent periods. The HTTrendline is formed by removing the Dominant Cycle from the Price Series. See the examples.
Calculation
More detailed information concerning the calculation of the Hilbert Transform related functions can be found on the
Mesa Software site.
The basic flow and simplified pseudo code for the computation for the Dominant Cycle Period is:
Compute the Hilbert Transform
{Detrend Price}
{Compute InPhase and Quadrature components}
Compute the Period of the Dominant Cycle
{Use ArcTangent to compute the current phase}
{Resolve the ArcTangent ambiguity}
{Compute a differential phase, resolve phase wraparound, and limit delta phase errors}
{Sum DeltaPhases to reach 360 degrees. The sum is the instantaneous period.}
{Resolve Instantaneous Period errors and smooth}
Compute the Instantaneous Trendline
{Average over the period of the Dominant Cycle at each bar} Return the Hilbert Transform Trendline measured at the current bar
Example
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community.Indicators;
namespace WealthLab.Strategies
{
public class MyStrategy : WealthScript
{
protected override void Execute()
{
/*
Example - Another method of determining a trendual period
using the HTTrendline is shown in the chartscript
RocketScience, v1, by soulaerius.
Here's a small extract of that script in which a "significant" deviation of the smoothed price,
determined by an Optimization Variable, from the Trendline is used to declare trend mode.
*/
int trendfilter = (int)(2 / 100.0);
// compute dominant cycle phase
WMA SmoothPrice = WMA.Series( Close, 4 );
// compute trendline as simple average over dominant cycle period
HTTrendLine Trendline = HTTrendLine.Series( AveragePrice.Series( Bars ) );
bool trend = false;
for(int bar = 10; bar < Bars.Count; bar++)
{
//declare a trend mode if smoothprice is more than TRENDFILTER % from trendline
if ( (SmoothPrice[bar] - Trendline[bar]) / Trendline[bar] >= trendfilter )
trend = true;
else if
( (SmoothPrice[bar] - Trendline[bar]) / Trendline[bar] <= -trendfilter )
trend = false;
if( trend )
SetBarColor( bar, Color.Navy );
else
SetBarColor( bar, Color.Silver );
}
}
}
}