チャネルブレイクアウトbotコード(by スナフキン氏)読解メモ32

の続きです。
題材コードは https://sshuhei.com/entry/channelbreakout/ です。

      for i in range(1, 10, 1):
         with open(filename, 'r', encoding="utf-8") as f:
             reader = csv.reader(f)
             header = next(reader)
             for row in reader:
                 candleStick = [row for row in reader if row[4] != "0"]

の検証から。

1536067200,836101,836740,835650,836524,469.55627,392621800
1536067260,836612,836953,836300,836900,428.57858,358585950
1536067320,836911,837650,836900,837433,453.44196,379669730
1536067380,837501,837543,837035,837270,304.40607,254883380
1536067440,837270,837481,836949,837123,256.21262,214499120
1536067500,837173,838276,837159,837965,456.99435,382902880
1536067560,838005,838448,837560,838389,356.66867,298876160
1536067620,838402,838888,838101,838127,378.10135,317035970
1536067680,838111,838450,838069,838115,288.92056,242181020
1536067740,838115,839500,838069,839153,464.81525,389952320

上記データが入ったcsvをtest.csvという名前のファイルとして用意します。

import csv
filename = "test.csv"
for i in range(1, 10, 1):
   with open(filename, 'r', encoding="utf-8") as f:
       reader = csv.reader(f)
       header = next(reader)
       print(header)
       for row in reader:
           candleStick = [row for row in reader if row[4] != "0"]
           print(candleStick)

コードを書いて実行します。

['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'], ['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'], ['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'], ['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'], ['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'], ['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'], ['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'], ['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]

分かりやすいようにまずheaderだけ検証します。

['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']
['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800']

for文のiが効いていないため、csvの一行目が必ず出力されます。

for文を消してrowに何が入っているか確認します。

with open(filename, 'r', encoding="utf-8") as f:
   reader = csv.reader(f)
   header = next(reader)
   for row in reader:
       print(row)
['1536067260', '836612', '836953', '836300', '836900', '428.57858', '358585950']
['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730']
['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380']
['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120']
['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880']
['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160']
['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970']
['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020']
['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']

となるため、やはり1行目は消えそうです。
なので、ヘッダーありcsvを使う場合は問題ないですが、ヘッダーなしの場合は `header = next(reader)` 行を消したほうがよさそうです。

また、candleStickは、

with open(filename, 'r', encoding="utf-8") as f:
   reader = csv.reader(f)
   header = next(reader)
   for row in reader:
       candleStick = [row for row in reader if row[4] != "0"]
       print(candleStick)

と書くと、

[['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'],
['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'],
['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'],
['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'],
['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'],
['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'],
['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'],
['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]

と出ます。
予想に反してreader行数分のcandleStick配列は出力されていません。
これはおそらくcandleStick生成時の内包表記内でreaderイテレータが進んでいることで
`for row in reader` のreaderイテレータが最後まで到達していると判断し処理を終えていると考えられます。
また、内包表記内でイテレータを1つ進めるためrowが1つskipされ、結果要素が8つになってしまいます(とはいえ大量にデータ取得してバックテストするので大勢に影響はありません)。
期待通りのデータを得るためには、例えば下記のように書けます。

with open(filename, 'r', encoding="utf-8") as f:
   reader = csv.reader(f)
   header = next(reader)
   candleStick = [row for row in reader if row[4] != "0"]
   print(candleStick)
[['1536067200', '836101', '836740', '835650', '836524', '469.55627', '392621800'],
['1536067260', '836612', '836953', '836300', '836900', '428.57858', '358585950'],
['1536067320', '836911', '837650', '836900', '837433', '453.44196', '379669730'],
['1536067380', '837501', '837543', '837035', '837270', '304.40607', '254883380'],
['1536067440', '837270', '837481', '836949', '837123', '256.21262', '214499120'],
['1536067500', '837173', '838276', '837159', '837965', '456.99435', '382902880'],
['1536067560', '838005', '838448', '837560', '838389', '356.66867', '298876160'],
['1536067620', '838402', '838888', '838101', '838127', '378.10135', '317035970'],
['1536067680', '838111', '838450', '838069', '838115', '288.92056', '242181020'],
['1536067740', '838115', '839500', '838069', '839153', '464.81525', '389952320']]

また、printを消して、かつヘッダなしcsvの場合は下記のように書けます。

with open(filename, 'r', encoding="utf-8") as f:
   reader = csv.reader(f)
   candleStick = [row for row in reader if row[4] != "0"]

15分経ったので今日はここまで。

↓次


この記事が気に入ったら、サポートをしてみませんか?気軽にクリエイターを支援できます。

2
仮想通貨 / botトレード / bot実装 / お小遣いトレーダー / レバレッジしません / @__WannaBeFree

この記事が入っているマガジン

コメントを投稿するには、 ログイン または 会員登録 をする必要があります。