4

I am trying to get RSI indicator from yahoo finance api.

so far I can get quote in CSV format, but there seems no api for specific indicator such as RSI.

Anyone knows how ?

thanks

user1225072
  • 335
  • 1
  • 4
  • 11

3 Answers3

6

You have all the data needed to calculate the RSI. http://www.investopedia.com/terms/r/rsi.asp

import numpy as np
from urllib.request import urlopen

# The stock to fetch
stock = 'AMD'

# Yahoos API
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
stockFile = []

# Fetch the stock info from Yahoo API
try:
    sourceCode = urlopen(urlToVisit).read().decode('utf-8')
    splitSource = sourceCode.split('\n')
    for eachLine in splitSource:
        splitLine = eachLine.split(',')
        if len(splitLine)==6:
            if 'values' not in eachLine:
                stockFile.append(eachLine)
except Exception as e:
    print(str(e), 'failed to organize pulled data')

except Exception as e:
    print(str(e), 'failed to pull price data')

date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=',',unpack=True,)


def rsiFunc(prices, n=14):
    # Returns an RSI array
    deltas = np.diff(prices)
    seed = deltas[:n+1]
    up = seed[seed>=0].sum()/n
    down = -seed[seed<0].sum()/n
    rs = up/down
    rsi = np.zeros_like(prices)
    rsi[:n] = 100. - 100./(1.+rs)

    for i in range(n, len(prices)):
        delta = deltas[i-1]
        if delta > 0:
            upval = delta
            downval = 0.
        else:
            upval = 0.
            downval = -delta
        up = (up*(n-1)+upval)/n
        down = (down*(n-1)+downval)/n
        rs = up/down
        rsi[i] = 100. - 100./(1.+rs)
    return rsi


# Lets see what we got here
rsi = rsiFunc(closep)
n = 0
for i in date:
    print('Date stamp:', i, 'RSI', rsi[n])
    n+=1
Jack
  • 242
  • 4
  • 18
  • 1
    I can see lot of efforts in your interesting answers. I wish I can upvote them all. Unluckily most members do not encourage helpful and useful ones like you – Billal Begueradj Jun 04 '16 at 15:10
1

There is no such API for yahoo finance. I found an interesting API that seems to do what you are looking for (https://www.stockvider.com/). It's brand new, the API does not provide a lot of features but it aims to cover the most common technical indicators. So far you can get your data in xml format only.

For instance, you can get RSI values for Apple stock like this : https://api.stockvider.com/data/NASDAQ/AAPL/RSI?start_date=2015-05-20&end_date=2015-07-20

Ali Baba
  • 350
  • 1
  • 4
  • 12
0

You can get the quotes and compute the indicators you want with packages. See the examples of quantmod and TTR.

For example:

library(quantmod)
getSymbols('F',src='yahoo',return.class='ts') 
fpr <- Cl(F)
rsi <- RSI(fpr)

tail(cbind(Cl(F),rsi),10)
Robert
  • 5,038
  • 1
  • 25
  • 43