How to track orders? Kelly criterion sizers?



  • 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?


  • administrators

    @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.



  • 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


  • administrators

    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


  • administrators

    Some order tracking in this post Blog - Multi Example


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.