Navigation

    Backtrader Community

    • Register
    • 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/

    Portfolio Rebalancing with self.order_target_percent

    Indicators/Strategies/Analyzers
    3
    5
    2564
    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.
    • bartm
      bartm last edited by

      Hi. First off, thanks for building this great platform, I've found it very useful. Now to my problem at hand: I am backtesting a dynamic asset allocation strategy, with monthly rebalancing. In my current testing mode, I simply feed backtrader prices of 2 assets and rebalance on the first day of the month. I am running into margin/cash problems however, where rebalancing orders are not filled because this is an all-in strategy. So when the first order is a 'buy', often it does not get filled because there is no spare cash. A crude way of getting around this is to simply reduce the percent allocation per asset, which I have implemented. However, a better way would be to get backtrader to send the sell orders first when invoking the self.order_target_percent(). I can't figure out a way to do this however, and any help would be appreciated! Here is my code in the next method:

      percent = (1/len(self.datas))-.03
      if daymonth == lastday:
                  for index in range(0, len(self.datas)):
                      self.order = self.order_target_percent(self.datas[index], percent)
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        Orders are executed in the order you issued them: first issued, first executed. Issue selling orders first, then buying, should be fine.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 0
        • bartm
          bartm last edited by

          Thanks, yes that is what I intend to do, but can't figure out which classes/methods exactly control order issuance. I am relatively new to backtrader...the docs do not go into any details about how to control order issuance.

          1 Reply Last reply Reply Quote 0
          • P
            Paska Houso last edited by Paska Houso

            I was sure that this topic had been discussed before and google helped:

            • https://community.backtrader.com/topic/152/multi-asset-ranking-and-rebalancing/

            From that thread:

            @backtrader said in Multi-asset ranking and rebalancing:

            You are actually making the decision on which orders get sent first. If you want to send sell orders first, and without knowing what your algorithm/logic does, could be as easy as sorting the target % according to the change. The % being reduced (something will be sold) will be the first ones to be realized through an order_target_percent

            If we assume that you keep the percent calculation

            tosell = []
            tobuy = []
            for i, opnp in enumerate(zip(old_percents, new_percents)):
                op, np = opnp 
            
                if np < op:
                    tosell.append(i)
                else:
                    tobuy.append(i)
            
            for i in tosell:
                self.order_target_percent(self.datas[i], target=new_percents[i])
            
            for i in tobuy:
                self.order_target_percent(self.datas[i], target=new_percents[i])
            

            The code can for sure be optimized in several ways (depending on the surrounding conditions and data structures in play, but it should serve as a start)

            1 Reply Last reply Reply Quote 2
            • bartm
              bartm last edited by

              This is very helpful, thanks.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors