たぶん、慣れてしまえばチャートは不要だと思うけど最初は確認したい。
ライブラリの中で出力先は固定されているので、画像ファイルに出力するにはライブラリ自体の書き換える。たぶん、アップデートごとに動かなくなるので、本体が対応してくれたらいいんだけど。
修正作業手順
/usr/lib64/python2.7/site-packages/pyalgotrade/plotter.py の最後にあるplot関数を書き換える
オリジナル
def plot(self, fromDateTime=None, toDateTime=None):
"""Plots the strategy execution. Must be called after running the strategy.
:param fromDateTime: An optional starting datetime.datetime. Everything before it won't get plotted.
:type fromDateTime: datetime.datetime
:param toDateTime: An optional ending datetime.datetime. Everything after it won't get plotted.
:type toDateTime: datetime.datetime
"""
fig, mplSubplots = self.__buildFigureImpl(fromDateTime, toDateTime)
fig.autofmt_xdate()
plt.show()
修正後
def plot(self, fromDateTime=None, toDateTime=None,fileName=""):
"""Plots the strategy execution. Must be called after running the strategy.
:param fromDateTime: An optional starting datetime.datetime. Everything before it won't get plotted.
:type fromDateTime: datetime.datetime
:param toDateTime: An optional ending datetime.datetime. Everything after it won't get plotted.
:type toDateTime: datetime.datetime
"""
fig, mplSubplots = self.__buildFigureImpl(fromDateTime, toDateTime)
fig.autofmt_xdate()
if(len(fileName)):
plt.savefig(fileName)
else:
plt.show()
もちろん、メインプログラムにはAggを指定するのはお約束
import matplotlib
matplotlib.use('Agg')
追記
buildFigureを使えばfigオブジェクトを返してくれるので、あとは保存するなり煮るなり焼くなりすればいいことに気が付いた。
つまり、改造は不要で以下のように使えばいい。これでアップデートも怖くない
fig=plt.buildFigure(None,None)
fig.savefig("myoutput.png")

0 件のコメント:
コメントを投稿