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/

    AttributeError: 'int' object has no attribute 'ref'

    General Code/Help
    1
    2
    177
    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.
    • V
      Vypy1 last edited by

      I am trying to run a basic strategy just to check how things work on backorder. As per what is mentioned in the Quickstart guide, I have loaded my data onto cerebra, added the strategy and I get an error that states AttributeError: 'int' object has no attribute 'ref'. My code is as follows:

      from __future__ import (absolute_import, division, print_function, unicode_literals)
      import pandas as pd
      import talib as ta
      import numpy as np
      import backtrader as bt
      import datetime
      from datetime import datetime
      
      
      class MyFirstStrategy(bt.Strategy):
          params = (('fastma', 20), ('slowma', 50), )
          
          def __init__(self):
              self.dataclose = self.datas[0].close
              self.fma = bt.indicators.EMA(self.data, period = self.params.fastma)
              self.sma = bt.indicators.EMA(self.data, period = self.params.slowma)
      
              
              self.order = None
              self.buyprice=None
              self.buycomm=None
              
          def log(self, txt, dt=None):
              dt = self.datas[0].datetime.date()
              print(f'{dt}, {txt}')
              
          def notify_order(self, order):
              if order in [order.Submitted, order.Accepted]:
                  return
              
              
              if order in [order.Completed]:
                  if order.isbuy():
                      self.log(f'BUY EXECUTED: {order.executed.price}, Cost: {order.executed.value}, Comms: {order.executed.comm}')
                      
                  else:
                      self.log(f'SELL EXECUTED: {order.executed.price}, Cost: {order.executed.value}, Comms: {order.executed.comm}')
                      
                      
              elif order.status in [order.Rejected, order.Margin, order.Cancelled]:
                  self.log('Order rejected/cancelled by broker or insufficient margin')
                  
              self.order = None
              
              
          def notify_trade(self, trade):
              if not trade.isclosed:
                  return  
              self.log(f'OPERATION PROFIT: GROSS {trade.pnl}, NET {trade.pnlcomm}')
              
              
                    
          def next(self):
              self.log(f'Close: {self.dataclose[0]}')
              
              if self.order:
                  return
              
              if not self.position:
                  
                  if (self.fma[-1] < self.sma[-1]) and (self.fma[0] > self.sma[0]):
                      self.log(f'BUY CREATE, {self.dataclose[0]}')
                      self.order = self.buy()
                          
                          
              else:
                  if (self.fma[-1] > self.sma[-1]) and (self.fma[0] < self.sma[0]):
                      self.log(f'SELL CREATE, {self.dataclose[0]}')
                      self.order = self.sell()
      
      
      
      cerebro = bt.Cerebro()
      cerebro.broker.set_cash(1000000)
      
      print(f'Starting Portfolio Value: {cerebro.broker.getvalue()}')
      
      data = bt.feeds.PandasData(dataname=hdfc, datetime=None, open=-1, high=-1, low=-1, close=-1, volume=-1)
      cerebro.adddata(data)
      cerebro.addstrategy(MyFirstStrategy)
      cerebro.addsizer(bt.sizers.FixedSize, stake=10)
      cerebro.broker.setcommission(commission=0.001)
      
      cerebro.run()
      
      print(f'Final Portfolio Value: {cerebro.broker.getvalue()}')
      
      V 1 Reply Last reply Reply Quote 0
      • V
        Vypy1 @Vypy1 last edited by

        @vypy1 I have managed to fix this, it was a silly miss from my end where instead of putting in order.status in order.Completed I had put in just order.

        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors