Cerebro plot failed to run
-
from future import (absolute_import, division, print_function,
unicode_literals)import backtrader as bt
class TestStrategy(bt.Strategy):
def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = None def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: # Buy/Sell order submitted/accepted to/by broker - Nothing to do return # Check if an order has been completed # Attention: broker could reject order if not enough cash if order.status in [order.Completed]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.bar_executed = len(self) elif order.status in [order.Canceled, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') self.order = None def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return # Check if we are in the market if not self.position: # Not yet ... we MIGHT BUY if ... if self.dataclose[0] < self.dataclose[-1]: # current close less than previous close if self.dataclose[-1] < self.dataclose[-2]: # previous close less than the previous close # BUY, BUY, BUY!!! (with default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy() else: # Already in the market ... we might sell if len(self) >= (self.bar_executed + 5): # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.sell()
if name == 'main':
# Create a cerebro entity
cerebro = bt.Cerebro()# Add a strategy strats = cerebro.addstrategy( TestStrategy) kl = abu.ABuSymbolPd.make_kl_df('F', n_folds=int(2)) kl = kl[['open', 'high', 'low', 'close', 'volume']] data = bt.feeds.PandasData(dataname=kl) # Add the Data Feed to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(1000.0) cerebro.broker.set_coc(True) # Add a FixedSize sizer according to the stake cerebro.addsizer(bt.sizers.FixedSize, stake=10) # Set the commission cerebro.broker.setcommission(commission=0.0) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.plot()
Above are my code, and whenever I am running cerebro.plot(). I have the following errors.
<IPython.core.display.HTML object>
cerebro.plot()
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\backtrader\cerebro.py", line 996, in plot
plotter.show()
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\backtrader\plot\plot.py", line 794, in show
self.mpyplot.show()
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\pyplot.py", line 254, in show
return _show(*args, **kw)
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_nbagg.py", line 255, in show
manager.show()
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_nbagg.py", line 91, in show
self._create_comm()
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_nbagg.py", line 123, in _create_comm
self.add_web_socket(comm)
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_webagg_core.py", line 433, in add_web_socket
self.resize(w, h)
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_webagg_core.py", line 419, in resize
size=(w / self.canvas._dpi_ratio, h / self.canvas._dpi_ratio))
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_webagg_core.py", line 490, in _send_event
s.send_json(payload)
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_nbagg.py", line 199, in send_json
self.comm.send({'data': json.dumps(content)})
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\ipykernel\comm\comm.py", line 121, in send
data=data, metadata=metadata, buffers=buffers,
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\ipykernel\comm\comm.py", line 66, in _publish_msg
self.kernel.session.send(self.kernel.iopub_socket, msg_type,
AttributeError: 'NoneType' object has no attribute 'session'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib_pylab_helpers.py", line 74, in destroy_all
manager.destroy()
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_nbagg.py", line 127, in destroy
self._send_event('close')
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_webagg_core.py", line 490, in _send_event
s.send_json(payload)
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\matplotlib\backends\backend_nbagg.py", line 199, in send_json
self.comm.send({'data': json.dumps(content)})
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\ipykernel\comm\comm.py", line 121, in send
data=data, metadata=metadata, buffers=buffers,
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\ipykernel\comm\comm.py", line 66, in _publish_msg
self.kernel.session.send(self.kernel.iopub_socket, msg_type,
AttributeError: 'NoneType' object has no attribute 'session'Please let me know what goes wrong, thank for your help!
-
@heekah7 said in Cerebro plot failed to run:
Please let me know what goes wrong, thank for your help!
- You missed the top of the forum, what basically renders your code unreadable.
For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
- Something is broken well beyond the realm of backtrader
@heekah7 said in Cerebro plot failed to run:
File "C:\Users~\PycharmProjects\helloworld\venv\lib\site-packages\ipykernel\comm\comm.py", line 66, in _publish_msg
self.kernel.session.send(self.kernel.iopub_socket, msg_type,
AttributeError: 'NoneType' object has no attribute 'session'If you see the log, it is everything due to a connection which
matplotlib
opens. Nothing to do with backtrader -
I realize this topic is pretty old, but since I ran into this error myself recently....
It's incorrect to say:
Something is broken well beyond the realm of backtrader
While it's true that this error occurs in the guts of matplotlib, it happens because Backtrader is incorrectly assuming it's running inside a Jupyter notebook and therefore configuring matplotlib with an incorrect backend.
The fix is to use
cerebro.plot(iplot=False)
when not running inside a Jupyter notebook.See:
https://www.backtrader.com/blog/posts/2016-09-17-notebook-inline/notebook-inline/
and:
https://community.backtrader.com/topic/2106/cerebro-plot-error/4 -
PS. Just to clarify, I had been running Backtrader at the command line with no errors for quite some time. Then I was working inside a Jupyter notebook and suddenly my CLI code started throwing the same error mentioned above...
AttributeError: 'NoneType' object has no attribute 'session'
Some googling around led me to know it happens when matplotlib is configured with the wrong backend. Further googling led me to the solution, whereby Backtrader chooses an incorrect matplotlib backend. The offending code is at line 125 of plot.py:
if iplot: if 'ipykernel' in sys.modules: matplotlib.use('nbagg')
Once I had installed Jupyter on my system, and started using it, that's when the error began. So, my guess is we need some other way to detect whether or not we're running inside a notebook.
-
@heekah7 said in Cerebro plot failed to run:
ipykernel
I don't think backtrader --> matplotlib --> jupyter play well together. Check your vevn for ipykernel where matplotlib is installed. I'm guessing you need to recreate your venv fresh with matplotlib outside of jupyter. Not sure, but I would try that.
-
@Joe-Landers Did you get this resolved? I'm not able to recreate the error on my machine and I'm trying to resolve this issue.
-
@run-out Yes, I found that using
cerebro.plot(iplot=False)
when not running in a notebook worked. Also, this is only needed (for me at least) if the Jupyter server happens to be running (even in the background) at the time.Interestingly, I had another problem whereby matplotlib plots would crash whenever I scrolled. I Googled around learned it's because I installed Python from Homebrew on my mac, and that version of python doesn't have the same gui toolkit as the "full" version. I uninstalled that version of python and installed the official distribution from python.org and now both problems are gone ¯_(ツ)_/¯
-
@run-out However, if you're asking whether I found a better way to detect whether or not we're running inside a notebook? No, I didn't. However, I think the better answer is to not auto-detect at all. Most Jupyter users are quite familiar with using the
%matplotlib inline
majik when plotting in Jupyter. However, this auto-detection foobar is messing that up too. -
Ran into another issues today whereby the plots stopped showing up in my Jupyter notebooks (using just
cerebro.plot()
. On a hunch, I decided to trycerebro.plot(iplot=False)
inside the Jupyter notebook to see what would happen. As long as I have the majick%matplotlib inline
somewhere in my notebook, that issue is also fixed. -
@Joe-Landers Thanks a lot or all your replies, that was very helpful.
-
Just a not on this issue, we tried to recreate the problem to implement a fix but could not recreate the problem. The Jupyter issue has popped up from time to time and as best we can tell it's an installation issue with Jupyter. If someone has the problem again please let the community know as well as how your Jupyter was installed. Here is the link to github.
-
@run-out I'm running backtrader in a separate process (I programmed a live trading model sourcing data from a websocket, the data sourcing done in a separate process). My idea was to end the backtrader process on Friday afternoon after market close but on attempting to plot I get precisely the same error message. will try cerebro.plot(iplot=False) to see if it works.
-
@longhngo This solved the problem in my case