如何使用雅虎!来自 Julia 的金融市场数据下载器 (yfinance) Python 包?
How to use Yahoo! Finance market data downloader (yfinance) Python package from Julia?
我正在尝试将 yahoo finance
中的股票数据下载到我的 Julia 代码中,并将其用于进一步分析。由于Julia股票相关包在下载数据时有很多错误,我想使用PyCall
利用python库yfinance
来获取数据。
获取数据的python例子如下:
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
我对 julia 代码的看法:
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)
中安装了 yfinance
:
conda install -c ranaroussi yfinance
这将在 conda 的 base root
中安装 yfinance
库。
根据@PrzemyslawSzufel 的建议,我更新了我的 PyCall 代码:
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
:
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 中专门导入包来解决这个问题:
using Conda
Conda.add("ranaroussi yfinance")
yf = pyimport_conda("yfinance", PKG)
即使这样也不能解决问题。
请针对此错误提出解决方案。
谢谢。
假设您成功安装了 yfinance,您需要做的就是:
using PyCall
run(`$(PyCall.python) -m pip install yfinance`)
yf = pyimport("yfinance")
msft = yf.Ticker("MSFT")
请注意,截至今天,通过 Conda.jl
安装 yfinance
两个 Anaconda 似乎都不起作用,因此需要使用 pip
。
现在您可以将其余代码复制到您的 Julia 函数中。将 print
替换为 println
,一切都会正常进行。
举个例子:
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
⋮ => ⋮
我正在尝试将 yahoo finance
中的股票数据下载到我的 Julia 代码中,并将其用于进一步分析。由于Julia股票相关包在下载数据时有很多错误,我想使用PyCall
利用python库yfinance
来获取数据。
获取数据的python例子如下:
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
我对 julia 代码的看法:
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)
中安装了 yfinance
:
conda install -c ranaroussi yfinance
这将在 conda 的 base root
中安装 yfinance
库。
根据@PrzemyslawSzufel 的建议,我更新了我的 PyCall 代码:
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
:
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 中专门导入包来解决这个问题:
using Conda
Conda.add("ranaroussi yfinance")
yf = pyimport_conda("yfinance", PKG)
即使这样也不能解决问题。 请针对此错误提出解决方案。 谢谢。
假设您成功安装了 yfinance,您需要做的就是:
using PyCall
run(`$(PyCall.python) -m pip install yfinance`)
yf = pyimport("yfinance")
msft = yf.Ticker("MSFT")
请注意,截至今天,通过 Conda.jl
安装 yfinance
两个 Anaconda 似乎都不起作用,因此需要使用 pip
。
现在您可以将其余代码复制到您的 Julia 函数中。将 print
替换为 println
,一切都会正常进行。
举个例子:
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
⋮ => ⋮