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



  • 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



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



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



  • @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.



  • @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?



  • @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.


Log in to reply
 

});