1

我是编码新手,尝试使用 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()

看看有没有人可以帮忙,非常感谢。

4

1 回答 1

1

问题似乎不是打印数据。您在初始化对象时出错。您需要共享代码的另一部分。

我想通过开发一个测试应用程序来帮助你。测试应用程序基于Plotting应用程序。我使用此链接修复了“从 matplotlib.dates 修复 ImportError”错误。我使用此链接在应用程序中使用测试数据 ( 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版本导致"Fix ImportError from matplotlib.dates"。避免此错误的方法是matplot通过运行以下代码来使用旧版本:

pip uninstall matplotlib
pip install matplotlib==3.1.1

下面是应用测试图像:

在此处输入图像描述

于 2021-12-15T16:16:03.577 回答