RSI built-in calculation in Amibroker

RSI (Relative Strength Index) is a known oscillator ranging from 0 to 100. Invented by J. Welles Wilder Jr. and published in his famous book New Concepts in Technical Trading Systems in 1978. Each trading platform has RSI available. It may be useful to know its implementation for customization needs. Here is the internal calculation of Amibroker’s RSI oscillator. This code was made available directly by its author Tomasz Janeczko, so there is no doubt that it is right.

// Internally RSI is implemented as follows
//
function BuiltInRSIEquivalent( period )
{
    P = N = 0;

    result = Null;

    for ( i = 1; i < BarCount; i++ )
    {
        diff = C[ i ] - C[ i - 1 ];
        W = S = 0;

        if ( diff > 0 )
            W = diff;

        if ( diff < 0 )
            S = -diff;

        P = ( ( period - 1 ) * P + W ) / period;

        N = ( ( period - 1 ) * N + S ) / period;

        if ( i >= period )
            result[ i ] = 100 * P / ( P + N );
    }

    return result;
}

Plot( BuiltInRSIEquivalent( 14 ), "RSI 1", colorRed );
Plot( RSI( 14 ), "RSI 2", colorBlue );

Here is a graph of the code above on the future EUR-USD, you can clearly see that the embedded RSI and the proposed code produce the same values. Moreover, it is possible to notice the rebound of the price when the RSI touches the value of oversold of 30, on the other hand it rebounds also when it approaches the value of overbought of 70.

RSI on EUR-USD future with overbought oversold threshold of 70-30

The RSI of the above image has been calculated over 14 periods.

You can download the code here

Leave a Reply

Your email address will not be published. Required fields are marked *