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/

    issue with self.Position.size

    General Code/Help
    3
    6
    3494
    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.
    • R
      Rstrong last edited by

      I am trying to use a strategy that buys or sells based on how many shares are currently in the position. My issue arises when I test multiple data feeds.

      I have my next function iterate through each data, and for most things I want, I can specify to use the data that is currently being iterated through.

      However, since self.position.size only produces the size for datas[0], when i try to reference size in subsequent datas it produces 0s. Below are the print functions I use.

      print('Start Short {} {}'.format(Time(0),self.position.size))
      print('Add Short {} {}'.format(Time(0),self.position.size))
      print('Close {} {}'.format(Time(0),self.position.size))
      

      Below are a sample of outputs where the first datafeed prints the size fine but the one that trades right after prints 0s for size.

      Start Short 10:03:00 0
      Add Short 10:08:00 -100
      Close 10:46:00 -200
      
      Start Short 09:40:00 0
      Add Short 09:43:00 0
      Add Short 09:47:00 0
      Close 10:13:00 0
      

      I was looking for functions in the documentation that allow you to specify which data to pull the size number from, but couldn't find any. The best I could find was self.broker.getposition() but that produces values in addition to size and i cant pull the size alone.

      How do I get around this?

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by ab_trader

        Docs - Strategy

        Try self.getposition(data=your_data)

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        R 1 Reply Last reply Reply Quote 0
        • R
          Rstrong @ab_trader last edited by

          @ab_trader self.getposition(data= your_data) produces -
          -Size: 0

          • Price: 0.0
          • Price orig: 0.0
          • Closed: 0
          • Opened: 0
          • Adjbase: None

          This doesnt allow me to get just the size so that I can make trades on it. I want to be able to say something like :

           if self.position_size(data = my_data) > 500:
              (some logic)
          

          @backtrader do you have any recommendations?

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            Why can't you access the size? It's an attribute of the returned Position instance

            the_size = self.getposition(data=thedata).size
            

            Isn't that working?

            R 1 Reply Last reply Reply Quote 1
            • R
              Rstrong @backtrader last edited by

              @backtrader that solved it!

              I didnt know that .size was an attribute of getposition. If its in the docs i didnt see it.

              Thanks for the help!

              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators last edited by

                It's documented: Docs - Position

                In any case Python doesn't hide the attributes and you can use the inspect module or simple dir to see what's available.

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