Yahoo Finance modul használata ( yfinance package)
yfinance egy kedvelt opensource library melyet Ran Aroussi fejlesztett és hozzáférést nyujt a
nyilvános céges pénzügyi adatokhoz a Yahoo Finance keresztül.
Csomag elérése:
https://pypi.org/project/yfinance/
Installálás
pip install fix-yahoo-finance==0.1.30
pip install yfinance --upgrade --no-cache-dir
conda install -c ranaroussi yfinance
conda install -c plotly plotly
Python programba a modul importálása
import yfinance as yf
Használat:
Covid-19 kapcsán a pfizer cég adatait nézzük :
https://uk.finance.yahoo.com/quote/PFE?p=PFE&.tsrc=fin-srch
A céges adatok elérése történhet:
- yf.Tickers -> egyedi azonosító val történő lékérdezéssel
- yf.download -> csoportos adat letöltéssel
- yf.pandas_datareader -> pandas data oldasóval
import yfinance as yf
pfizer = yf.Ticker("PFE")
#print('-----------------------')
#print(pfizer.info)
old = pfizer.history(start="2010-01-01", end="2020-07-21")
print(old.head())
print('-----------------------')
print(pfizer.actions) # show actions (dividends, splits)
print('-----------------------')
print(pfizer.dividends) # show dividends
print('-----------------------')
print(pfizer.splits) # show splits
print('-----------------------')
Open High Low Close Volume Dividends \ Date 2009-12-31 11.161699 11.216324 11.040310 11.040310 27063136 0.0 2010-01-04 11.088863 11.495516 11.070656 11.489447 54898644 0.0 2010-01-05 11.483379 11.489448 11.258810 11.325574 45714931 0.0 2010-01-06 11.325576 11.416617 11.234534 11.289159 43640975 0.0 2010-01-07 11.313432 11.331640 11.204182 11.246668 41557112 0.0 Stock Splits Date 2009-12-31 0 2010-01-04 0 2010-01-05 0 2010-01-06 0 2010-01-07 0 ----------------------- Dividends Stock Splits Date 2010-02-03 0.170778 0.0 2010-05-05 0.170778 0.0 2010-08-04 0.170778 0.0 2010-11-04 0.170778 0.0 2011-02-02 0.189753 0.0 2011-05-11 0.189753 0.0 2011-08-03 0.189753 0.0 2011-11-08 0.189753 0.0 2012-02-01 0.208729 0.0 2012-05-09 0.208729 0.0 2012-08-01 0.208729 0.0 2012-11-07 0.208729 0.0 2013-01-30 0.227704 0.0 2013-05-08 0.227704 0.0 2013-07-31 0.227704 0.0 2013-11-06 0.227704 0.0 2014-02-05 0.246679 0.0 2014-05-07 0.246679 0.0 2014-07-30 0.246679 0.0 2014-11-05 0.246679 0.0 2015-02-04 0.265655 0.0 2015-05-06 0.265655 0.0 2015-08-05 0.265655 0.0 2015-11-04 0.265655 0.0 2016-02-03 0.284630 0.0 2016-05-11 0.284630 0.0 2016-08-03 0.284630 0.0 2016-11-08 0.284630 0.0 2017-02-01 0.303605 0.0 2017-05-10 0.303605 0.0 2017-08-02 0.303605 0.0 2017-11-09 0.303605 0.0 2018-02-01 0.322581 0.0 2018-05-10 0.322581 0.0 2018-08-02 0.322581 0.0 2018-11-08 0.322581 0.0 2019-01-31 0.341556 0.0 2019-05-09 0.341556 0.0 2019-08-01 0.341556 0.0 2019-11-07 0.341556 0.0 2020-01-30 0.360531 0.0 2020-05-07 0.360531 0.0 ----------------------- Date 2010-02-03 0.170778 2010-05-05 0.170778 2010-08-04 0.170778 2010-11-04 0.170778 2011-02-02 0.189753 2011-05-11 0.189753 2011-08-03 0.189753 2011-11-08 0.189753 2012-02-01 0.208729 2012-05-09 0.208729 2012-08-01 0.208729 2012-11-07 0.208729 2013-01-30 0.227704 2013-05-08 0.227704 2013-07-31 0.227704 2013-11-06 0.227704 2014-02-05 0.246679 2014-05-07 0.246679 2014-07-30 0.246679 2014-11-05 0.246679 2015-02-04 0.265655 2015-05-06 0.265655 2015-08-05 0.265655 2015-11-04 0.265655 2016-02-03 0.284630 2016-05-11 0.284630 2016-08-03 0.284630 2016-11-08 0.284630 2017-02-01 0.303605 2017-05-10 0.303605 2017-08-02 0.303605 2017-11-09 0.303605 2018-02-01 0.322581 2018-05-10 0.322581 2018-08-02 0.322581 2018-11-08 0.322581 2019-01-31 0.341556 2019-05-09 0.341556 2019-08-01 0.341556 2019-11-07 0.341556 2020-01-30 0.360531 2020-05-07 0.360531 Name: Dividends, dtype: float64 ----------------------- Series([], Name: Stock Splits, dtype: int64)
print(pfizer.sustainability) # show sustainability
print('-----------------------')
Value 2021-9 palmOil False controversialWeapons False gambling False socialScore 13.07 nuclear False furLeather False alcoholic False gmo False catholic True socialPercentile None peerCount 83 governanceScore 9.63 environmentPercentile None animalTesting True tobacco False totalEsg 24.53 highestControversy 3 esgPerformance AVG_PERF coal False pesticides False adult False percentile 39 peerGroup Pharmaceuticals smallArms False environmentScore 1.82 governancePercentile None militaryContract False -----------------------
import plotly.graph_objects as go fig = go.Figure(data=[go.Candlestick( x=old['Date'], open=old['Open'], high=old['High'], low=old['Low'], close=old['Close'])]) fig.show()
import plotly.express as px fig = px.line(old, x="Date", y="Open", title='PFizer Stock Prices 2021') fig.show()
# OHLC(Open, High, Low, and Close) charts fig = go.Figure(data=go.Ohlc( x=old['Date'], open=old['Open'], high=old['High'], low=old['Low'], close=old['Close'])) fig.show()
Minta:
https://github.com/klajosw/python/blob/master/kl_py_yfinance_minta_01.ipynb
Megjegyzések
Megjegyzés küldése