2015-09-01 60 views
3

我想從雅虎財經API獲取RSI指標。從雅虎API獲取技術指標

到目前爲止我可以以CSV格式獲得報價,但似乎沒有針對特定指標(如RSI)的API。

任何人都知道如何?

謝謝

回答

4

你必須計算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 
+1

我可以在你有趣的答案中看到很多努力。我希望我能把他們全部搞定。不幸的是,大多數成員不鼓勵像你這樣有幫助和有用的人 –

0

你可以得到報價並計算你想要的包裝指標。請參閱quantmodTTR的示例。

例如:

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

tail(cbind(Cl(F),rsi),10) 
2

沒有爲雅虎財經沒有這樣的API。我發現了一個有趣的API,它似乎可以做你正在尋找的東西(https://www.stockvider.com/)。這是全新的,API不提供很多功能,但它旨在覆蓋最常見的技術指標。到目前爲止,您只能以xml格式獲取數據。

舉例來說,你可以得到RSI值,蘋果公司的股票就像這樣:https://api.stockvider.com/data/NASDAQ/AAPL/RSI?start_date=2015-05-20&end_date=2015-07-20