TASC January 2026 John Ehler's Reversion Index

I tried to convert the Tradestaion code in AFL. It is not working. Can anyone point the bug and fix it? Thanks

/* TASC Jan 2026
Ehler's Reversion Index
Tradestation Code
Indicator: Reversion Index

{
TASC APR 2026
Reversion Index
(C) 2005 John F. Ehlers
}

inputs:
Length( 20 );

variables:
DeltaSum( 0 ),
AbsDeltaSum( 0 ),
Count( 0 ),
Ratio( 0 ),
Smooth( 0 ),
Trigger( 0 );

DeltaSum = 0;
AbsDeltaSum = 0;

for Count = 0 to Length - 1
begin
DeltaSum = DeltaSum + Close[Count] - Close[Count + 1];
AbsDeltaSum = AbsDeltaSum + AbsValue( Close[Count]
- Close[Count + 1] );
end;

if AbsDeltaSum <> 0 then
Ratio = DeltaSum / AbsDeltaSum;

Smooth = $SuperSmoother( Ratio, 8 );
Trigger = $SuperSmoother( Ratio, 4 );

Plot1( Smooth, "Smooth" );
Plot2( 0, "Zero" );
Plot3( Trigger, "Triger" );

Function: $SuperSmoother

{
SuperSmoother Function
(C) 2025 John F. Ehlers
}

inputs:
Price(numericseries),
Period(numericsimple);

variables:
a1( 0 ),
b1( 0 ),
c1( 0 ),
c2( 0 ),
c3( 0 );

a1 = ExpValue(-1.414 * 3.14159 / Period);
b1 = 2 * a1 * Cosine(1.414 * 180 / Period);
c2 = b1;
c3 = -a1 * a1;
c1 = 1 - c2 - c3;

if CurrentBar >= 4 then
$SuperSmoother = c1*(Price + Price[1]) / 2
+ c2 * $SuperSmoother[1] + c3 * $SuperSmoother[2];
if CurrentBar < 4 then
$SuperSmoother = Price;
*/
//=============== AFL Code ================

PI = 3.1415926;
SQ2 = sqrt( 2 );

function SuperSmoother( array, periods )
{
a1 = exp( -SQ2 * PI / periods );
b1 = 2 * a1 * cos( SQ2 * PI / periods );
c2 = b1;
c3 = -a1 * a1;
c1 = 1 - c2 - c3;

Filt = Nz( array ); 

for ( i = 4; i < BarCount; i++ ) 
{ 
     Filt[ i ] = c1 * ( array[ i ] + array[ i - 1 ] ) / 2 + 
                 c2 * Filt[ i - 1 ] + 
                 c3 * Filt[ i - 2]; 
} 

return Filt; 

}

_SECTION_BEGIN ("Ehler's Reversion Index");

T = Param("per", 20,1,500,1);
T1 = Param("per1", 8,1,500,1);
T2 = Param("per2", 4,1,500,1);

ch = C - Ref(C,-1) ;
ch_abs = abs(ch);
DeltaSum = Sum(ch,T);
AbsDeltaSum = Sum(ch_abs,T);

Ratio = iif(AbsDeltaSum > 0, DeltaSum/AbsDeltaSum,0);
//Ratio = DeltaSum/AbsDeltaSum;

Smooth = SuperSmoother(Ratio,T1);
Trigger = SuperSmoother(Ratio,T2);
//Trigger = SuperSmoother(C,T2); // works for debug

Plot( Smooth, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
Plot(Trigger,"Trigger", colorYellow, styleThick);

_SECTION_END();

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

“It is not working” is pretty vague. Please try to be more specific, as per this post: How to ask a good question

@my1958, a couple of suggestions:

Cosine() in TradeStation EasyLanguage uses degrees - AFL on the other end expects values in radians.

The chart in the TS examples is using a 10 periods (see the TV one to compare your 20 periods plot).

Check also this user guide example to see an alternative implementation of the SuperSmoother avoiding the loop (wrap it in a function passing to it the array and the periods and return the calculated array).

2 Likes

For anything that has output =constant *output [i-1] you need to start with IIR very powerful

So Super Smoother fn can be done using below lines, the rest is not that hard to implement


sm=null;
sm_period = 0.5*period;
c1 = 1.41421 * 3.14159 / sm_period;  
c2 = 2.71828^-c1;  
a1 = 2 * c2 * cos( c1 );  
a2 = -c2^2;  
b0 = (1 - a1 - a2)/2;  
b1 = b0;  
sm = IIR( src, b0, a1, b1, a2 );

THank you very much. Your solution worked. Very elegant. You taught me something.