ricequant / rqalpha

A extendable, replaceable Python algorithmic backtest && trading framework supporting multiple securities
http://rqalpha.io
Other
5.33k stars 1.61k forks source link

Plotting is not working functionally when using pandas > 0.15 #219

Closed limccn closed 3 years ago

limccn commented 6 years ago

Hello, RQAlpha Team

RQAlpha is really a effective tool for price back-testing.

I found something wrong when using command # rqalpha plot someresult.pkl to plot my back-testing result. It came out a blank window. I tried to solve this problem and found something interesting.

in rqalpha/rqalpha/mod/rqalpha_mod_sys_analyser/plot.py, line 52-53

 portfolio = result_dict["portfolio"]
 benchmark_portfolio = result_dict.get("benchmark_portfolio")

 print portfolio.index
 print benchmark_portfolio.index

by printing portfolio.index and benchmark_portfolio.index , I found an unreasonable difference.

image

According to https://github.com/pandas-dev/pandas/issues/8614 says, matplotlib can not plotting when DatetimeIndex is created by pandas > 0.15 .

I found a temporary way to solve this problem. and finally plotting was working functionally.

Use index.to_pydatetime() to explicitly convert DatetimeIndex type index to Python Datetime type.

For example: modify rqalpha/rqalpha/mod/rqalpha_mod_sys_analyser/plot.py, line 152 ax.plot(portfolio["unit_net_value"] - 1.0, label=_(u"strategy"), alpha=1, linewidth=2, color=red) to ax.plot(index.to_pydatetime(),portfolio["unit_net_value"] - 1.0, label=_(u"strategy"), alpha=1, linewidth=2, color=red)

Although it is not the best way to solve this problem, I know you can find the best one finally. And I hope these information may help you.

Thanks.

Cuizi7 commented 3 years ago

plotting works fine with pandas and matplotlib now