def plot_backtest_result_curve(log_file):
data_dict = read_result_from_log(log_file)
#benchmark_dates, benchmark_value = sort_dict_buy_key(data_dict)
portfolio_history_data = pd.DataFrame(data_dict).T
portfolio_history_data.columns = ['total_value', 'cash', 'stock_value', 'bench_value']
dates_ = list(portfolio_history_data.index)
dates_.pop()
dates = []
for d in dates_:
if d == None:
d = config.start_date
date = datetime.datetime.strptime(d, '%Y-%m-%d').date()
dates.append(date)
#dates = [datetime.datetime.strptime(d, '%Y-%m-%d').date() for d in dates if d != None else config.start_date]
total_value = list(portfolio_history_data["total_value"].values)
cash = list(portfolio_history_data["cash"].values)
cash_value = list(portfolio_history_data["stock_value"].values)
bench_value = list(portfolio_history_data["bench_value"].values)
total_value.pop()
bench_value.pop()
# xaxis tick formatter
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
# xaxis ticks
#plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
# figure title and legend
plt.gca().legend()
plt.gca().set(title="event factor backtesting porfolio value",
ylabel = "porfolio total value",
xlabel = "backtesting time")
plt.locator_params(axis='x', nticks=10)
plt.plot(dates, total_value)
plt.plot(dates, bench_value)
plt.gcf().autofmt_xdate()
plt.show()
#data = read_result_from_log(sys.argv[1])
#plot_backtest_result_curve(data)
评论列表
文章目录