// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN( "BoH Risk Aversion Indicator" );

//MyPercentRank Function as Coded by Ramon Cummins
function MyPercentRank( Data, Periods )
{
    Count = 0;
    for( i = 1; i < Periods + 1 ; i++ )
    {
        Count = Count + IIf( Ref( Data, 0 ) > Ref( Data, -i ), 1, 0 );
    }
    return 100 * Count / Periods;
}

function RAI( Length )
{
    PCHG		= ( C / Ref( C, -1 ) );
    xRR		 	= MyPercentRank( PCHG, 252 );
    Vol			= StDev( PCHG, 252 ) * 3.1;
    xRAI		= Correlation( xRR, Vol, Length );
    return xRAI;
}

//RAI Plot
Length	= Param( "Correlation Period", 21, 1, 300, 1 );
xRAI	= RAI( Length );
Plot( xRAI, "", colorGreen, styleThick );
Plot( 0, "", colorDarkRed, styleThick | styleNoLabel );
Plot( 0.2, "", colorDarkRed, styleDashed | styleNoLabel );
Plot( -0.2, "", colorDarkRed, styleDashed | styleNoLabel );

_SECTION_END();