!pip install yfinance
import yfinance as yf
pfe = yf.Ticker(‘PFE’)
pfe.info
{
‘zip’: ‘10017’,
‘sector’: ‘Healthcare’,
‘fullTimeEmployees’: 88300,
‘longBusinessSummary’: ‘Pfizer Inc. develops, manufactures, and sells healthcare products worldwide. It offers …’
‘city’: ‘New York’,
‘phone’: ‘212–733–2323’,
‘state’: ‘NY’,
‘country’: ‘United States’,
…
‘profitMargins’: 0.31169,
‘enterpriseToEbitda’: 11.87,
‘52WeekChange’: -0.15343297,
…
}
pfe.actions
Date Dividends Stock Splits
1972–08–29 0.00333 0.0
1972–11–28 0.00438 0.0
1973–02–28 0.00333 0.0
1973–05–30 0.00333 0.0
1973–08–28 0.00333 0.0
… … …
2019–05–09 0.36000 0.0
2019–08–01 0.36000 0.0
2019–11–07 0.36000 0.0
2020–01–30 0.38000 0.0
2020–05–07 0.38000 0.0
hist = pfe.history(period=”6mo”)
type(hist)
<class pandas.core.frame.DataFrame’>
hist.info()
<class ‘pandas.core.frame.DataFrame’>
DatetimeIndex: 125 entries, 2020–01–13 to 2020–07–10
Data columns (total 7 columns):
# Column Non-Null Count Dtype
— — — — — — — — — — — — — — -
0 Open 125 non-null float64
1 High 125 non-null float64
2 Low 125 non-null float64
3 Close 125 non-null float64
4 Volume 125 non-null int64
5 Dividends 125 non-null float64
6 Stock Splits 125 non-null int64
df1 = hist[[‘Open’]]
print(df1)
Open Date
2020–01–13 38.83
2020–01–14 38.65
2020–01–15 39.39
2020–01–16 39.98
2020–01–17 39.76
… …
2020–07–06 34.95
2020–07–07 34.05
2020–07–08 34.01
2020–07–09 33.73
2020–07–10 33.66
tickers = [‘TSLA’, ‘API’, ‘LMND’,’MRK’]
import matplotlib.pyplot as plt
for i,ticker in enumerate(tickers):
current_ticker = yf.Ticker(ticker)
plt.subplot(len(tickers),1,i+1)
current_ticker.history(period='365d')['Close'].plot(figsize= (16,60), title='1 year price history for ticker: '+ticker)
ticker = tickers[0]
yf_info = yf.Ticker(ticker).info
print(ticker)
# Output:
# TSLA
#an easy way to get 1 year stock growth
yf_info[‘52WeekChange’]
4.8699937
stock_52w_change = []
profitsMargins = []
tickers = ['NVS','JNJ','ABBV','AMGN']
for ticker in tickers:
print(ticker)
current_ticker = yf.Ticker(ticker)
current_ticker_info = current_ticker.info
stock_52w_change.append(current_ticker_info['52WeekChange'])
profitsMargins.append(current_ticker_info['profitMargins'])
import pandas as pd
df = pd.DataFrame([stock_52w_change, profitsMargins], columns=tickers, index=['52w change', 'profitMargins'])
print(df)
# Output
# NVS JNJ ABBV AMGN
# profitMargins -0.06242 0.160992 0.482794 0.469441
# 52w change 0.24318 0.188630 0.247700 0.320250
import matplotlib.ticker as mtick
ax = df.plot.bar()
ax.yaxis.set_major_formatter(mtick.PercentFormatter(xmax=1))
ax.set_title('Comparing Profit Margins and 52 weeks growth rates for pharma stocks')
import pandas_datareader.data as pdr
from datetime import date
end = date.today()
start = datetime(year=end.year-1, month=end.month, day=end.day-2)
# More information of the datasource:
spx_index = pdr.get_data_stooq('^SPX', start, end)
spx_index['Close'].plot(title='1 year price history for index S&P500')
!pip install quandl
london_fixing_gold_price = quandl.get("LBMA/GOLD",start_date=start, end_date=end, authtoken=<your auth token>)
print(london_fixing_gold_price.columns)
# Output:
# Index([‘USD (AM)’, ‘USD (PM)’, ‘GBP (AM)’, ‘GBP (PM)’, ‘EURO (AM)’, ‘EURO (PM)’], dtype=’object’)
london_fixing_gold_price['USD (AM)'].plot(figsize=(20,5), title="Gold Price: London Fixing in USD(AM price)"), plt.show();
#a harder way to get 1 year growth, controlling the exact dates
london_fixing_gold_price['USD (AM)']['2020–07–17'] / london_fixing_gold_price['USD (AM)']['2019–07–17']
# Output:
# 1.2870502569960023
_NYSE_URL = 'https://old.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nyse&render=download'
_NASDAQ_URL = 'https://old.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download'
_AMEX_URL = 'https://old.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=amex&render=download'
!pip install get-all-tickers
from get_all_tickers import get_tickers as gt
list_of_tickers = gt.get_tickers(NYSE=True, NASDAQ=True, AMEX=False)
len(list_of_tickers)
# Output:
# 6144
print(list_of_tickers)