Mein Code sieht wie folgt aus:
import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
import yfinance as yf
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import os
from pandas import ExcelWriter
yf.pdr_override()
start = dt.datetime(2017, 12, 1)
now = dt.datetime.now()
root = Tk()
ftypes = [(".xlsm", "*.xlsx", ".xls")]
ttl = "Title"
dir1 = 'C:\\'
filePath = askopenfilename(filetypes=ftypes, initialdir=dir1, title=ttl)
filePath = r"C:\Pfad meiner Datei
stocklist = pd.read_excel(filePath)
stocklist = stocklist.head()
exportList = pd.DataFrame(
columns=['Stock', "RS_Rating", "50 Day MA", "150 Day Ma", "200 Day MA", "52 Week Low", "52 week High"])
for i in stocklist.index:
stock = str(stocklist["Symbol"])
RS_Rating = stocklist["RS Rating"]
try:
df = pdr.get_data_yahoo(stock, start, now)
smaUsed = [50, 150, 200]
for x in smaUsed:
sma = x
df["SMA_" + str(sma)] = round(df.iloc[:, 4].rolling(window=sma).mean(), 2)
currentClose = df["Adj Close"][-1]
moving_average_50 = df["SMA_50"][-1]
moving_average_150 = df["SMA_150"][-1]
moving_average_200 = df["SMA_200"][-1]
low_of_52week = min(df["Adj Close"][-260:])
high_of_52week = max(df["Adj Close"][-260:])
try:
moving_average_200_20 = df["SMA_200"][-20]
except Exception:
moving_average_200_20 = 0
# Condition 1: Current Price > 150 SMA and > 200 SMA
if (currentClose > moving_average_150 > moving_average_200):
cond_1 = True
else:
cond_1 = False
# Condition 2: 150 SMA and > 200 SMA
if (moving_average_150 > moving_average_200):
cond_2 = True
else:
cond_2 = False
# Condition 3: 200 SMA trending up for at least 1 month (ideally 4-5 months)
if (moving_average_200 > moving_average_200_20):
cond_3 = True
else:
cond_3 = False
# Condition 4: 50 SMA> 150 SMA and 50 SMA> 200 SMA
if (moving_average_50 > moving_average_150 > moving_average_200):
# print("Condition 4 met")
cond_4 = True
else:
# print("Condition 4 not met")
cond_4 = False
# Condition 5: Current Price > 50 SMA
if (currentClose > moving_average_50):
cond_5 = True
else:
cond_5 = False
# Condition 6: Current Price is at least 30% above 52 week low (Many of the best are up 100-300% before coming out of consolidation)
if (currentClose >= (1.3 * low_of_52week)):
cond_6 = True
else:
cond_6 = False
# Condition 7: Current Price is within 25% of 52 week high
if (currentClose >= (.75 * high_of_52week)):
cond_7 = True
else:
cond_7 = False
# Condition 8: IBD RS rating >70 and the higher the better
if (RS_Rating > 70):
cond_8 = True
else:
cond_8 = False
# Own condition 9: Price > 15$
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)
except Exception:
print("No data on " + stock)
print(exportList)
newFile = os.path.dirname(filePath) + "/ScreenOutput.xlsx"
writer = ExcelWriter(newFile)
exportList.to_excel(writer, "Sheet1")
writer.save()
Ich bin Python Anfänger und es kommt beim ausführen des Skripts folgender Fehler:
Traceback (most recent call last):
File "C:\Users\Pfad", line 3361, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Symbol'
The above exception was the direct cause of the following exception:
Was muss ich tun ? Ich wäre für jede Hilfe dankbar
Key error
Die Fehlermeldung ist recht eindeutig. `stocklist` (wie auch immer die aussieht) hat keine Spalte `Symbol`.
Warum benutzt Du in `for x in smaUsed` `x` um es dann gleich in `sma` umzunennen?
Die Bedingungen kann man auch direkt einer Variable zuweisen:
Hier entspricht aber der Kommentar nicht dem Code. Was davon ist richtig?
Das Exceptionhandling ist etwas sehr vereinfachend.
Warum benutzt Du in `for x in smaUsed` `x` um es dann gleich in `sma` umzunennen?
Die Bedingungen kann man auch direkt einer Variable zuweisen:
Code: Alles auswählen
# Condition 1: Current Price > 150 SMA and > 200 SMA
cond_1 = currentClose > moving_average_150 > moving_average_200
Das Exceptionhandling ist etwas sehr vereinfachend.
Sirius3 hat geschrieben: Freitag 24. Dezember 2021, 11:47 Die Fehlermeldung ist recht eindeutig. `stocklist` (wie auch immer die aussieht) hat keine Spalte `Symbol`.
Also vielen dank, ich hatte einfach in der "stocklist" einen tippfehler...
Warum benutzt Du in `for x in smaUsed` `x` um es dann gleich in `sma` umzunennen?
Die Bedingungen kann man auch direkt einer Variable zuweisen:
OK das werd ich dann noch ändernHier entspricht aber der Kommentar nicht dem Code. Was davon ist richtig?Code: Alles auswählen
# Condition 1: Current Price > 150 SMA and > 200 SMA cond_1 = currentClose > moving_average_150 > moving_average_200
Wieso entspricht der Code nicht dem Kommentar ? Ich überprüfe doch ob der aktuelle preis größer als der 150 / 200er moving average ist ?
Das Exceptionhandling ist etwas sehr vereinfachend.
Dein Kommentar sagt:Keelo61 hat geschrieben: Freitag 24. Dezember 2021, 12:28Hier entspricht aber der Kommentar nicht dem Code. Was davon ist richtig?Code: Alles auswählen
# Condition 1: Current Price > 150 SMA and > 200 SMA cond_1 = currentClose > moving_average_150 > moving_average_200
Wieso entspricht der Code nicht dem Kommentar ? Ich überprüfe doch ob der aktuelle preis größer als der 150 / 200er moving average ist ?
Ist "Current Price" größer als 150 irgendwas UND ist "Current Price" größer als 200 irgendwas.
Dein Code sagt:
Wahr ist wenn: "currentClose" ist größer als "moving_average_150" UND ist "moving_average_150" größer als "moving_average_200".
Ist schon ein gewaltiger Unterschied.