Forbid short selling
Since short selling may be forbidden in markets like CHINA AShares, I was wondering if there were any settings in Backtrader ensuring that.
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 = self.broker.getposition(data) if not position.size: return 0 # do not sell if nothing is open return self.p.stake
That's only a patch which works if the position is
0and only works if you don't manually specify the
sizewhich 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.