
RE: Ich habe eine Frage und weiß nicht, ob ich einen Thread eröffnen soll?!
| 15.05.2025, 15:30
Der erste Code holt von Yahoo die Daten vom jetzigen SP500 und speicher sie als File. Der zweite Code lädt das File prüft auf die beiden Bedingungen.
Das Zwischenspeichern im File ist notwendig, da Yahoo einen sonst blockiert und die Aktion auch etwas dauert.
Der Code ist nur Rohform: Es wird unnötig viel berechnet statt nur das letzte Datum, auch beim 2yr Hoch wird immer adjusted Close genommen statt High usw.
Mir ist gerade aufgefallen, dass Yahoo im adjusted Close auch Dividenden berücksichtigt und nicht nur Splits.
Die Ausgabe ist (Stand 9.5.2025):
AMCR
CCI
CHTR
CLX
CVS
D
DIS
ES
FRT
HAS
HSIC
KVUE
MDT
MOS
SOLV
TAP
TSN
Das Zwischenspeichern im File ist notwendig, da Yahoo einen sonst blockiert und die Aktion auch etwas dauert.
Code:
import yfinance as yf
import pandas as pd
df = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0] # always returns list of df
sp500_list = df['Symbol'].tolist()
full_stock_data = yf.download(sp500_list, '2010-01-01')
full_stock_data.to_hdf('sp500_list.h5', format='table', key='ticker')
Code:
import pandas as pd
def screen_stock(df):
df['2yr_high'] = df['Close'].rolling('730D').max()
latest = df.iloc[-1] # latest data point
condition1 = latest['Close'].iloc[0] >= 0.8 * latest['2yr_high'].iloc[0]
weekly_close = df['Close'].resample('W-FRI').last().ffill() # Resample to weekly closing prices
# Check for sufficient data and compute 500-week EMA
if len(weekly_close) < 500:
return False
ema_500 = weekly_close.ewm(span=500, adjust=False).mean()
latest_ema = ema_500.iloc[-1]
# Check if the latest close is within 0-10% above the EMA
latest_close = df['Close'].iloc[-1]
condition2 = (latest_close.iloc[0] >= latest_ema.iloc[0]) & (latest_close.iloc[0] <= 1.1 * latest_ema.iloc[0])
return condition1 and condition2
full_stock_data = pd.read_hdf('./sp500_list.h5')
for column_name, column in full_stock_data.items():
if( column_name[0]=='Close')and(column_name[1]=='AMCR'):
if screen_stock(full_stock_data.filter(items=[column_name])):
print(column_name[1])
Der Code ist nur Rohform: Es wird unnötig viel berechnet statt nur das letzte Datum, auch beim 2yr Hoch wird immer adjusted Close genommen statt High usw.
Mir ist gerade aufgefallen, dass Yahoo im adjusted Close auch Dividenden berücksichtigt und nicht nur Splits.
Die Ausgabe ist (Stand 9.5.2025):
AMCR
CCI
CHTR
CLX
CVS
D
DIS
ES
FRT
HAS
HSIC
KVUE
MDT
MOS
SOLV
TAP
TSN