2016-05-05 11 views
-1

で定義されていない。できるだけ早く私は、私は次のエラーを取得する第四の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が認識されたときに、変数 'c​​3'が最終的な 'elif'ステートメントで認識されない理由を教えてください。ところで は、私はパンダのデータフレームを反復処理しています

くらい

グレン

+0

はい、 'c3'は別の' elif'ブロックでのみ定義されています。 'if ... elif .. else'文の中の** one **ブロックだけが実行されるので、テスト中のそれらのブロックのうちの一つに割り当てられた名前を別のものに使うことはできません。 –

+0

であり、その「elif」に達した場合、それはその上の条件が満たされなかったことを意味するからです。 c3が決して宣言されなかったことを意味する。 – hjpotter92

+0

'o1'と' o2'はエラーをスローしません。なぜなら、Pythonは以前のものがすでにマッチしていたとしても、後のテスト条件を見ていないからです。したがって、 'ctmp

答えて

0

TLを高く評価し、任意の支援; DR:あなたが最初NCNTチェックするとあなたはおそらく動作します:

`if ncnt==4 and ctmp > otmp and ctmp > c3:` 

より長い答えを得るには、最初に、少しばかげて珍しい何かをやっていることを受け入れる必要があります。 else節の中に名前を定義しておいて、後でコードをチェックする前に名前を定義するためにプログラムがそれらのクラスを実行することを信用しています。ある

、あなたのforループの最初の繰り返しで、あなたはこの行ヒット:あなたが新しい変数o1c1、およびr1を定義するためのコードを実行し、真であるため、

if ctmp<otmp and ncnt==1: 

を。条件は最初の反復で偽だった場合は、次の行を打つでしょう:Pythonは変数を探し始めるでしょう

elif ctmp<otmp and otmp<o1 and ctmp<c1 and rtmp<=r1 and ncnt==2: 
    o2 = otmp 

o1の値を取得し、迅速にあなたにNameError: name 'o1' is not defined例外を与えることを試みます。

二つの実のオプションがあります:あなたがそれらを使用する前に

  1. これは好ましい方法であり、あなたがダウンしてあなたのコードの年間の維持を担当する人によってストーカーがなくなり、おそらく0にそれらを設定し、allthe名を定義しますが、この線。

  2. 最初に状態を注意深く確認してください。このコードは、現在の状態がncntで示されている小さな状態マシンです。あなたのコードが最初に正しい状態になっていることを確認した場合、あなたは悪い変数を決して参照しません。だから、より良いコードはncountの名前を変更し、if nState == 3 and o3 > something...でifステートメントを開始します。 nStateが間違っていれば、if文はまだ存在しない変数を見ずに停止します。

コーディングを保存してください。

+0

Thk you Charles。すべて今、意味があります。しばらくコード化していないので、私はそうするべきではないと思っています。 – glen

関連する問題