Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate RSI(Relative Strength Index) using some programming language (JS/C#..)

I am working to calculate RSI (Relative Strength Index). I have data like this

**Date|Close|Change|Gain|Loss**

The formula for calculating this is

RSI = 100 - 100/(1+RS)
where RS = Average Gain / Average Loss

Source

So I want to calculate via some programming language either in JavaScript or C# but i don't know exactly how to convert that in programming language or what steps do I need.

If there is anything you want more to understand my problem i will try to explain.

like image 319
Blu Avatar asked Mar 25 '14 05:03

Blu


2 Answers

Simple way to translate the RSI formula:

public static double CalculateRsi(IEnumerable<double> closePrices)
{
    var prices = closePrices as double[] ?? closePrices.ToArray();

    double sumGain = 0;
    double sumLoss = 0;
    for (int i = 1; i < prices.Length; i++)
    {
        var difference = prices[i] - prices[i - 1];
        if (difference >= 0)
        {
            sumGain += difference;
        }
        else
        {
            sumLoss -= difference;
        }
    }

    if (sumGain == 0) return 0;
    if (Math.Abs(sumLoss) < Tolerance) return 100;

    var relativeStrength = sumGain / sumLoss;

    return 100.0 - (100.0 / (1 + relativeStrength));
}

There are plenty of projects implementing RSI in different ways. An incremental way can be found here

like image 94
Riga Avatar answered Oct 10 '22 17:10

Riga


This should not be different to Riga's answer however it seems to never drop below 40, so be careful, maybe just stick with TA_LIB?

    //Relative Strength Index
    function rsi($ar, $period, $opt, $offset=0) //opt: 0=none, 1=exponential, 2=wilder, 3=average all
    {
        GLOBAL $smoothsteps;
        $pag = 0; //Previous Average Losses
        $pal = 0; //Previous Average Gains

        //Count average losses and gains
        $len = sizeof($ar)-1-$offset;
        $end = $len-$period-$offset;
        for($i = $len; $i > $end; $i--)
        {
            if($ar[$i] > $ar[$i-1]) //Gain
                $pag += $ar[$i] - $ar[$i-1]; 
            else //Loss
                $pal += $ar[$i-1] - $ar[$i];
        }
        $pag /= $period;
        $pal /= $period;

        //Smooth
        $ag = 0; //Average Losses
        $al = 0; //Average Gains
        for($i = $len; $i > 0; $i--)
        {
            if($ar[$i] > $ar[$i-1]) //Gain
                $ag += $ar[$i] - $ar[$i-1]; 
            else //Loss
                $al += $ar[$i-1] - $ar[$i];
        }

        if($opt == 3) //Average All Three
        {
            $a = 1 / $smoothsteps;
            $tag = $a * $ag + (1 - $a) * $pag;
            $tal = $a * $al + (1 - $a) * $pal;
            $wag = $pag * 13 + $ag;
            $wal = $pal * 13 + $al;
            $ag = ($wag+$tag+$pag)/3;
            $al = ($wal+$tal+$pal)/3;
        }
        else if($opt == 2) //Wilder Exp
        {
            $ag = $pag * 13 + $ag;
            $al = $pal * 13 + $al;
        }
        else if($opt == 1) //Exponential (Lame) [Closest to Trading View]
        {
            $sa = 1 / $smoothsteps;
            $ag = $sa * $ag + (1 - $sa) * $pag;
            $al = $sa * $al + (1 - $sa) * $pal;
        }
        else if($opt == 0) //None
        {
            $ag = $pag;
            $al = $pal;
        }

        //Relative Strength
        $rs = $ag / $al;

        //Relative Strength Index
        return 100 - (100 / (1+$rs));
    }
like image 43
SirTames Avatar answered Oct 10 '22 15:10

SirTames