Plot stocks in S&P500

once a month it is good idea to have a look if there were not any big moves in stocks that are not on our regular watchlist to have overall idea what is going on with individual stocks.
Besides selected stocks from S&P500 some other stocks have been added to the list.

In [1]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

# ___library_import_statements___
import pandas as pd

# for pandas_datareader, otherwise it might have issues, sometimes there is some version mismatch
pd.core.common.is_list_like = pd.api.types.is_list_like

# make pandas to print dataframes nicely
pd.set_option('expand_frame_repr', False)  

import pandas_datareader.data as web
import numpy as np
import matplotlib.pyplot as plt
import datetime
import time

#newest yahoo API 
import yfinance as yahoo_finance

#optional 
#yahoo_finance.pdr_override()

%matplotlib inline
In [2]:
import talib as ta
import numpy as np

import matplotlib.pyplot as plt

# was giving me some warnings
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
In [3]:
start_time = datetime.datetime(2000, 1, 1)
#end_time = datetime.datetime(2019, 1, 20)
end_time = datetime.datetime.now().date().isoformat()         # today
In [ ]:
 
In [4]:
def plot_data(df):
    # plot price
    plt.figure(figsize=(15, 2.5))
    plt.title('Stock data ' + str(ticker))
    plt.plot(df.index, df['Adj Close'])
    #plt.title('Price chart (Adj Close) ' + str(ticker))
    plt.show()
    return None
In [5]:
def get_data_plot_data(ticker):
    # yahoo gives only daily historical data
    attempts = 0
    connected = False
    while not connected:
        try:
            df = web.get_data_yahoo(ticker, start=start_time, end=end_time)
            connected = True
            #print('connected to yahoo') 
            #df = df.reset_index()
            plot_data(df)
            
        except Exception as e:
            #print("type error: " + str(e))
            time.sleep( 5 )
            attempts += 1
            if attempts >= 3:
                connected = True
            pass

    # use numerical integer index instead of date
    #df = df.reset_index()
    #print(df.head(5))

    return None
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [6]:
with open('stock_list.txt', 'r') as f:
    # list in following format
    # MMM 3M Company  Industrials Industrial Conglomerates
    #tickers = [f.read().splitlines()[0] for line in f]
    lines = [line for line in f]
In [7]:
#for line in lines:        # all stocks
for line in lines[:10]:    # subset of stocks
    print(line)
    ticker = line.split()[0]
    get_data_plot_data(ticker)
    print('-----------------')
TSLA Tesla

-----------------
BIDU Baidu

-----------------
BABA Alibaba

-----------------
SLV Silver

-----------------
GLD Gold

-----------------
MMM 3M Company  Industrials Industrial Conglomerates

-----------------
ABT Abbott Laboratories Health Care Health Care Equipment

-----------------
ABBV    AbbVie Inc. Health Care Pharmaceuticals

-----------------
ACN Accenture plc   Information Technology  IT Consulting & Other Services

-----------------
ATVI    Activision Blizzard Communication Services  Interactive Home Entertainment

-----------------
In [ ]: