Created
December 30, 2023 01:00
-
-
Save dharmatech/d07956f46dafbb5abfe2c71d68f1a290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yfinance_download | |
from bokeh.plotting import figure, show | |
from bokeh.models import NumeralTickFormatter, HoverTool | |
import bokeh.models | |
import bokeh.palettes | |
import bokeh.transform | |
df = yfinance_download.update_records('^GSPC', interval='1d') | |
# keep only the last record of each year | |
tmp = df.groupby(df.index.year).agg({'Close': 'last'}) | |
tmp['Close_1'] = tmp['Close'].shift(1) | |
tmp['pct'] = (tmp['Close'] - tmp['Close_1']) / tmp['Close_1'] * 100 | |
tmp.tail(40) | |
# create bokeh chart from tmp dataframe. x-axis is Date. y-axis is pct. | |
p = figure( | |
title='S&P 500 Yearly Returns', | |
x_axis_label='Year', | |
y_axis_label='%', | |
x_axis_type='datetime', | |
sizing_mode='stretch_both' | |
) | |
p.vbar( | |
x='Date', | |
top='pct', | |
source=tmp, | |
line_color='black', | |
line_width=1, | |
alpha=0.5 | |
) | |
p.xaxis.formatter = NumeralTickFormatter(format='0') | |
p.yaxis.formatter = NumeralTickFormatter(format='0') | |
p.add_tools( | |
HoverTool( | |
tooltips=[ | |
('Year', '@Date'), | |
('%', '@pct{0.00}'), | |
], | |
formatters={ | |
'@Date': 'datetime', | |
}, | |
mode='vline' | |
) | |
) | |
p.xaxis.ticker = bokeh.models.DatetimeTicker(desired_num_ticks=40) | |
show(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment