Two things concerns me in your sizer:
- you don't have provision for size definition in case of short position
- I don't think that this line should work, but I might be wrong
size = math.floor((cash * self.p.risk) / data)
I don't think that
data is a price you want.
So reworking one of the built in sizers I would try the following (not tested):
class maxRiskSizer(bt.Sizer): params = (('risk', 20),) def __init__(self): if self.p.risk > 1 or self.p.risk < 0: raise ValueError('The risk parameter is a percentage which must be entered as a float. e.g. 0.5') def _getsizing(self, comminfo, cash, data, isbuy): position = self.broker.getposition(data) if not position: size = math.floor((cash * self.p.risk) /data.close) else: size = position.size return size