-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstocks.py
More file actions
276 lines (221 loc) · 10.6 KB
/
Copy pathstocks.py
File metadata and controls
276 lines (221 loc) · 10.6 KB
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
# /// script
# description = "Window functions for stocks - demonstrates various window functions on real stock data"
# requires-python = ">=3.12, <3.13"
# dependencies = ["daft>=0.7.10", "yfinance>=0.2.0", "pandas>=2.0.0"]
# ///
import yfinance as yf
import daft
from daft import Window, col
from daft.functions import rank
# -----------------------------------------------------------------------------
# Data Loading: Fetch Historical Stock Data
# -----------------------------------------------------------------------------
def load_stock_data(tickers=["AAPL", "MSFT", "GOOGL", "AMZN", "TSLA"], period="1y"):
"""
Load historical stock data using yfinance (Yahoo Finance).
Args:
tickers: List of stock ticker symbols
period: Time period (e.g., "1y", "6mo", "2y")
Returns:
Daft DataFrame with columns: ticker, date, open, high, low, close, volume
"""
print(f"Fetching stock data for {tickers} over period: {period}...")
data = []
for ticker in tickers:
try:
stock = yf.Ticker(ticker)
hist = stock.history(period=period)
for date, row in hist.iterrows():
data.append(
{
"ticker": ticker,
"date": date.strftime("%Y-%m-%d"),
"open": float(row["Open"]),
"high": float(row["High"]),
"low": float(row["Low"]),
"close": float(row["Close"]),
"volume": int(row["Volume"]),
}
)
except Exception as e:
print(f"Error fetching data for {ticker}: {e}")
print(f"Loaded {len(data)} rows of stock data")
return daft.from_pylist(data)
# -----------------------------------------------------------------------------
# Window Function Examples
# -----------------------------------------------------------------------------
if __name__ == "__main__":
# Load the data
df = load_stock_data()
# Show the data
df.sort(["ticker", "date"]).limit(10).show()
# -----------------------------------------------------------------------------
# Example 1: Calculate Daily Returns (Price Change %)
# Uses: partition_by + order_by + lag
# -----------------------------------------------------------------------------
print("\n" + "=" * 80)
print("EXAMPLE 1: Daily Returns")
print("=" * 80)
by_ticker_date = Window().partition_by("ticker").order_by("date")
df_with_returns = df.with_column(
"daily_return",
((col("close") - col("close").lag(1).over(by_ticker_date)) / col("close").lag(1).over(by_ticker_date) * 100),
).sort(["ticker", "date"])
print(df_with_returns.select("ticker", "date", "close", "daily_return").limit(15).collect())
# -----------------------------------------------------------------------------
# Example 2: Moving Averages (5-day and 20-day)
# Uses: partition_by + order_by + rows_between
# -----------------------------------------------------------------------------
print("\n" + "=" * 80)
print("EXAMPLE 2: Moving Averages")
print("=" * 80)
by_ticker_date_5day = (
Window().partition_by("ticker").order_by("date").rows_between(-4, 0) # Current row + 4 preceding = 5 days
)
by_ticker_date_20day = (
Window().partition_by("ticker").order_by("date").rows_between(-19, 0) # Current row + 19 preceding = 20 days
)
df_with_ma = (
df_with_returns.with_column("ma_5", col("close").mean().over(by_ticker_date_5day))
.with_column("ma_20", col("close").mean().over(by_ticker_date_20day))
.sort(["ticker", "date"])
)
print(df_with_ma.select("ticker", "date", "close", "ma_5", "ma_20").limit(25).collect())
# -----------------------------------------------------------------------------
# Example 3: Rank Stocks by Daily Volume
# Uses: partition_by + order_by + rank
# -----------------------------------------------------------------------------
print("\n" + "=" * 80)
print("EXAMPLE 3: Daily Volume Rankings (Top 3 per day)")
print("=" * 80)
by_date_volume = Window().partition_by("date").order_by("volume", desc=True)
df_with_volume_rank = (
df.with_column("volume_rank", rank().over(by_date_volume))
.filter(col("volume_rank") <= 3)
.sort(["date", "volume_rank"])
)
print(df_with_volume_rank.select("date", "ticker", "volume", "volume_rank").limit(20).collect())
# -----------------------------------------------------------------------------
# Example 4: Cumulative Trading Volume per Stock
# Uses: partition_by + order_by (unbounded frame)
# -----------------------------------------------------------------------------
print("\n" + "=" * 80)
print("EXAMPLE 4: Cumulative Trading Volume")
print("=" * 80)
df_with_cumulative = df.with_column("cumulative_volume", col("volume").sum().over(by_ticker_date)).sort(
["ticker", "date"]
)
print(df_with_cumulative.select("ticker", "date", "volume", "cumulative_volume").limit(20).collect())
# -----------------------------------------------------------------------------
# Example 5: Identify Highest Volatility Days (using high-low range)
# Uses: partition_by + order_by + rank
# -----------------------------------------------------------------------------
print("\n" + "=" * 80)
print("EXAMPLE 5: Most Volatile Days per Stock (Top 5)")
print("=" * 80)
df_with_volatility = df.with_column("daily_range", col("high") - col("low")).with_column(
"daily_range_pct", (col("high") - col("low")) / col("low") * 100
)
by_ticker_volatility = Window().partition_by("ticker").order_by("daily_range_pct", desc=True)
df_top_volatile = (
df_with_volatility.with_column("volatility_rank", rank().over(by_ticker_volatility))
.filter(col("volatility_rank") <= 5)
.sort(["ticker", "volatility_rank"])
)
print(df_top_volatile.select("ticker", "date", "low", "high", "daily_range_pct", "volatility_rank").collect())
# -----------------------------------------------------------------------------
# Example 6: Stock Performance Rankings (Total Return over Period)
# Uses: window function to get first close, then calculate total return
# -----------------------------------------------------------------------------
print("\n" + "=" * 80)
print("EXAMPLE 6: Stock Performance Rankings (Total Return %)")
print("=" * 80)
by_ticker_date_first = (
Window().partition_by("ticker").order_by("date").rows_between(-999999, 0) # Unbounded - all previous rows
)
df_with_first_close = df.with_column("first_close", col("close").min().over(by_ticker_date_first)).with_column(
"total_return_pct", (col("close") - col("first_close")) / col("first_close") * 100
)
# Get the most recent date for each stock
by_ticker_last_date = Window().partition_by("ticker").order_by("date", desc=True)
df_latest_performance = df_with_first_close.with_column("date_rank", rank().over(by_ticker_last_date)).filter(
col("date_rank") == 1
)
by_performance = Window().order_by("total_return_pct", desc=True)
df_performance_ranking = df_latest_performance.with_column("performance_rank", rank().over(by_performance)).sort(
"performance_rank"
)
print(
df_performance_ranking.select(
"performance_rank", "ticker", "date", "first_close", "close", "total_return_pct"
).collect()
)
# -----------------------------------------------------------------------------
# Example 7: Golden Cross / Death Cross Detection
# (When 5-day MA crosses 20-day MA)
# Uses: Moving averages + lag to detect crossovers
# -----------------------------------------------------------------------------
print("\n" + "=" * 80)
print("EXAMPLE 7: Golden Cross / Death Cross Detection")
print("=" * 80)
df_with_signals = (
df_with_ma.with_column("ma_5_prev", col("ma_5").lag(1).over(by_ticker_date))
.with_column("ma_20_prev", col("ma_20").lag(1).over(by_ticker_date))
.with_column(
"golden_cross",
(col("ma_5_prev") <= col("ma_20_prev")) & (col("ma_5") > col("ma_20")),
)
.with_column(
"death_cross",
(col("ma_5_prev") >= col("ma_20_prev")) & (col("ma_5") < col("ma_20")),
)
)
df_crossovers = df_with_signals.filter(col("golden_cross") | col("death_cross")).sort(["ticker", "date"])
print(df_crossovers.select("ticker", "date", "close", "ma_5", "ma_20", "golden_cross", "death_cross").collect())
# -----------------------------------------------------------------------------
# Example 8: Rolling Max and Min (52-week high/low)
# Uses: partition_by + order_by + rows_between with max/min aggregations
# -----------------------------------------------------------------------------
print("\n" + "=" * 80)
print("EXAMPLE 8: Rolling 52-Week High/Low")
print("=" * 80)
# Approximate 52 weeks as 252 trading days
by_ticker_52week = Window().partition_by("ticker").order_by("date").rows_between(-251, 0)
df_with_52week = (
df.with_column("week_52_high", col("high").max().over(by_ticker_52week))
.with_column("week_52_low", col("low").min().over(by_ticker_52week))
.with_column(
"distance_from_high_pct",
(col("close") - col("week_52_high")) / col("week_52_high") * 100,
)
)
# Get latest data for each stock
df_52week_latest = (
df_with_52week.with_column("date_rank", rank().over(by_ticker_last_date))
.filter(col("date_rank") == 1)
.sort("ticker")
)
print(
df_52week_latest.select(
"ticker",
"date",
"close",
"week_52_high",
"week_52_low",
"distance_from_high_pct",
).collect()
)
print("\n" + "=" * 80)
print("WINDOW FUNCTIONS DEMONSTRATED:")
print("=" * 80)
print("""
1. Daily Returns - Using lag() to compare with previous day
2. Moving Averages - Using rows_between() for rolling windows
3. Volume Rankings - Using rank() to rank across different partitions
4. Cumulative Volume - Using unbounded window for running totals
5. Volatility Rankings - Finding top N values per partition
6. Performance Rankings - Using first() to calculate total returns
7. Technical Signals - Combining multiple window functions for trading signals
8. 52-Week High/Low - Using max/min with rolling windows
All done! Try modifying the tickers or period to explore different stocks.
""")