import numpy as np import pandas as pd import matplotlib.pyplot as plt from openbb import obb import warnings warnings.filterwarnings("ignore") obb.user.preferences.output_type = "dataframe" import os import math import plotly.graph_objects as go from dash import Dash, dcc, html FILEPATH = "c:/Users/steve/Downloads/" SP500_MONTHLY_FILENAME = 'sp500_monthly_historical.csv' # ## Graph the long-term S&P 500 and Shiller P/E # ### Monthly prices of S&P 500 # yfinance data only goes back to 1985, so read in a file with monthly S&P 500 prices since 1950 historical_sp500_monthly = pd.read_csv(FILEPATH + SP500_MONTHLY_FILENAME,index_col='date') historical_sp500_monthly.index = pd.to_datetime(historical_sp500_monthly.index) # Read monthly data since 2005 from yfinance sp500mo_current = obb.index.price.historical(symbol='^GSPC', start_date='2005-01-01', provider='yfinance', interval='1M') sp500mo_current.index = pd.to_datetime(sp500mo_current.index) # Splice the two dataframes together # Remove any overlapping data sp500mo_current.loc[sp500mo_current.index > historical_sp500_monthly.index[-1]] # Append the new data to the old sp500mo = pd.concat([historical_sp500_monthly, sp500mo_current]) # ### Monthly Shiller P/E ratios shiller = obb.index.sp500_multiples(series_name='shiller_pe_month', provider='multpl', start_date='1950-01-01') # Calculate the 30-year average of the Shiller P/E shiller['30_yr_avg'] = shiller['value'].rolling(360).mean() # ### Produce graphs # Graph the S&P 500 with a log scale monthly_fig = go.Figure(data=[go.Candlestick( x=sp500mo.index, open=sp500mo['open'], high=sp500mo['high'], low=sp500mo['low'], close=sp500mo['close'], name="Price" )]) monthly_fig.update_yaxes(type="log") monthly_fig.update_layout( title=f"S&P 500 monthly", xaxis_rangeslider_visible=False, # template="plotly_dark", height=600, width=1075 ) # Graph the Shiller P/E shiller_fig = go.Figure() shiller_fig.add_trace(go.Scatter(x=shiller.index, y=shiller['value'], line=dict(color="blue"), name="Shiller P/E" ) ) # Add moving averages shiller_fig.add_trace(go.Scatter(x=shiller.index, y=shiller['30_yr_avg'], line=dict(color='orange'), name = '30-year avg')) shiller_fig.update_layout(title="Shiller P/E", #template="plotly_dark", height=600, width=1075)