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

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

describePLForNotificationメソッドの続きから。

  def describePLForNotification(self, pl, df_candleStick):
     import matplotlib
     matplotlib.use('Agg')
     import matplotlib.pyplot as plt
     close = df_candleStick["close"]
     index = range(len(pl))
     # figure
     fig = plt.figure(figsize=(20, 12))
     # for price
     ax = fig.add_subplot(2, 1, 1)
     ax.plot(df_candleStick.index, close)
     ax.set_xlabel('Time')
     # y axis
     ax.set_ylabel('The price[JPY]')
     # for PLcurve
     ax = fig.add_subplot(2, 1, 2)
     # plot
     ax.plot(index, pl, color='b', label='The PL curve')
     ax.plot(index, [0] * len(pl), color='b', )
     # x axis
     ax.set_xlabel('The number of Trade')
     # y axis
     ax.set_ylabel('The estimated Profit/Loss(JPY)')
     # legend and title
     ax.legend(loc='best')
     ax.set_title('The PL curve(Time span:{})'.format(self.candleTerm))
     # save as png
     today = datetime.datetime.now().strftime('%Y%m%d')
     number = "_" + str(len(pl))
     fileName = today + number + ".png"
     plt.savefig(fileName)
     plt.close()
     return fileName
     # save as png
     today = datetime.datetime.now().strftime('%Y%m%d')
     number = "_" + str(len(pl))
     fileName = today + number + ".png"
     plt.savefig(fileName)

名前をつけてグラフを保存します。

     plt.close()

メモリを食いすぎることがあるようなので、ファイル書き出したらcloseします。
参考: http://xartaky.hatenablog.jp/entry/2017-09-19-plt-close

     return fileName

生成しておいたファイル名を返します。

loopメソッドに戻ります。

                    fileName = self.describePLForNotification(pl,
                                                             df_candleStick)

ファイル名を受け取ります。

                    self.lineNotify(message, fileName)

作っておいたメッセージとファイル名を指定してlineNotifyメソッドに渡します。
lineNotifyメソッドは下記参照。

                   # 一定以上の値幅を取った場合,次の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

「# 一定以上の値幅を取った場合,次の10トレードはロットを1/10に落とす.」
こういう調整、botトレードの経験が現れているようで勉強になります。

                    if plRange > waitTh:
                        waitTerm = originalWaitTerm
                        lot = round(originalLot / 10, 3)

この時点でのbest_bidからエントリー時に格納しておいたlastPositionPriceを減算した plRange が waitTh よりも大きい場合、
waitTermにoriginalWaitTermを入れ、
lotをoriginalLotの1/10にします。
waitThはloopメソッド実行時に渡される値です。デフォルトでは、

    channelBreakOut.waitTh = 10000

となっています。
この場合、10000円以上の値幅を取った際はロットを落とす、ということになります。
originalWaitTermもloopメソッド実行時に渡される値です。

    channelBreakOut.waitTerm = 0

デフォルトは0です。
originalLotはChannelBreakOutクラス初期化時に設定されている値です。

        originalLot = self.lot
        self._lot = 0.01

0.01のままだと1/10すると0.001になります。
bitflyer FXでは現状0.001の注文ができなくなっている気がするので注文失敗するかもしれません。

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

↓次


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