!pip install yahoofinance
!pip install plotly
!pip install kaleido
!pip install psutil
from fastbook import *
import yahoofinance as yf
import plotly.express as px
import os
profile = yf.HistoricalPrices('SPY', "2000-01-01", "2021-01-01", frequency="1d")
df = profile.to_dfs()["Historical Prices"]
df.head(10)
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2000-01-03 | 148.25000 | 148.25000 | 143.875000 | 145.43750 | 97.825676 | 8164300 |
2000-01-04 | 143.53125 | 144.06250 | 139.640625 | 139.75000 | 94.000107 | 8089800 |
2000-01-05 | 139.93750 | 141.53125 | 137.250000 | 140.00000 | 94.168259 | 12177900 |
2000-01-06 | 139.62500 | 141.50000 | 137.750000 | 137.75000 | 92.654861 | 6227200 |
2000-01-07 | 140.31250 | 145.75000 | 140.062500 | 145.75000 | 98.035896 | 8066500 |
2000-01-10 | 146.25000 | 146.90625 | 145.031250 | 146.25000 | 98.372215 | 5741700 |
2000-01-11 | 145.81250 | 146.09375 | 143.500000 | 144.50000 | 97.195099 | 7503700 |
2000-01-12 | 144.59375 | 144.59375 | 142.875000 | 143.06250 | 96.228210 | 6907700 |
2000-01-13 | 144.46875 | 145.75000 | 143.281250 | 145.00000 | 97.531441 | 5158300 |
2000-01-14 | 146.53125 | 147.46875 | 145.968750 | 146.96875 | 98.855675 | 7437300 |
df.shape
(5284, 6)
df.duplicated().values.any()
False
df.isna().values.any()
False
fig = px.line(df, x=df.index, y="Adj Close", title='SPY')
fig.show()
if not os.path.exists("data2"):
os.mkdir("data2")
past_five = []
for index, row in df.iterrows():
if len(past_five) == 5:
to_graph = [val - past_five[0] for val in past_five]
fig = px.line(to_graph)
fig.update_layout(showlegend=False)
ret = row["Adj Close"] > past_five[-1]
fig.write_image(f"data2/{ret}_{index}.png")
del past_five[0]
past_five.append(row["Adj Close"])