简体   繁体   English

backtrader 情节错误加载后端“TkAgg”

[英]backtrader plot error loading backend 'TkAgg'

I try to use backtrader but get an error each time I try to plot something... this code, a simple copy paste of an working exemple leed me to this error: ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'qt5' is currently running我尝试使用 backtrader,但每次尝试绘制某些内容时都会出现错误......这段代码,一个工作示例的简单复制粘贴让我发现了这个错误:ImportError:无法加载后端'TkAgg',需要'tk'交互式框架,因为“qt5”当前正在运行

import backtrader as bt
from datetime import datetime

class firstStrategy(bt.Strategy):

    def __init__(self):
        self.rsi = bt.indicators.RSI_SMA(self.data.close, period=21)

    def next(self):
        if not self.position:
            if self.rsi < 30:
                self.buy(size=100)
        else:
            if self.rsi > 70:
                self.sell(size=100)


#Variable for our starting cash
startcash = 10000

#Create an instance of cerebro
cerebro = bt.Cerebro()

#Add our strategy
cerebro.addstrategy(firstStrategy)

#Get Apple data from Yahoo Finance.
data = bt.feeds.Quandl(
    dataname='AAPL',
    fromdate = datetime(2016,1,1),
    todate = datetime(2017,1,1),
    buffered= True
    )

#Add the data to Cerebro
cerebro.adddata(data)

# Set our desired cash start
cerebro.broker.setcash(startcash)

# Run over everything
cerebro.run()

#Get final portfolio Value
portvalue = cerebro.broker.getvalue()
pnl = portvalue - startcash

#Print out the final result
print('Final Portfolio Value: ${}'.format(portvalue))
print('P/L: ${}'.format(pnl))

#Finally plot the end results
cerebro.plot(style='candlestick')

Im working on anaconda spyder4 with last updated python and matplotlib我正在使用最新更新的 python 和 matplotlib 处理 anaconda spyder4

You could try:你可以试试:

import matplotlib
matplotlib.use('QT5Agg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM