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/

    Forbid short selling

    General Code/Help
    2
    4
    195
    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.
    • Y
      yoghurtshawn last edited by

      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!

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

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

        1 Reply Last reply Reply Quote 1
        • Y
          yoghurtshawn last edited by

          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 = self.broker.getposition(data)
                if not position.size:
                    return 0  # do not sell if nothing is open
          
                return self.p.stake
          
          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            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.

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