For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Scaling out of Positions?

  • Hello, I've got quite a complex system built in Backtrader - but now I'm working on optimizing my money management, which is the most import aspect of trading altogether! Right now - I'm attempting to scale out of an existing position at a take profit level. To try and accomplish this I've tried several methods. I'm having some trouble deciphering the output of my logging method which is called in the notify_order() method because there are so many orders being processed. The lagged representation (which I fully understand) of buy/sell & profit/loss signals makes it hard for me to tell if I have it set up right. Here is my first method for this management scheme:

    (1) Submit One Bracket Order
    My method for a buy bracket

    main =*size, exectype=bt.Order.Market, transmit=False)
    stop_loss = self.sell(size=2*size,exectype=bt.Order.Stop,price=sl,parent=main,transmit=False)
    take_profit = self.sell(size=size,exectype=bt.Order.Limit,price=tp,parent=main,transmit=True)

    Where "sl" and "tp" are respective predefined exit points in the market. Notice here that my take profit order is 1/2 the size of my main order. This is to "scale out" of the market and leave half of the trade running. Has anybody scaled out of a position in Backtrader? And if so, is the the proper way to achieve that goal? What's weird is that by creating a dictionary of orders and then employing a "cleaning" method on ever call of next() to remove orders that are not alive() I can track how many orders are still open. At the end of my backtesting sweeps sometimes I see up to 100+ orders leftover in my using this method. So I feel like they are not executing properly! Maybe calling self.close() never gets rid of the stop loss orders that were never triggered?

    My other option is to create a secondary set of orders: (1) main order and (2) stop loss. This order would be my "runner" to close manually. The first order would be a bracket just like above - but it would have size=size instead of 2*size so that my first bracket would essentially be my scale out order. This seems less elegant makes it even harder for me to tell what is happening when I'm looking at the logs.

Log in to reply