Random Walk Index and of Highs / Lows

Hello Everyone,

Old wine (question) in new bottle (forum).

Puns apart, we have three inbuilt functions in this regard:

Objective is to replicate the inbuilt functions, to be able to experiment with other price-derived arrays instead of using the default Highs and Lows. Interestingly, a similar request was posted a decade back - Random Walk Index -- raw AFL code request. And feel the same way as asked here RWIHI question.

Let me show you what I have done to unveil RwIHI() function but the values do not match with the inbuilt one.

Inbuilt RwIHI():

``````minperiods = Param( "Min Periods", 9, 1, 200, 1 );
maxperiods = Param( "Max Periods", 40, 1, 200, 1 );
Plot( RwIHI( minperiods, maxperiods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
``````

Custom raw RWH:

``````_SECTION_BEGIN( "Unveil RWIHi()" );
mthd = ParamList( "Select Method", "Method 1|Method 2", 0 );
pMin = Param( "Min Periods", 9, 1, 200, 1 );
pMax = Param( "Max Periods", 40, 1, 200, 1 );

switch( mthd )
{
case "Method 1":
// https://www.mail-archive.com/amibroker@yahoogroups.com/msg30080.html
RWHmin = ( H - Ref( L, -pMin ) ) / ( Max( ATR( 1 ), ATR( pMin ) ) * sqrt( pMin ) );
RWHmax = ( H - Ref( L, -pMax ) ) / ( Max( ATR( 1 ), ATR( pMax ) ) * sqrt( pMax ) );
RWH = Max( RWHmin, RWHmax );
break;

case "Method 2":
// http://www.amibroker.com/members/library/detail.php?id=924&hilite=RWIHi
VarMaxHi = 0;
for( i = 5; i <= BarCount - 1; i++ )
{
VarMaxHi[ i ] = Max( ( H[ i ] - L[ i - 1 ] ) / ( ( H[ i ] - L[ i ] ) * sqrt( i - 4 ) ), ( H[ i ] - L[ i - 2 ] ) / ( ( H[ i ] - L[ i ] ) * sqrt( i - 3 ) ) );
RWH[ i ] = Max( VarMaxHi[ i ], VarMaxHi[ i - 1 ] );
}
break;
}

Title = "RWH: " + RWH;
Plot( RWH, "RWH", ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();
``````
2 Likes

@Lennon the differences may be due to definitions. I have not had the chance to compare your formula to the original but these might help you. The original article by Mike Poulos's "Of trends and random walks" appeared in the February 1991 issue of STOCKS & COMMODITIES. There was no AmiBroker back then and I've pasted a description from 1991 of the calculations in a PM (I wasn't sure about copyright issues so I didn't want to post on the public forum).

A couple of years later the editor of the Technical Analysis of Stocks & Commodities included a spreadsheet calculation of the indicator, see your PM.

Hope that helps you some. Good luck.

2 Likes

Here is how AmiBroker's RWI* indicators are calculated:

``````function cRWIhi( pmin, pmax ) {
// AFL function by fxshrat for https://forum.amibroker.com
// reproduction of https://www.amibroker.com/guide/afl/rwihi.html
RWIHmax = -1e9;
for ( i = pmin; i <= pmax; i++ ) {
RWIH = (H - Ref(L, -i)) / ATR(i) / sqrt(i);
RWIHmax = Max( RWIHmax, RWIH );
}
return RWIHmax;
}

function cRWIlo( pmin, pmax ) {
// AFL function by fxshrat for https://forum.amibroker.com
// reproduction of https://www.amibroker.com/guide/afl/rwilo.html
RWILmax = -1e9;
for ( i = pmin; i <= pmax; i++ ) {
RWIL = (Ref( H, -i ) - L) / ATR(i) / sqrt(i);
RWILmax = Max( RWILmax, RWIL );
}
return RWILmax;
}

function cRWI( pmin, pmax ) {
// reproduction of https://www.amibroker.com/guide/afl/rwi.html
result = cRWIhi( pmin, pmax ) - cRWIlo( pmin, pmax );
return result;
}

minperiods = Param( "Min Periods", 9, 1, 200, 1 );
maxperiods = Param( "Max Periods", 40, 1, 200, 1 );

Plot( cRWIhi( minperiods, maxperiods ), "RWIHi", colorGreen );
Plot( cRWIlo( minperiods, maxperiods ), "RWILo", colorRed );
Plot( cRWI( minperiods, maxperiods  ), "RWI", colorDefault );
``````
3 Likes

Small update in regards to start of array. After a few tests it looks OK now (for comparison test just remove the "c" in front of function call).

``````function cRWIhi( pmin, pmax ) {
// AFL function by fxshrat for https://forum.amibroker.com
// reproduction of https://www.amibroker.com/guide/afl/rwihi.html
local i, bi, RWIH, RWIHmax;
RWIHmax = -9e9;
bi = BarIndex();
for ( i = Min(pmin, pmax); i <= pmax; i++ ) {
RWIH = IIf( i+2<=bi, (H - Ref(L, -i)) / (ATR(i)) / sqrt(i), -9e9 );
RWIHmax = Max( RWIHmax, RWIH );
}
return IIf(RWIHmax > -9e9, RWIHmax, Null);
}

function cRWIlo( pmin, pmax ) {
// AFL function by fxshrat for https://forum.amibroker.com
// reproduction of https://www.amibroker.com/guide/afl/rwilo.html
local i, bi, RWIL, RWILmax;
RWILmax = -9e9;
bi = BarIndex();
for ( i = Min(pmin, pmax); i <= pmax; i++ ) {
RWIL = IIf( i+2<=bi, (Ref(H, -i) - L) / (ATR(i)) / sqrt(i), -9e9 );
RWILmax = Max( RWILmax, RWIL );
}
return IIf(RWILmax > -9e9, RWILmax, Null);
}

function cRWI( pmin, pmax ) {
// reproduction of https://www.amibroker.com/guide/afl/rwi.html
result = cRWIhi( pmin, pmax ) - cRWIlo( pmin, pmax );
return result;
}

minperiods = Param( "Min Periods", 9, 1, 200, 1 );
maxperiods = Param( "Max Periods", 40, 1, 200, 1 );

Plot( cRWIhi( minperiods, maxperiods ), "RWIHi", colorGreen );
Plot( cRWIlo( minperiods, maxperiods ), "RWILo", colorRed );
Plot( cRWI( minperiods, maxperiods ), "RWI", colorDefault );
``````
7 Likes

Lovely!

This is like pure magic with your "Elixir Logic" and shows the "Power of Amibroker". Accustomed to other programming compilers, I am often compelled to think in fragments and has worked along to join the fragments to create the final picture - mostly with unavoidable flaws. But the ability of AB to implement a formula as a whole and the possibilities it provides are truly mind-boggling (honestly disturbing for me at times because of lack of hardcore AFL skills ).

Here you have made me realize one of the basics which I have not been able to conceptualize correctly so far. By showing the loop, you cleared many of my AFL array related doubts. TJ and Milosz tried to explain it to me here but it did not breach my thick skull. LOL !

Let's say we want to replicate the MA() function.
Inbuilt MA():

``````per = Param( "Period", 15, 1, 100, 1 );

R = C; //Array
AR = MA( R, per ); //Simple Moving Average

Plot( AR, "AR", colorWhite, styleLine );
``````

Raw MA():

``````per = Param( "Period", 15, 1, 100, 1 );

R = C; //Array
SR = 0; //Sum of the array elements
AR = 0; //Average of the array elements

for( i = 0; i < per; i++ )
{
SR = SR + Ref( R, -i );
AR = SR / ( i + 1 );
}
Plot( AR, "AR", colorWhite, styleLine );
``````

Another one, let's say we want to replicate the ATR() function.
Inbuilt ATR():

``````per = Param( "Period", 15, 1, 100, 1 );
Plot( ATR( per ), "ATR", colorWhite, styleLine );
``````

Raw ATR():

``````// http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_true_range_atr
// Based on http://stockcharts.com/school/lib/exe/fetch.php?media=chart_school:technical_indicators_and_overlays:average_true_range_atr:cs-atr.xls
per = Param( "Period", 15, 1, 100, 1 );

cATR[ 0 ] = H[ 0 ] - L[ 0 ];
for( i = 1; i < BarCount; i++ )
{
R1[ i ] = H[ i ] - L[ i ];
R2[ i ] = abs( H[ i ] - C[ i - 1 ] );
R3[ i ] = abs( C[ i - 1 ] - L[ i ] );
TR[ i ] = Max( R1[ i ], Max( R2[ i ], R3[ i ] ) );
cATR[ i ] = ( TR[ i ] + cATR[ i - 1 ] * ( per - 1 ) ) / per;
}

Plot( cATR, "cATR", colorWhite, styleLine );
``````

Many many thanks to you, to TJ for the platform and to the beautiful minds of the forum!

P.S. The Raw ATR AFL is not precisely accurate to AB's AFL as I am not aware of the used variation of Wilders Smoothing Technique.

2 Likes