Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Cerebro.run() Type Error

    General Code/Help
    2
    2
    238
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Darshil Shah
      Darshil Shah last edited by

      Hello Guys,
      I am new to this community and have just started using backtrader. I have written a piece of code but whenever i use the cerebro.run(), It shows TypeError: must be real number, not LineBuffer. Attaching my piece of code for reference. It would be great if you could help. Thank you

      from future import (absolute_import, division, print_function,
      unicode_literals)
      import backtrader as bt
      import backtrader.indicators as btind
      import datetime
      import os.path
      import sys

      class Sentiment(bt.Indicator):
      lines = ('sentiment',)
      plotinfo = dict(
      plotymargin=0.15,
      plothlines=[0],
      plotyticks=[1.0, 0, -1.0])

      def next(self):
          self.date = self.data.datetime
          date = bt.num2date(self.date[0]).date()
          prev_sentiment = self.sentiment
          if date in date_sentiment:
              self.sentiment = date_sentiment[date]
          self.lines.sentiment[0] = self.sentiment
      

      class SentimentStrat(bt.Strategy):
      params = (
      ('period', 15),
      ('printlog', True),
      )

      def log(self, txt, dt=None, doprint=False):
          ''' Logging function for this strategy'''
          if self.params.printlog or doprint:
              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
          # Keep track of pending orders
          self.order = None
          self.buyprice = None
          self.buycomm = None
          self.sma = bt.indicators.SimpleMovingAverage(
              self.datas[0], period=self.params.period)
          self.date = self.data.datetime
          self.sentiment = None
          Sentiment(self.data)
          
      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')
              
          # Write down: no pending order
          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))
      
      ### Main Strat ###
      def next(self):
          # log closing price of the series from the reference
          self.log('Close, %.2f' % self.dataclose[0])
          
          date = bt.num2date(self.date[0]).date()
          prev_sentiment = self.sentiment
          if date in date_sentiment:
              self.sentiment = date_sentiment[date]
          
          # Check if an order is pending. if yes, we cannot send a 2nd one
          if self.order:
              return
          print(self.sentiment)
          # If not in the market and previous sentiment not none
          if not self.position and prev_sentiment:
              # buy if current close more than sma AND sentiment increased by >= 0.5
              if self.dataclose[0] > self.sma[0] and self.sentiment - prev_sentiment >= 0.5:
                  self.log('BUY CREATE, %.2f' % self.dataclose[0])
                  self.order = self.buy()
                  
          # Already in the market and previous sentiment not none
          elif prev_sentiment:
              # sell if current close less than sma AND sentiment decreased by >= 0.5
              if self.dataclose[0] < self.sma[0] and self.sentiment - prev_sentiment <= -0.5:
                  self.log('SELL CREATE, %.2f' % self.dataclose[0])
                  self.order = self.sell()
      
      def stop(self):
          self.log('(MA Period %2d) Ending Value %.2f' %
                   (self.params.period, self.broker.getvalue()), doprint=True)
      

      if name == 'main':
      cerebro = bt.Cerebro()

      # Strategy
      cerebro.addstrategy(SentimentStrat)
      
      # Data Feed
      data = bt.feeds.YahooFinanceCSVData(
          dataname = r'C:\Users\HP\Downloads\AMZN.csv',
          fromdate = earliest_date,
          todate = datetime.datetime(2019,6,19),
          reverse = False
      )
      
      cerebro.adddata(data)
      
      cerebro.broker.setcash(100000.0)
      cerebro.addsizer(bt.sizers.FixedSize, stake=10)
      cerebro.broker.setcommission(commission=0.001)
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      cerebro.plot()
      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @Darshil Shah last edited by

        @Darshil-Shah said in Cerebro.run() Type Error:

        I am new to this community and have just started using backtrader.

        Basics

        • Rule #1 when playing chess and doing algotrading: look at what you have before your nose
        • Rule #2: go always back to Rule #1

        As such (and the top of each and every page in the forum)

        For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
        

        Without that your code is basically unreadable for human beings with two eyes (those with three may possible add an extra visual dimension and put the code back together)

        There are a couple of obvious things in any case:

        @Darshil-Shah said in Cerebro.run() Type Error:

        It shows TypeError: must be real number, not LineBuffer

        • Haven't you considered that sharing the error (the trace) is as important as the code?

          This is telling you, that you are passing the line to an assigment, where you should be passing a float (or something which can be converted directly to a float like an int

        @Darshil-Shah said in Cerebro.run() Type Error:

            if date in date_sentiment:
                self.sentiment = date_sentiment[date]
        

        That code at the beginning is obviously doomed to fail, because there is no date_sentiment defined anywhere even if the code is unreadable.

        @Darshil-Shah said in Cerebro.run() Type Error:

            self.date = self.data.datetime
            date = bt.num2date(self.date[0]).date()
        

        A lot easier (and timezone aware if timezones are used) if you do:

        date = self.data.datetime.date()
        
        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }