Ich habe den folgenden Code, der gut arbeitet:Nameerror name ‚var‘ nicht in IF-Anweisung definiert
for index, row in dfcodes.iterrows():
# for each security code identify those with 3 down candles and narrowing raneg for first 3 days of 6 day set
scode=row['Security_Code']
tdate='2016-01-15'
df=fetch_last(scode,tdate,6)
dfreverse=df.sort('TradeDate', ascending=True)
#look for 3 consecutive down candles with narrowing range and then 2 up candles
dfdownbars=dfreverse.head(5)
ncnt=1
for index,row in dfdownbars.iterrows():
otmp = row['Opening_Price']
ctmp = row['Closing_Price']
rtmp = abs(row['Opening_Price'] - row['Closing_Price'])
dtmp = row['TradeDate']
if ctmp<otmp and ncnt==1:
o1 = otmp
c1 = ctmp
r1 = rtmp
d1 = dtmp
ncnt+=1
elif ctmp<otmp and otmp<o1 and ctmp<c1 and rtmp<=r1 and ncnt==2:
o2 = otmp
c2 = ctmp
r2 = rtmp
d2 = dtmp
ncnt += 1
elif ctmp<otmp and otmp<o2 and rtmp<=r2 and ncnt==3:
o3 = otmp
c3 = ctmp
r3 = rtmp
d3 = dtmp
ncnt += 1
else:
break
aber sobald ich einen 4. elif füge ich die folgende Fehlermeldung erhalten:
elif ctmp> otmp und CTMP> c3 und Nzähl == 4: Nameerror: name 'c3' ist nicht
definiertdh der fehlerhafte Code nun wie folgt aussieht:
for index, row in dfcodes.iterrows():
# for each security code identify those with 3 down candles and narrowing raneg for first 3 days of 6 day set
scode=row['Security_Code']
tdate='2016-01-15'
df=fetch_last(scode,tdate,6)
dfreverse=df.sort('TradeDate', ascending=True)
#look for 3 consecutive down candles with narrowing range and then 2 up candles
dfdownbars=dfreverse.head(5)
ncnt=1
for index,row in dfdownbars.iterrows():
otmp = row['Opening_Price']
ctmp = row['Closing_Price']
rtmp = abs(row['Opening_Price'] - row['Closing_Price'])
dtmp = row['TradeDate']
if ctmp<otmp and ncnt==1:
o1 = otmp
c1 = ctmp
r1 = rtmp
d1 = dtmp
ncnt+=1
elif ctmp<otmp and otmp<o1 and ctmp<c1 and rtmp<=r1 and ncnt==2:
o2 = otmp
c2 = ctmp
r2 = rtmp
d2 = dtmp
ncnt += 1
elif ctmp<otmp and otmp<o2 and rtmp<=r2 and ncnt==3:
o3 = otmp
c3 = ctmp
r3 = rtmp
d3 = dtmp
ncnt += 1
elif ctmp > otmp and ctmp > c3 and ncnt==4: # first up candle after 3 downs
o4 = otmp
c4 = ctmp
r4 = rtmp
ncnt += 1
else:
break
cnt -= 1
Kann mir jemand sagen, warum die Variable 'c3' in der finalen 'elif'-Anweisung nicht erkannt wird, wenn alle Variablen c1 - c3 erkannt wurden, bevor ich das letzte' elif 'hinzugefügt habe? By the way, ich bin Iterieren durch eine Pandas Datenrahmen
Jede mögliche Unterstützung viel
Glen
Ja, 'c3' ist nur in einem anderen' elif' Block definiert. Da immer nur ein Block in einer 'if ... elif .. else'-Anweisung ausgeführt wird, können Sie keine Namen verwenden, die nur in einem dieser Blöcke im Test einer anderen zugewiesen sind. –
weil, wenn das 'elif' erreicht wurde, das heißt, die Bedingung darüber wurde nicht erfüllt. Implizieren, dass 'c3' nie erklärt wurde. – hjpotter92
Dass 'o1' und' o2' keine Fehler sind, liegt daran, dass Python nicht einmal auf spätere Testbedingungen schaut, wenn ein früherer schon übereinstimmt. Wenn also 'ctmp