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

get stock data using python - not using quandl

我使用 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 = '.'))
}

我只知道 pandas_datareader 在 Python 中实现了类似的功能。不幸的是,随着 yahoo 和 google API 的改变,这个包被破坏了。此代码:

import pandas_datareader as pdr

panel_data = pdr.get_data_yahoo('MSFT')

结果:

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.

是否有当前可用的 Python 包来实现上述目标。我知道 quandl,但这是一项付费服务​​。谢谢

Quandl 有免费和付费等级。您绝对可以从 Quandl 获得免费的股票数据,您可以通过他们的 api 轻松获得。 pip install quandlconda install quandl。您需要做的就是注册一个免费帐户,并获得一个 API 密钥。然后是这样的。

import quandl

quandl.ApiConfig.api_key = "YOUR_API_KEY"

df = quandl.get_table("WIKI/PRICES", ticker = ["MSFT"], 
                      qopts = {"columns": ["date", "ticker", "adj_open", "adj_close"]}, 
                      paginate=True)

他们的网站上还有大量文档。和多个来源。

退房:

初学者。

Alpha Vantage is another great source which is free and provides realtime stock quotes as RESTful JSON and CSV apis. Here is the API documentation 给它。

设置

设置起来相当简单。您需要做的就是从 here 生成一个免费的 API 密钥,然后安装他们的模块以及 matplotlib

pip install matplotlib
pip install alpha_vantage

例子

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

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

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)

输出:

Source求代码和图片

编辑

更改了一些代码。参考评论修改。

尝试 fix_yahoo_finance:

from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
data = yf.download("MSFT", start="2017-01-01", end="2017-04-30")
print(data)

[*********************100%***********************]  1 of 1 downloaded
                 Open       High    ...     Adj Close    Volume
Date                                ...                        
2017-01-03  62.790001  62.840000    ...     60.664047  20694100
2017-01-04  62.480000  62.750000    ...     60.392612  21340000
2017-01-05  62.189999  62.660000    ...     60.392612  24876000
2017-01-06  62.299999  63.150002    ...     60.916084  19922900
2017-01-09  62.759998  63.080002    ...     60.722206  20256600
2017-01-10  62.730000  63.070000    ...     60.702820  18593000