Backtrader Community

    • 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/

    self.positions throws AttributeError when cerebro.broker set to IBbroker

    General Code/Help
    ib broker positions live trading strategy
    2
    2
    288
    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.
    • N
      nikhilsalunke last edited by

      I'm trying to access the self.positions attribute from within the next method of a strategy. It works as expected with backbroker but throws Attribute Error when run with IBbroker.
      Error: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'positions'

      Here's my code:

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      from datetime import datetime, timedelta
      
      import backtrader as bt
      
      
      tat = "TATASTEEL_STK_NSE"
      rel = "RELIANCE_STK_NSE"
      itc = "ITC_STK_NSE"
      stocks = [tat, rel, itc]
      
      
      class MyStrategy(bt.Strategy):
          def __init__(self):
              pass
      
          def next(self):
              print(self.getpositionbyname(itc)) # 
              print(self.getpositionbyname(rel)) #  These 3 work well
              print(self.getpositionbyname(tat)) #
              print(self.positions) # Throws Attribute Error
              print(self.positionsbyname) # Throws Attribute Error
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          store = bt.stores.IBStore(host="192.168.1.3", port=7497, _debug=False)
          datas = [store.getdata(dataname=stk, rtbar=True) for stk in stocks]
      
          for data in datas:
              cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes)
      
          cerebro.setbroker(store.getbroker())
          cerebro.addstrategy(MyStrategy)
      
          cerebro.run()
      

      In debug mode the strategy attributes postitions and positionsbyname hold an str value containing the error message.

      24e730d2-10c0-4d72-adf6-41567c6ebd0a-image.png

      The Full Traceback looks something like this:
      f37e01c4-e4f0-446c-b310-c016f4e8518f-image.png

      Although, separately requesting position for each datafeed using self.getpositionbyname() works seamlessly:

      ef21cdc9-622a-4ce9-9959-31865940a9f6-image.png

      vladisld 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld @nikhilsalunke last edited by

        @nikhilsalunke Seems to be a bug in IBBroker implementation.

        I've submitted new issue:
        https://github.com/backtrader2/backtrader/issues/61

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