-
Notifications
You must be signed in to change notification settings - Fork 37
/
spy_milestones.py
executable file
·347 lines (291 loc) · 10.1 KB
/
spy_milestones.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env -S uv run --quiet --script
# /// script
# dependencies = [
# "pandas",
# "matplotlib",
# "numpy",
# "highlight_text",
# "yfinance",
# "persistent-cache@git+https://github.com/namuan/persistent-cache"
# ]
# ///
"""
Animate SPY price milestones from inception to present
Usage:
./spy_milestones.py -h
./spy_milestones.py -v # To log INFO messages
./spy_milestones.py -vv # To log DEBUG messages
./spy_milestones.py --save-video output.mp4 # Save animation to video file
"""
import logging
from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import yfinance as yf
from matplotlib.animation import FFMpegWriter, FuncAnimation, PillowWriter
from persistent_cache import PersistentCache
from common import RawTextWithDefaultsFormatter
from common.logger import setup_logging
def parse_args():
parser = ArgumentParser(
description=__doc__, formatter_class=RawTextWithDefaultsFormatter
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
dest="verbose",
help="Increase verbosity of logging output",
)
parser.add_argument(
"-s",
"--symbol",
default="SPY",
help="Stock symbol to analyze (default: SPY)",
)
parser.add_argument(
"--save-video",
type=Path,
help="Save animation to video file (e.g., output.mp4)",
)
return parser.parse_args()
def format_timedelta(td):
years = td.days // 365
months = (td.days % 365) // 30
if years > 0 and months > 0:
return f"{years}y {months}m"
elif years > 0:
return f"{years}y"
else:
return f"{months}m"
FONT_FAMILY = "spot mono"
def animate_spy_milestones(symbol="SPY", output_file=None):
logging.info(f"Animating price milestones for {symbol}")
start_date = "2010-01-01"
end_date = datetime.now().strftime("%Y-%m-%d")
logging.info(f"Downloading data from {start_date} to {end_date}")
stock_data = download_data(end_date, start_date, symbol)
# Resample to weekly data
weekly_data = stock_data.resample("W").max()
logging.debug(f"Resampled to {len(weekly_data)} weekly data points")
# Milestones to track
milestones = [
1000,
10000,
20000,
30000,
40000,
50000,
60000,
70000,
80000,
90000,
100000,
]
milestone_dates = {}
milestone_points = {}
price_point_to_check = "High"
# Find milestone dates and prices
for date, row in weekly_data.iterrows():
price = row[price_point_to_check].item()
for milestone in milestones:
if milestone not in milestone_dates and price >= milestone:
milestone_dates[milestone] = date
milestone_points[milestone] = price
logging.info(
f"Milestone ${milestone} reached on {date:%Y-%m-%d} at ${price:.2f}"
)
break
# Calculate time differences between milestones
time_to_milestone = {}
prev_milestone_date = weekly_data.index[0]
for milestone in milestones:
if milestone in milestone_dates:
current_date = milestone_dates[milestone]
time_diff = current_date - prev_milestone_date
time_to_milestone[milestone] = time_diff
logging.debug(
f"Time to milestone ${milestone}: {format_timedelta(time_diff)}"
)
prev_milestone_date = current_date
# Set up the figure
plt.style.use("dark_background")
fig, ax = plt.subplots(figsize=(15, 8))
fig.patch.set_facecolor("#1C1C1C")
ax.set_facecolor("#1C1C1C")
(line,) = ax.plot([], [], color="#00BFD8", linewidth=2)
scatter = ax.scatter([], [], color="yellow", s=150, zorder=5)
ax.grid(False)
ax.set_axis_off()
# Calculate padded limits
y_max = weekly_data[price_point_to_check].max().item() * 1.1
y_min = weekly_data[price_point_to_check].min().item() * 0.9
x_range = weekly_data.index[-1] - weekly_data.index[0]
x_padding = x_range * 0.1 # 5% padding
y_range = y_max - y_min
y_padding = y_range * 0.1 # 10% padding
# Set padded limits
ax.set_ylim(y_min - y_padding, y_max + y_padding)
ax.set_xlim(weekly_data.index[0] - x_padding, weekly_data.index[-1] + x_padding)
annotations = []
current_milestones = set()
fade_states = {}
fade_in_frames = 15
def calculate_alpha(milestone, frame, milestone_frame):
if milestone not in fade_states:
fade_states[milestone] = {"start_frame": frame}
frames_elapsed = frame - fade_states[milestone]["start_frame"]
return min(frames_elapsed / fade_in_frames, 1.0)
def animate(frame):
for ann in annotations:
ann.remove()
annotations.clear()
current_date = weekly_data.index[frame]
mask = weekly_data.index <= current_date
line.set_data(weekly_data.index[mask], weekly_data[price_point_to_check][mask])
scatter_points = []
current_price = int(weekly_data[price_point_to_check].iloc[frame].item())
for milestone in milestones:
if milestone not in current_milestones:
if current_price >= milestone and milestone in milestone_dates:
current_milestones.add(milestone)
logging.debug(f"Frame {frame}: Adding milestone ${milestone}")
for milestone in current_milestones:
date = milestone_dates[milestone]
price = milestone_points[milestone]
milestone_frame = weekly_data.index.get_loc(date)
alpha = calculate_alpha(milestone, frame, milestone_frame)
scatter_points.append([date, price])
time_text = (
f"${milestone}\n{date.strftime('%Y-%m-%d')}"
if milestone == milestones[0]
else f"${milestone}\n{date.strftime('%Y-%m-%d')}\nTime: {format_timedelta(time_to_milestone[milestone])}"
)
ann = ax.annotate(
time_text,
xy=(date, price),
xytext=(-120, 40),
textcoords="offset points",
bbox=dict(
boxstyle="round,pad=0.5", fc="#2C2C2C", ec="none", alpha=alpha * 1
),
arrowprops=dict(
arrowstyle="->",
connectionstyle="arc3,rad=-0.3",
color="#FF0000",
),
color="#FFFFFF",
fontfamily=FONT_FAMILY,
fontweight="bold",
fontsize=12,
)
annotations.append(ann)
scatter.set_offsets(
np.array(scatter_points) if scatter_points else np.empty((0, 2))
)
# Add last price annotation if we have data
last_price = weekly_data[price_point_to_check][mask].iloc[-1].item()
last_date = weekly_data.index[mask][-1]
last_price_ann = ax.annotate(
current_price,
xy=(last_date, last_price),
xytext=(10, 0),
textcoords="offset points",
color="#00BFD8",
fontfamily=FONT_FAMILY,
fontsize=12,
fontweight="bold",
ha="left",
va="center",
)
annotations.append(last_price_ann)
# Add title in top left
title_ann = ax.annotate(
"$BTC - Journey towards 100000",
xy=(0.10, 0.8), # Position in axes coordinates
xycoords="axes fraction",
color="#FFFFFF",
fontfamily=FONT_FAMILY,
fontsize=40,
alpha=0.8,
)
annotations.append(title_ann)
# Add year display in top left
year_ann = ax.annotate(
f"{current_date.year}",
xy=(0.10, 0.7), # Position in axes coordinates
xycoords="axes fraction",
color="#666666", # Grey color
fontfamily=FONT_FAMILY,
fontsize=60,
fontweight="bold",
alpha=0.8,
)
annotations.append(year_ann)
# credit annotation
plt.figtext(
0.84,
0.13,
"Developed by ",
ha="right",
va="bottom",
fontsize=10,
fontfamily=FONT_FAMILY,
color="#FFFFFF",
)
plt.figtext(
0.9,
0.13,
"@namuan_twt",
ha="right",
va="bottom",
fontsize=10,
fontfamily=FONT_FAMILY,
color="lightblue",
)
plt.figtext(
0.9,
0.1,
"Data from Yahoo Finance",
ha="right",
va="bottom",
fontsize=10,
fontfamily=FONT_FAMILY,
color="#FFFFFF",
)
return [line, scatter] + annotations
frames = len(weekly_data)
logging.info(f"Creating animation with {frames} frames")
anim = FuncAnimation(
fig, animate, frames=frames, interval=10, blit=True, repeat=False
)
if output_file:
# Create parent directories if they don't exist
output_file.parent.mkdir(parents=True, exist_ok=True)
logging.info(f"Saving animation to {output_file}")
extension = output_file.suffix.lower()
if extension == ".mp4":
writer = FFMpegWriter(fps=30, bitrate=2000)
else: # Default to GIF
writer = PillowWriter(fps=30)
anim.save(str(output_file), writer=writer)
plt.close()
else:
plt.tight_layout()
plt.show()
return milestone_dates
@PersistentCache()
def download_data(end_date, start_date, symbol):
stock_data = yf.download(symbol, start=start_date, end=end_date)
return stock_data
def main(args):
logging.info(f"Starting SPY milestones animation for symbol: {args.symbol}")
animate_spy_milestones(args.symbol, args.save_video)
logging.info("Animation completed")
if __name__ == "__main__":
args = parse_args()
setup_logging(args.verbose)
main(args)