I am trying to download the stock data from yahoo finance
to my Julia code and use it for further analysis. Since Julia stocks related packages have a lot of bugs while downloading data, I want to use the PyCall
to utilise the python library yfinance
to get the data.
The python example to get the data is presented below:
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
My take on the julia code:
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
Apologies for poor example, but I am not sure how to use PyCall
in such scenario.
Please suggest a solution to this problem.
Thanks in advance.
Update:
I installed yfinance
in the anaconda (conda version 4.9.2)
using the following command:
conda install -c ranaroussi yfinance
This will install the yfinance
library in the base root
of conda.
Following the suggestions by @PrzemyslawSzufel, I updated my PyCall code:
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)
However, upon running this snippet a PyError
is thrown:
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.
I tried resolving this by specifically importing the package in julia using conda:
using Conda
Conda.add("ranaroussi yfinance")
yf = pyimport_conda("yfinance", PKG)
Even this doesn't resolve the issue. Please suggest a solution for this error. Thanks.
Assuming that you managed to install yfinance all you need to do is:
using PyCall
run(`$(PyCall.python) -m pip install yfinance`)
yf = pyimport("yfinance")
msft = yf.Ticker("MSFT")
Note that as of today installation of yfinance
via Conda.jl
neither Anaconda does not seem to work and hence pip
needs to be used.
Now you can copy the rest of the code to your Julia function. Replace print
with println
and everything will just work.
For an example:
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
⋮ => ⋮