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/

    buy_bracket fails, when Sizer returns size=0

    General Code/Help
    2
    6
    287
    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.
    • F
      forexDealer last edited by

      Hello,

      I have coded a sizer, which sometimes returns a size of 0. That is intended, because what I want to have is that it returns the size based on my risk and the stoploss. If the stoploss is too far away, nothing should be traded. Also, if the minimum lot size (e.g. micro-lot = 0.01) has not been met, it should return size=0.
      However, buy_bracket/sell_bracket do not like that.
      If the size becomes 0, I get this error:

      os = self.buy_bracket(
        File "/usr/lib/python3.8/site-packages/backtrader/strategy.py", line 1155, in buy_bracket
          kargs['size'] = o.size
      AttributeError: 'NoneType' object has no attribute 'size'
      

      A simple example of a sizer that triggers that error would be:

      class CustomSizer(bt.Sizer):
          def _getsizing(self, comminfo, cash, data, isbuy):
                return 0
      

      It can be checked for example with the bracket example in the documentation

      Cheers,
      forexDealer

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

        You can check the size before issuing the order, if it is more than 0, than issue the order, otherwise not.

        • 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
        1 Reply Last reply Reply Quote 0
        • F
          forexDealer last edited by

          How can I control inside the _getsizing(...) method whether an order should be send or not?
          The buy_bracket(...) method is calling _getsizing(...) automatically if I don't pass the size as an argument in buy_bracket(...).

          Of course I could also just pass the size manually as an argument in buy_bracket, but then what is the purpose of having a sizer?

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

            @forexDealer said in buy_bracket fails, when Sizer returns size=0:

            but then what is the purpose of having a sizer?

            To define non-zero position size evidently. I doubt that it is possible to send zero size order in real life, and bt reflects real life trading.

            bt doesn't issue order for zero size order call. And in this case for bracket order it will be no parent order, loos like this is the reason for error.

            • 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
            1 Reply Last reply Reply Quote 0
            • F
              forexDealer last edited by

              @ab_trader said in buy_bracket fails, when Sizer returns size=0:

              I doubt that it is possible to send zero size order in real life, and bt reflects real life trading.

              So it would be good if a sizer would be able to prevent issuing an order. Which is currently not possible, right?

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

                @forexDealer said in buy_bracket fails, when Sizer returns size=0:

                So it would be good if a sizer would be able to prevent issuing an order. Which is currently not possible, right?

                I don't think that sizer should prevent something, it just defines size of the position and it works for cases when signal to enter position and size of the position are independent. Seems this is not your case, so use another approach.

                • 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
                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors