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

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

lineNotifyメソッドの続きから。

       else:
          try:
              files = {"imageFile": open(fileName, "rb")}
              requests.post(self.line_notify_api, data=payload,
                            headers=headers, files=files)
          except:
              pass

fileNameが指定されていればファイルも併せてPOSTします。

loopメソッドに戻ります。

                    lastPositionPrice = best_ask

lastPositionPriceにbest_askを入れておきます。
厳密にはこの価格で約定したとは限らないのですが、近似値として入れています。

               # ショートエントリー
               elif judgement[1]:
                   print(datetime.datetime.now())
                   self.order.market(size=lot, side="SELL")
                   pos -= 1
                   message = "Short entry. Lot:{}, Price:{}, ".format(lot,
                                                                      best_bid)
                   self.lineNotify(message)
                   lastPositionPrice = best_bid

judgement[1]に1が入っている場合はshortします。
処理手順はlongの時と同様です。
時刻を標準出力し、
成行注文し、
positionを1減算し、
messageを作ってLINEに送り、
lastPositionPriceに売値の近似値としてbest_bidを入れています。

            elif pos == 1:

longポジションを持っている場合の分岐です。

               # ロングクローズ
               if judgement[2]:
                   print(datetime.datetime.now())
                   self.order.market(size=lot, side="SELL")
                   pos -= 1
                   plRange = best_bid - lastPositionPrice
                   pl.append(pl[-1] + plRange * lot)
                   message = "Long close. Lot:{}, Price:{}, pl:{}".format(lot,
                                                                          best_bid,
                                                                          pl[
                                                                              -1])
                   fileName = self.describePLForNotification(pl,
                                                             df_candleStick)
                   self.lineNotify(message, fileName)
                   # 一定以上の値幅を取った場合,次の10トレードはロットを1/10に落とす.
                   if plRange > waitTh:
                       waitTerm = originalWaitTerm
                       lot = round(originalLot / 10, 3)
                   elif waitTerm > 0:
                       waitTerm -= 1
                       lot = round(originalLot / 10, 3)
                   if waitTerm == 0:
                       lot = originalLot

judgement[2]に1が入っていればlongをクローズします。

                   print(datetime.datetime.now())
                   self.order.market(size=lot, side="SELL")
                   pos -= 1

ここまではエントリーのときと同じです。
時刻を標準出力し、
longクローズなので成行で売り、
positionを1減算します。

                   plRange = best_bid - lastPositionPrice
                   pl.append(pl[-1] + plRange * lot)

この時点でのbest_bidからエントリー時に格納しておいたlastPositionPriceを減算します。
`pl` はloopメソッド冒頭で空配列として初期化されているものです。
その `pl` に pl配列の最後の要素 + lastPositionPriceとbest_bidの差 * lotをappendします。
つまり、これまでの損益に今回のクローズでの損益を加算したものをpl配列に追加します。

                   message = "Long close. Lot:{}, Price:{}, pl:{}".format(lot,
                                                                          best_bid,
                                                                          pl[
                                                                              -1])

`pl` 付きでLINE用メッセージを作ります。

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

↓次


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