で定義されていない。できるだけ早く私は、私は次のエラーを取得する第四のelif追加として、しかしNameError名「VAR」私は正常に動作し、次のコード持ってIF文
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
を:
のelifをCTMP> otmpとCTMP> C3とNCNT == 4: NameError:
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
:誤ったコードは次のようになります。すなわち名 'C3' を
が定義されていません。
最終的な 'elif'を追加する前にすべての変数c1〜c3が認識されたときに、変数 'c3'が最終的な 'elif'ステートメントで認識されない理由を教えてください。ところで は、私はパンダのデータフレームを反復処理しています
くらい
グレン
はい、 'c3'は別の' elif'ブロックでのみ定義されています。 'if ... elif .. else'文の中の** one **ブロックだけが実行されるので、テスト中のそれらのブロックのうちの一つに割り当てられた名前を別のものに使うことはできません。 –
であり、その「elif」に達した場合、それはその上の条件が満たされなかったことを意味するからです。 c3が決して宣言されなかったことを意味する。 – hjpotter92
'o1'と' o2'はエラーをスローしません。なぜなら、Pythonは以前のものがすでにマッチしていたとしても、後のテスト条件を見ていないからです。したがって、 'ctmp