ich habe mir einige Datensätze von Yahoo finance runtergeladen und will diese nun auf bestimmte Bedingungen überprüfen und wenn diese Bedingungen erfüllt (True) sind, die betreffenden Unternehmen (ticker) als Dataframe anzeigen lassen.
hier mal die entsprechende Funktion:
Code: Alles auswählen
def createPortfolioLocal(self, parent, controller, lbl_PortfolioOverview):
starttime = time.time()
path = 'Data/HistoricalData'
exportlist = pd.DataFrame(columns=['Stock', 'RS_Rating', '50 Day MA', '150 Day MA', '200 Day MA', '52 Week Low', '52 Week High'])
for filename in os.listdir(path):
try:
df_rs_rating = pd.read_csv(os.path.join(path, filename))
df_rs_rating = pd.DataFrame(df_rs_rating)
df_rs_rating['Diff Vortag'] = df_rs_rating['Adj Close'].diff()
delta = df_rs_rating['Adj Close'].diff()
up = delta.clip(lower=0)
down = -1*delta.clip(upper=0)
ema_up = up.ewm(com=13, adjust=False).mean()
ema_down = down.ewm(com=13, adjust=False).mean()
rs = ema_up/ema_down
df_rs_rating['RSI'] = 100 - (100/(1 + rs))
df_rs_rating['ticker'] = filename[:-4]
stock = df_rs_rating['ticker']
RS_Rating = df_rs_rating['RSI']
df_rs_rating.reset_index(inplace=False)
df_rs_rating.set_index('Date', inplace=True)
# Skip first 14 days to have real values
df_rs_rating = df_rs_rating.iloc[14:]
except Exception:
print('sth. went wrong')
print(Exception.with_traceback())
try:
smaUsed = [50, 150, 200]
for x in smaUsed:
df_rs_rating['SMA_' + str(x)] = round(df_rs_rating.iloc[:,5].rolling(window=x).mean(), 2)
currentClose = df_rs_rating['Adj Close'][-1]
moving_average_50 = df_rs_rating['SMA_50'][-1]
moving_average_150 = df_rs_rating['SMA_150'][-1]
moving_average_200 = df_rs_rating['SMA_200'][-1]
low_of_52week = min(df_rs_rating['Adj Close'][-260:])
high_of_52week = max(df_rs_rating['Adj Close'][-260:])
try:
moving_average_200_20past = df_rs_rating['SMA_200'][-20]
except Exception:
moving_average_200_20past = 0
print('Checking ' + filename + ' ...')
print(df_rs_rating.tail())
# Condition 1: Current price > 150 SMA and > 200 SMA
if (currentClose > moving_average_150 and currentClose > moving_average_200):
cond_1 = True
# print(currentClose)
# print(moving_average_150)
# print(moving_average_200)
print('cond_1 is True')
else:
cond_1 = False
# print(currentClose)
# print(moving_average_150)
# print(moving_average_200)
print('cond_1 is False')
# Condition 2: 150 SMA > 200 SMA
if (moving_average_150 > moving_average_200):
cond_2 = True
# print('cond_2 is True')
else:
cond_2 = False
# print('cond_2 is False')
# # Condition3: 200 SMA trending up for at least 1 month (ideally 4-5 months)
# if (moving_average_200 > moving_average_200_20past):
# cond_3 = True
# # print('cond_3 is True')
# else:
# cond_3 = False
# # print('cond_3 is False')
# # Condition 4: 50 SMA > 150 SMA and 50 SMA > 200 SMA
# if (moving_average_50 > moving_average_150 and moving_average_50 > moving_average_200):
# cond_4 = True
# # print('cond_4 is True')
# else:
# cond_4 = False
# # print('cond_4 is False')
# # Condition 5: Current price > 50 SMA
# if (currentClose > moving_average_50):
# cond_5 = True
# # print('cond_5 is True')
# else:
# cond_5 = False
# # print('cond_5 is False')
# # Condition 6: Current price is at least 30% above 52 weeks low (many of the best are up to 100 - 300% above)
# if (currentClose >= (1.3*low_of_52week)):
# cond_6 = True
# # print('cond_6 is True')
# else:
# cond_6 = False
# # print('cond_6 is False')
# # Condition 7: Current price is within 25% of 52 week high
# if (currentClose >= (.75*high_of_52week)):
# cond_7 = True
# # print('cond_7 is True')
# else:
# cond_7 = False
# # print('cond_7 is False')
# # Condition 8: (IBD) RS rating > 70 and the higher the better
# if (RS_Rating > 70):
# cond_8 = True
# # print('cond_8 is True')
# else:
# cond_8 = False
# # print('cond_8 is False')
# if(cond_1 and cond_2 and cond_3 and cond_4 and cond_5 and cond_6 and cond_7 and cond_8):
# exportlist = exportlist.append({'Stock': stock, 'RS_Rating': RS_Rating,'50 Day MA': moving_average_50, '150 Day MA': moving_average_150, '200 Day MA': moving_average_200, '52 Week Low': low_of_52week, '52 Week High': high_of_52week}, ignore_index=True)
if(cond_1 and cond_2):
exportlist = exportlist.append({'Stock': stock, 'RS_Rating': RS_Rating,'50 Day MA': moving_average_50, '150 Day MA': moving_average_150, '200 Day MA': moving_average_200, '52 Week Low': low_of_52week, '52 Week High': high_of_52week}, ignore_index=True)
except Exception:
print('No Data on ' + stock)
print(Exception.with_traceback())
exportlist.to_csv('exportlist.csv')
print(exportlist.head())
print(len(exportlist))
self.lbl_PortfolioOverview.configure(text = exportlist)
endtime = time.time()
print(endtime - starttime)
hier mal ein Auszug der Ausgabe:
Code: Alles auswählen
Stock RS_Rating ... 52 Week Low 52 Week High0 0 1COV.DE
1 1COV.DE
2 1COV.D... 0 NaN
1 0.000000
2 0... ... 22.177183 61.900002
1 0 A
1 A
2 A
3 A
4 ... 0 NaN
1 0.000000
2 0... ... 62.860069 130.119995
2 0 AAL
1 AAL
2 AAL
3 AA... 0 NaN
1 100.000000
2 ... ... 9.040000 30.090000
3 0 AAP
1 AAP
2 AAP
3 AA... 0 NaN
1 100.000000
2 ... ... 74.666817 176.669998
4 0 AAPL
1 AAPL
2 AAPL
3 ... 0 NaN
1 0.000000
2 1... ... 55.661041 142.946396
Die Berechnungen klappen. die Ausgabe von 'print(df_rs_rating.tail())' sieht aus wie erwartet.
Nur der DataFrame 'exportlist' sieht halt aus wie er aussieht.
Besten Dank im Voraus und noch einen schönen Tag zusammen
TechHippie420