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

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

loopメソッドの続きから。

                   fileName = self.describePLForNotification(pl,
                                                            df_candleStick)

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

引数は、
pl=取引クローズ毎の損益履歴、
df_candleStick=ローソク足、
です。

       import matplotlib
       matplotlib.use('Agg')
       import matplotlib.pyplot as plt

matplotlibはグラフ描画の際に用いられるモジュールです。
参考: https://deepage.net/features/numpy-matplotlib.html
損益履歴とローソク足の情報を使ってグラフを描画しようとしているらしいことが分かります。

       close = df_candleStick["close"]

ローソク足の終値を取得します。

       index = range(len(pl))

損益履歴の要素個数をrangeで配列にしてindexに入れておきます。
参考: https://www.pythonweb.jp/tutorial/list/index12.html

       # figure
       fig = plt.figure(figsize=(20, 12))

グラフを描画するfigureを用意します。
参考: http://ailaby.com/matplotlib_fig/#id1
`figsize=(20, 12)`の20と12はfigureの縦インチと横インチのようです。
参考: http://www.solima.net/python/archives/179

       # for price
       ax = fig.add_subplot(2, 1, 1)
       ax.plot(df_candleStick.index, close)
       ax.set_xlabel('Time')
       ax = fig.add_subplot(2, 1, 1)

用意したfigureに2行1列のグラフ群を用意する想定でプロット番号1のグラフをまず用意します。
参考: http://ailaby.com/matplotlib_fig/#id2

       ax.plot(df_candleStick.index, close)

x軸にローソク足の番号、y軸に終値を描画します。

       ax.set_xlabel('Time')

グラフに名前を付けます。

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

↓次


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