For each symbol in SP500, lets download daily historical data.
Code
data = yf.download(tickers.Symbol.to_list(), start=start_date, end=end_date, interval='1D', auto_adjust=True)data.tail()
[*********************100%***********************] 503 of 503 completed
2 Failed downloads:
['BRK.B']: YFTzMissingError('$%ticker%: possibly delisted; no timezone found')
['BF.B']: YFPricesMissingError('$%ticker%: possibly delisted; no price data found (1d 2020-01-01 -> 2025-01-15)')
Price
Adj Close
Close
...
Volume
Ticker
BF.B
BRK.B
A
AAPL
ABBV
ABNB
ABT
ACGL
ACN
ADBE
...
WTW
WY
WYNN
XEL
XOM
XYL
YUM
ZBH
ZBRA
ZTS
Date
2025-01-07
NaN
NaN
137.410004
242.210007
179.529999
131.289993
113.400002
92.250000
356.390015
422.630005
...
402500
2588200
2195300
3017800
12625900
1232400
2103000
1649100
353800
2488500
2025-01-08
NaN
NaN
137.000000
242.699997
178.500000
130.800003
114.250000
92.660004
357.730011
419.579987
...
655000
3939100
1862300
3714700
17858100
1274500
2025400
2385600
413600
2353200
2025-01-10
NaN
NaN
137.470001
236.850006
175.169998
129.630005
112.309998
90.169998
349.790009
405.920013
...
594700
3529500
2655900
5441000
19304500
1334500
2555000
2709300
460000
3179500
2025-01-13
NaN
NaN
141.949997
234.399994
176.740005
128.850006
113.190002
90.830002
349.140015
408.500000
...
459400
4299500
1850500
3114600
17073400
1154300
2163100
1565800
505200
2306100
2025-01-14
NaN
NaN
143.429993
233.279999
175.550003
127.599998
113.019997
91.989998
348.989990
412.709991
...
441700
4151400
2773800
5952800
11187700
2137000
1793600
1457100
414500
3608200
5 rows × 2517 columns
Calculate Rolling Average
Now, The 100 day rolling average is calculated and compared with latest closing price. Then, the data where the 100 days rolling average is less than latest closing price is filtered