首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python获取股票数据-不使用quandl

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

Stack Overflow用户
提问于 2018-07-22 04:14:15
回答 1查看 4.6K关注 0票数 10

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

代码语言:javascript
复制
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的改变,这个包被破坏了。这段代码:

代码语言:javascript
复制
import pandas_datareader as pdr

panel_data = pdr.get_data_yahoo('MSFT')

结果如下:

代码语言:javascript
复制
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,但这是一项付费服务。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-07-30 17:17:03

试试fix_yahoo_finance

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51459752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档