株価チャートにオーダーのエントリータイミングを矢印で描く
まだ作成途中なので、データ構造など改善の余地は多数あり。
本題はグラフへの矢印を描く方法をメモしておくこと。
orderDataFrameには次のようにオーダー情報を格納。
TradeId OrderDate TradeTicker OrderPrice OrderQty TradeType
0.0 2016-09-01 8306 574.0 100.0 sell
1.0 2016-09-02 8306 575.5 100.0 sell
次のように書いたらとりあえず出来た。
########## オーダータイミングをチャート上に表示 ########## fig = plt.figure() # グラフ描画領域の作成 point = 1 # 描画するサブプロットの位置を示す変数 for ticker in tickerList: ax = fig.add_subplot(1, len(tickerList), point) # サブプロットの作成 ax.plot(stocksPanel['Close'][ticker]) # 株価のグラフ描画 ax.set_title(ticker) # グラフタイトル #ここからグラフへの矢印描画 for orderDataRow in orderDataFrame.iterrows(): ax.annotate( 'Order Done', xy=(orderDataRow[1]['OrderDate'], orderDataRow[1]['OrderPrice']), # arrowpropsの矢印の終点 xytext=(orderDataRow[1]['OrderDate'], orderDataRow[1]['OrderPrice']+50), # 注釈コメントの位置。arrowprops矢印の始点 arrowprops=dict(facecolor='red'), # 矢印の設定 horizontalalignment='left', verticalalignment='top', color='red' ) point += 1
以下、ポイントのメモ。
◎メモ1
pandas.DataFrameから1行ずつ行を取り出すのに「iterrows()」を使用。
※)これがベストなのかは保証しない。◎メモ2
取得されたorderDataRow はtuple。
orderDataRow[1]['OrderDate']のように数字指定が嫌だ。