使用python获取股票数据 - 不使用quandl

cs0*_*815 10 python r stockquotes

我使用R包quantmod没有问题,它使用Yahoo来获取股票数据,如下所示:

get_stock_prices <- function(target, return_format = "tibble", ...) {
    # Get stock prices
    print(target)
    stock_prices_xts <- getSymbols(Symbols = target, auto.assign = FALSE, ...)
    # Rename
    names(stock_prices_xts) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")
    # Return in xts format if tibble is not specified
    if (return_format == "tibble") {
        stock_prices <- stock_prices_xts %>%
            as_tibble() %>%
            rownames_to_column(var = "Date") %>%
            mutate(Date = ymd(Date))
    } else {
        stock_prices <- stock_prices_xts
    }
    write.csv(stock_prices, file = paste(target, "csv", sep = '.'))
}
Run Code Online (Sandbox Code Playgroud)

我只知道Python中的pandas_datareader来实现类似的东西.不幸的是,随着雅虎和谷歌API的变化,这个包破产了.这段代码:

import pandas_datareader as pdr

panel_data = pdr.get_data_yahoo('MSFT')
Run Code Online (Sandbox Code Playgroud)

结果是:

Yahoo Actions has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.
Run Code Online (Sandbox Code Playgroud)

是否有一个目前正在运行的Python包来实现上述目的.我知道quandl但这是一项付费服务​​.谢谢.

Sam*_*ajM 10

Alpha Vantage是另一个免费的优秀来源,提供RESTful JSON和CSV apis的实时股票报价.这是它的API文档.

建立

设置起来相当简单.您需要做的就是从此处生成一个免费的API密钥,然后将其模块与matplotlib一起安装

pip install matplotlib
pip install alpha_vantage
Run Code Online (Sandbox Code Playgroud)

例子

您可以在其文档页面上查看示例,但我也会在此处列出一些示例.

这是我在网上找到的一些代码:

from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt
import sys

def stockchart(symbol):
    ts = TimeSeries(key='your_key', output_format='pandas')
    data, meta_data = ts.get_intraday(symbol=symbol,interval='1min', outputsize='full')
    print data
    data['4. close'].plot()
    plt.title('Stock chart')
    plt.show()

symbol=raw_input("Enter symbol name:")
stockchart(symbol)
Run Code Online (Sandbox Code Playgroud)

输出:

产量

代码和图片的来源.

编辑

改变了一些代码.请参阅注释以了解更改.