首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用雅虎!金融市场数据下载程序(yfinance) Python包从朱莉娅?

问如何使用雅虎!金融市场数据下载程序(yfinance) Python包从朱莉娅?
EN

Stack Overflow用户
提问于 2021-01-24 19:48:11
回答 1查看 3.6K关注 0票数 2

我正在尝试将股票数据从yahoo finance下载到我的朱莉娅代码中,并使用它进行进一步的分析。由于Julia相关的包在下载数据时有很多错误,所以我想使用PyCall来使用python库yfinance来获取数据。

获取数据的python示例如下所示:

代码语言:javascript
复制
import yfinance as yf
msft = yf.Ticker("MSFT")
# get stock info
msft.info

# get historical market data
hist = msft.history(period="max")

# show actions (dividends, splits)
msft.actions
print(msft.actions)

# show dividends
msft.dividends
print(msft.dividends)

# show splits
msft.splits
print(msft.splits)

# show financials
msft.financials
msft.quarterly_financials

# show major holders
msft.major_holders

# show institutional holders
msft.institutional_holders

# show balance sheet
msft.balance_sheet
msft.quarterly_balance_sheet

# show cashflow
msft.cashflow
msft.quarterly_cashflow

# show earnings
msft.earnings
msft.quarterly_earnings

# show sustainability
msft.sustainability

# show analysts recommendations
msft.recommendations

# show next event (earnings, etc)
msft.calendar

# show ISIN code - *experimental*
# ISIN = International Securities Identification Number
msft.isin

# show options expirations
msft.options

# get option chain for specific expiration
opt = msft.option_chain('2021-01-29')
# data available via: opt.calls, opt.puts

我对朱莉娅密码的看法:

代码语言:javascript
复制
module MyModule
using PyCall
function __init__()
    py"""
    import yfinance as yf
    def data()
        data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30")
    """
end
tickers_data = py"data"

end

为糟糕的示例表示歉意,但我不知道如何在这种情况下使用PyCall。请提出解决这个问题的办法。

提前谢谢。

更新:

我使用以下命令在anaconda (conda version 4.9.2)中安装了(conda version 4.9.2):

代码语言:javascript
复制
conda install -c ranaroussi yfinance

这将将yfinance库安装在conda的base root中。

按照@PrzemyslawSzufel的建议,我更新了我的PyCall代码:

代码语言:javascript
复制
using PyCall

yf = pyimport("yfinance")

# Tickers
msft = yf.Ticker("MSFT")

# get stock information
msft.info

# get historical market data
hist = msft.history(period="max")

# show actions (dividends, splits)
msft.actions
println(msft.actions)

但是,在运行此代码段时,将引发一个PyError:

代码语言:javascript
复制
PyError (PyImport_ImportModule

The Python package yfinance could not be imported by pyimport. Usually this means
that you did not install yfinance in the Python version being used by PyCall.

PyCall is currently configured to use the Julia-specific Python distribution
installed by the Conda.jl package.  To install the yfinance module, you can
use `pyimport_conda("yfinance", PKG)`, where PKG is the Anaconda
package the contains the module yfinance, or alternatively you can use the
Conda package directly (via `using Conda` followed by `Conda.add` etcetera).

Alternatively, if you want to use a different Python distribution on your
system, such as a system-wide Python (as opposed to the Julia-specific Python),
you can re-configure PyCall with that Python.   As explained in the PyCall
documentation, set ENV["PYTHON"] to the path/name of the python executable
you want to use, run Pkg.build("PyCall"), and re-launch Julia.

我尝试通过使用conda在julia中专门导入包来解决这个问题:

代码语言:javascript
复制
using Conda

Conda.add("ranaroussi yfinance")
yf = pyimport_conda("yfinance", PKG)

即使这样也不能解决问题。请对此错误提出解决方案。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-24 20:40:02

假设您成功地安装了yfinance,那么您所需要做的就是:

代码语言:javascript
复制
using PyCall
run(`$(PyCall.python) -m pip install yfinance`)
yf = pyimport("yfinance")
msft = yf.Ticker("MSFT")

请注意,到目前为止,通过yfinance安装Conda.jl似乎都不起作用,因此需要使用pip。

现在,您可以将其余的代码复制到Julia函数中。将print替换为println,一切都将正常工作。

举个例子:

代码语言:javascript
复制
julia> # get stock info
       msft.info
Dict{Any, Any} with 123 entries:
  "tradeable"                    => false
  "market"                       => "us_market"
  "dayLow"                       => 224.22
  "sharesShort"                  => 39201229
  "priceToBook"                  => 14.0704
  "sharesOutstanding"            => 7560500224
  "nextFiscalYearEnd"            => 1656547200
  "threeYearAverageReturn"       => nothing
  "legalType"                    => nothing
  "address1"                     => "One Microsoft Way"
  "regularMarketPreviousClose"   => 225.95
  "priceHint"                    => 2
  "askSize"                      => 1400
  "fundFamily"                   => nothing
  "regularMarketVolume"          => 31716461
  "trailingEps"                  => 6.199
  "ask"                          => 229.88
  "lastSplitDate"                => 1045526400
  ⋮                              => ⋮
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65875169

复制
相关文章

相似问题

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