02 October 2011

Trend Quality Indicators for Amibroker (AFL)

Trend Quality Indicators for Amibroker (AFL)


Originally described in Stocks & Commodities April 2004, by David Sepiashvili.
The trend-quality indicator (or Q-indicator) is a trend detection and estimation tool that is based on a two-step filtering technique. It measures cumulative price changes over term-oriented semicycles and relates them to ‘noise.’ The approach reveals congestion and trending periods of the price movement and focuses on the most important trends, evaluating their strength in the process. The indicator is presented in a centered oscillator and banded oscillator format.

Both Q-Indicator (the Trend Quality one) and the B-Indicator (trend-noise Balance Indicator) can be displayed (alternatively).

Different crossover signals can be easily added by just adding the required string in ParamList (the string SHALL follows the convention ?MA m1,m2 where ?MA is the MA type (EMA, SMA or WMA) and m1,m2 are the period ALWAYS in 2 digits (i.e. 5 shall be written as 05)

Q-indicator has 3 levels: 2(+/-) and 5(+/-): 0 to 2 means weak or NO trend; 2 to 5 means moderate trend, >= 5 means strong trend (Up trend if positive, Down trend if negative).

B-Indicator fluctuates between 0 to 100. It is better suited to identifies overbought/oversold levels. It has 2 levels: 65 and 80. 0 to 65 means NO or weak trend, 65 to 80 means moderate trend and >= 80 means strong trend.

I like this indicator since it is quite “agnostic” about the chosen parameters (Obviously the crossover MAs do matter !!).

Here is a screenshot of how the indicator looks:



Trend Quality Indicators for Amibroker (AFL)

In "Trend-Quality Indicator" in this issue, David Sepiashvili presents an innovative trend-detection tool -- the trend-quality indicator -- that attempts to estimate the trend in relation to noise.

Calculations presented in the article can be easily reproduced using AmiBroker Formula Language. The only tricky part is a piecewise exponential moving average that restarts the calculations on every moving average crossover, but we managed to implement it in two lines of code, thanks to AmiBroker's powerful Ama2 function, which allows easy implementation of all kinds of single-order infinite impulse response filters.

Listing 1 shows ready-to-use indicator code to plot the Q-indicator. In AmiBroker, select Indicator Builder from the Analysis menu, click the "Add" button, enter the formula, and then press "Apply." Figure 3 shows a sample chart.


// Piecewise EMA is an EMA that restarts calculations each time
// the 'sincebar' argument is true
function PiecewiseEMA( array, range, sincebar )
{
factor = IIf( sincebar, 1, 2/(range+1) );
return AMA2( array, factor, 1-factor );
}
// parameters
m=4;
n=250;
// generate reversal signals based on EMA crossover rule
Lpf1 = EMA( C, 7 );
Lpf2 = EMA( C, 15 );
CrossPoint = Cross( Lpf1, Lpf2 ) OR Cross( Lpf2, Lpf1 );
Periods = BarsSince( CrossPoint );
// variable bar sum
DC = Close - Ref( Close, -1 );
CPC = Sum( DC, Periods );
// smooth CPC by piecewise 4 bar EMA
Trend = PiecewiseEMA( CPC, 4, CrossPoint );
// noise
DT = CPC - Trend;
Noise = 2 * sqrt( MA( DT^2, n ) );
// alternative 'linear' noise calculation
// Noise = 2 * MA( abs( DT ), n ) );
QIndicator = Trend/Noise;
Plot(sign(Lpf1-Lpf2), "Rev", colorRed );
Plot( Qindicator, "Qindicator", colorGreen, styleHistogram);
PlotGrid( -1 );
PlotGrid( 1 );
PlotGrid( 2 );
PlotGrid( -2 );
PlotGrid( 5 );
PlotGrid( 5);

No comments:

My Blog List

Total Pageviews

Search This Blog

Followers