1

I am new to coding, trying to use backtrader to do a simple backtesting process. I was able to execute the buy and sell, but when i am trying to plot the graph it shows: AttributeError: type object 'Gcf' has no attribute '_set_new_active_manager'

My code is as below:

print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

cerebro.run()

print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

cerebro.plot()

See if any one can offer some help, many thanks.

Sercan
  • 4,739
  • 3
  • 17
  • 36
  • I'v run into a similar problem with `AttributeError: 'OptReturn' object has no attribute 'datas'` – Roger Jul 20 '23 at 05:57

1 Answers1

1

The problem doesn't seem to be printing the data. You are making an error initializing an object. You need to share the other part of the code.

I wanted to help you by developing a test application. The test application was based on the Plotting application. I used this link to fix the "Fix ImportError from matplotlib.dates" error. I used this link to use the test data (2005-2006-day-001.txt) in the application. Below is a demo sample:

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()

Current matplot version causes "Fix ImportError from matplotlib.dates". The way to avoid this error is to use older matplot version by running the following codes:

pip uninstall matplotlib
pip install matplotlib==3.1.1

Below is the application test image:

enter image description here

Sercan
  • 4,739
  • 3
  • 17
  • 36