text: from datetime import datetime
  import backtrader as bt
  
  # create a Stratey
  class RsiSignalStrategy(bt.SignalStrategy):
      params = dict(rsi_periods=14, rsi_upper=70, rsi_lower=30, rsi_mid=50)
  
      def __init__(self):
  
          # add RSI indicator
          rsi = bt.indicators.RSI(period=self.p.rsi_periods,
                                  upperband=self.p.rsi_upper,
                                  lowerband=self.p.rsi_lower)
  
          # add RSI from TA-lib just for reference
          bt.talib.RSI(self.data, plotname='TA_RSI')
  
          # long condition (with exit)
          rsi_signal_long = bt.ind.CrossUp(rsi, self.p.rsi_lower, plot=False)
          self.signal_add(bt.SIGNAL_LONG, rsi_signal_long)
          self.signal_add(bt.SIGNAL_LONGEXIT, -(rsi > self.p.rsi_mid))
  
          # short condition (with exit)
          rsi_signal_short = -bt.ind.CrossDown(rsi, self.p.rsi_upper, plot=False)
          self.signal_add(bt.SIGNAL_SHORT, rsi_signal_short)
          self.signal_add(bt.SIGNAL_SHORTEXIT, rsi < self.p.rsi_mid)
  
  # create a Data Feed
  data = bt.feeds.YahooFinanceData(dataname='FB',
                                   fromdate=datetime(2018, 1, 1),
                                   todate=datetime(2018, 12, 31))
  
  # create a Cerebro entity
  cerebro = bt.Cerebro(stdstats = False)
  
  # # set up the backtest
  cerebro.addstrategy(RsiSignalStrategy)
  cerebro.adddata(data)
  cerebro.broker.setcash(1000.0)
  cerebro.broker.setcommission(commission=0.001)
  cerebro.addobserver(bt.observers.BuySell)
  cerebro.addobserver(bt.observers.Value)
  cerebro.run()
  
  cerebro.plot(iplot=True, volume=False)
  