Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Scaling in and Sizers

    General Code/Help
    2
    2
    1329
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • RandyT
      RandyT last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by backtrader

        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 (or SizerBase, 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 the Sizer there is a strategy member attribute, which allows the sizer to query whatever interfaces the strategy may have. The rationale behind that: abstracting the Strategy from any logic related to position sizing, allowing it to concentrate on the logic of entering/exiting the market.

        The Sizerattributes are describe here: https://www.backtrader.com/docu/sizers/sizers.html#backtrader.Sizer

        Over the strategy attribute 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()
        
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors