【问题标题】:Backtrader fail to show plot graphBacktrader 无法显示绘图图
【发布时间】:2023-03-16 14:39:02
【问题描述】:
我是编码新手,尝试使用 backtrader 进行简单的回测过程。我能够执行买卖,但是当我试图绘制它显示的图表时:
AttributeError: type object 'Gcf' has no attribute '_set_new_active_manager'
我的代码如下:
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()
看看有没有人可以提供一些帮助,非常感谢。
【问题讨论】:
标签:
python
matplotlib
plot
graph
backtrader
【解决方案1】:
问题似乎不在于打印数据。您在初始化对象时出错。您需要共享代码的另一部分。
我想通过开发一个测试应用程序来帮助您。测试应用程序基于Plotting 应用程序。我使用this link 修复了 "Fix ImportError from matplotlib.dates" 错误。我使用this link 在应用程序中使用测试数据(2005-2006-day-001.txt)。下面是一个演示示例:
from __future__ import (absolute_import, division, print_function, unicode_literals)
import backtrader as bt
class St(bt.Strategy):
def __init__(self):
self.sma = bt.indicators.SimpleMovingAverage(self.data)
data = bt.feeds.BacktraderCSVData(dataname='dataset.txt')
cerebro = bt.Cerebro()
cerebro.adddata(data)
cerebro.addstrategy(St)
cerebro.run()
cerebro.plot()
当前matplot 版本导致“修复来自 matplotlib.dates 的 ImportError”。避免此错误的方法是通过运行以下代码来使用较旧的matplot 版本:
pip uninstall matplotlib
pip install matplotlib==3.1.1
下面是应用测试图: