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

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

while "status" in response:
  try:
      response = self.api.sendchildorder(
          product_code=self.product_code, child_order_type="MARKET",
          side=side,
          size=size, minute_to_expire=minute_to_expire)
  except:
      pass
  time.sleep(3)
return response

なぜresponseにstatusキーが入っている限りloopさせているかというと、sendchildorderを実行した際に取得できる値にはstatusキーが含まれていないからです。
pybitflyerはbitflyerのAPIを参照してレスポンスをオブジェクトとして返すだけなので、正常にAPI参照できた場合はbitflyerのAPIリファレンス通りの値が返ってくると考えてよいです。
参考: https://github.com/yagays/pybitflyer/blob/4d89e78bcd88b772cc785687dc7fe801a1de1296/pybitflyer/pybitflyer.py#L60
リファレンスによると、sendchildorderでは下記のような値が返ってきます。

{
    "child_order_acceptance_id": "JRF20150707-050237-639234"
}

statusキーは含まれていません。
なので、sendchildorderが成功した場合は上記情報がresponseに、失敗した場合は何も入らないのでresponseは

response = {"status": "internalError in order.py"}

で指定したものが変わらず入っています。
この処理は工夫が感じられます。
本来bitflyerのAPIドキュメントにエラーレスポンスの仕様が書かれていてかつpybitflyerがそれをハンドリングできる実装であればこの処理はもう少し厳密に書けるのですが、それがないので工夫しているのだと思います。

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

この記事が気に入ったらサポートをしてみませんか?