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/

    How to track orders? Kelly criterion sizers?

    General Code/Help
    2
    5
    2630
    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.
    • T
      Taewoo Kim last edited by

      Before I start, thanks to @administrators for answering my complete noob questions.

      1. Another rather noob'ish question - How do I track orders? If I have multiple long positions, do I need to keep track of Order object's ref id in the strategy object? (unique order identifier) But how would I reference them for running calculations / determining exit ? Wouldn't make sense to run those numbers at every next() / bar

      2. Is there a built-in kelly criterion based sizer that takes into consideration account balance / equity, leverage past wins/losses, and auto-sets the position sizes?

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

        @Taewoo-Kim said in How to track orders? Kelly criterion sizers?:

        1. Another rather noob'ish question - How do I track orders? If I have multiple long positions, do I need to keep track of Order object's ref id in the strategy object? (unique order identifier) But how would I reference them for running calculations / determining exit ? Wouldn't make sense to run those numbers at every next() / bar

        You can keep the order instance returned by the different order-generation methods (buy, sell, close, order_target_xxx, buy_bracket) or later when the changes in status is notified to the strategy via notify_order

        You probably want to do it when the status indicates that the order has Completed.

        1. Is there a built-in kelly criterion based sizer that takes into consideration account balance / equity, leverage past wins/losses, and auto-sets the position sizes?

        No. But the questions seems to imply that the Sizer will do everything alone (this simply may be a wrong interpretation). For clarification: the Sizer does only calculate sizes. It doesn't generate orders. The generation of orders triggers the calling of the Sizer. Because a sizer has access to the strategy and broker, it can return a size having a specific position size in mind.

        1 Reply Last reply Reply Quote 0
        • T
          Taewoo Kim last edited by

          You can keep the order instance returned by the different order-generation methods (buy, sell, close, order_target_xxx, buy_bracket) or later when the changes in status is notified to the strategy via notify_order

          The latter part I get. Let me explain

          in the tutorial

           if len(self) >= (self.bar_executed + 5):
                      # SELL, SELL, SELL!!! (with all possible default parameters)
                      self.log('SELL CREATE, %.2f' % self.dataclose[0])
          
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.sell()
          

          if I have multiple positions open that are using the same "strategy" (close after 5 bars) but opened at different times, how do I tell which order to sell?

          Unless the only way is to keep the orders in a list.. and iterate through the loop at every next().. which seems very inefficient.

          PS: thanks for being patient with my noob questions

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

            There are different things that can play a role here:

            • orders
            • positions
            • trades

            You talk about tracking orders and positions, but it would seem you actually care more about the trade. The strategy is also notified about trades with

            def notify_trade(self, trade):
            

            And the trade offers you 2 things which can help you keep track of how long you have been holding that particular trade:

            • dt (float representing the timestamp, but you can get a datetime instance via trade.open_datetime())
            • baropen (int which holds the len when the trade was started)

            Possible approach:

            • keep the notified trades in an active list

            • when len(trade.data) >= (trade.baropen + 5)

              The reason to use trade.data for the len is because with multiple data sources, each can trade a different number of days.

            • execute a self.close(trade.data) which will aim at closing the trade (and therefore the position)

            • when the trade is notified as closed, remove the trade from the active list

              You could even consider having an intermediate list for trades which are pending closure because an order is pending execution.

            If you are brave you can reverse with

            • self.sell(size=trade.size * 2

            • The current trade will be notified as trade.isclosed == True and a new trade will be notified as open with trade.justopened == True

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

              Some order tracking in this post Blog - Multi Example

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