For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
How can I create a sizer based on value?
-
I'm having trouble using the PercentSizer for live trading, because if I have a big short position what the sizer sees as cash is far from the real cash in my account. So I'd like to build a custom sizer that does exactly the same, but sizing on a percentage of Value instead of cash. This is what I tried, without success.
class ValuePercentSizer(bt.Sizer): params = ( ('percents', 20), ('retint', True), ) def __init__(self): pass def _getsizing(self, comminfo, value, data, isbuy): position = self.broker.getposition(data) if not position: size = value / data.close[0] * (self.params.percents / 100) else: size = position.size if self.p.retint: size = int(size) return size
-
@chrix I solved it like this
class ValuePercentSizer(bt.Sizer): params = ( ('percents', 20), ('retint', True),# return an int size or rather the float value ) def __init__(self): pass def _getsizing(self, comminfo, cash, data, isbuy): position = self.broker.getposition(data) accvalue = self.broker.getvalue() if not position: size = accvalue / data.close[0] * (self.params.percents / 100) else: size = position.size if self.p.retint: size = int(size) return size