Scaling in and Sizers
I need to implement the ability to scale into a trade. Desired logic is as follows:
- Desired trade size in number of contracts is calculated using VaR and determined that the current market and account size requires X number of contracts.
- I want to scale into the trade in 3 tranches
As I have currently implemented a VaR sizer, I am returning the total number of contracts. This can be easily modified to return the remaining number of contracts based on the current position size.
However, in the Strategy, I now need to check not only if I have an open position, but whether I have a full position size based on VaR.
I'm not sure that the sizer route is the best approach here given other discussions that suggest that I cannot query the sizer from the Strategy.
How best to implement this?
Nothing prevents you from querying the
Sizerwhich has been added to the strategy.
- See the getsizer method of the strategy
Since you have subclassed
SizerBase, which is the same), you control any extra interfaces/methods which may help you in the querying.
With that in mind, the
Sizerconcept was developed with the other direction in mind and that's why inside the
Sizerthere is a
strategymember attribute, which allows the sizer to query whatever interfaces the strategy may have. The rationale behind that: abstracting the
Strategyfrom any logic related to position sizing, allowing it to concentrate on the logic of entering/exiting the market.
Sizerattributes are describe here: https://www.backtrader.com/docu/sizers/sizers.html#backtrader.Sizer
strategyattribute you can also reach the broker, to for example query the actual net liquidation value
class MySizer(bt.Sizer): def mymethod(self): value = self.strategy.broker.get_value()