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
Sizer
which has been added to the strategy.- See the getsizer method of the strategy
Since you have subclassed
Sizer
(orSizerBase
, which is the same), you control any extra interfaces/methods which may help you in the querying.With that in mind, the
Sizer
concept was developed with the other direction in mind and that's why inside theSizer
there is astrategy
member attribute, which allows the sizer to query whatever interfaces the strategy may have. The rationale behind that: abstracting theStrategy
from any logic related to position sizing, allowing it to concentrate on the logic of entering/exiting the market.The
Sizer
attributes are describe here: https://www.backtrader.com/docu/sizers/sizers.html#backtrader.SizerOver the
strategy
attribute you can also reach the broker, to for example query the actual net liquidation valueclass MySizer(bt.Sizer): def mymethod(self): value = self.strategy.broker.get_value()