How to build a sizer that can use some measure of volatility, like ATR, and the multiplier for the future to determine position sizing?
-
The following is my attempt to make a custom position sizer based on ATR and the multiplier for the given future. I'm sure this is relatively easy but I seem to have hit a wall.
I seem to have a position size of 0 for every trade.
What am I getting wrong?Thanks.
class comm_sizer(bt.Sizer): def __init__(self, atr): #self.atr_parameter= atr_parameter atr= self.atr risk_per_trade=5000 atr_risk_per_trade=5 sizing = math.floor((risk_per_trade/(mult*atr_risk_per_trade*atr))) # position = self.broker.getposition(data) def _getsizing(self,comminfo,cash,data,isbuy): return sizing
-
You may have already checked this, but in case you haven't.
Getting zero size for every trade could imply there's not enough cash to execute the transaction. Check to see if you are getting margined out. Read "Orders -Order Status Values"
-
@t3chap said in How to build a sizer that can use some measure of volatility, like ATR, and the multiplier for the future to determine position sizing?:
I seem to have a position size of 0 for every trade.
With the following code ...
@t3chap said in How to build a sizer that can use some measure of volatility, like ATR, and the multiplier for the future to determine position sizing?:
class comm_sizer(bt.Sizer): def __init__(self, atr): #self.atr_parameter= atr_parameter atr= self.atr risk_per_trade=5000 atr_risk_per_trade=5 sizing = math.floor((risk_per_trade/(mult*atr_risk_per_trade*atr))) # position = self.broker.getposition(data) def _getsizing(self,comminfo,cash,data,isbuy): return sizing
It would be a miracle if you get something other than an Exception, both in
__init__
and in_getsizing
.In any case to use the
ATR
you probably want to access the strategy attributes (where theATR
will have been instantiated), taking advantage of the fact that the sizers have an attribute member calledstrategy