For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Forbid short selling

  • Hi community,

    Since short selling may be forbidden in markets like CHINA AShares, I was wondering if there were any settings in Backtrader ensuring that.

    Many thanks!

  • administrators

    Don't sell too much and you won't short sell.

  • It seems this LongOnly sizer could do the trick. Right?

    class LongOnly(bt.Sizer):
        params = (('stake', 1),)
        def _getsizing(self, comminfo, cash, data, isbuy):
          if isbuy:
              return self.p.stake
          # Sell situation
          position =
          if not position.size:
              return 0  # do not sell if nothing is open
          return self.p.stake

  • administrators

    That's only a patch which works if the position is 0 and only works if you don't manually specify the size which will trigger the usage of the sizer.

    Let me insist: don't sell when not needed and you won't short.

    It is really easy: if your code is selling (going to a minus position from either zero or a positive one) short ... your code has a problem.

Log in to reply