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

# Pyramiding support

• Hi, Does backtrader support pyramiding?
What I meant is: buy 100 shares at 100, another 100 at 102, another 100 at 104. So we have total of 300 shares.
Sell them all at 102 if say stop loss is hit.

• I wrote percents sizer, which can calculate sizer of the order relative to current balance: https://github.com/mementum/backtrader/tree/master/backtrader/sizers.

You could start from this point to write your own writer. Then contribute it! :)

• @Nagesh-G.R. said in Pyramiding support:

What I meant is: buy 100 shares at 100, another 100 at 102, another 100 at 104. So we have total of 300 shares.
Sell them all at 102 if say stop loss is hit.

3 buy orders and 1 stop order are within the arsenal of the broker implementation in backtrader. But a pre-built function implementing such logic is not. What the platform offers is the possibility to easily (as easy as possible) implement those kind of logic policies.

• @Maxim-Korobov said in Pyramiding support:

percents sizer, which can calculate sizer of the order relative to current balance

What is the difference between your sizer and `order_target_percent`?

• "pyramiding" is what I have been referring to as "scaling in".

There are some challenges in implementing this that I have been unable to solve without also adding some persisted information about the trade that starts off this series of other trades. I've been looking at ways to possibly store this information with the broker (in my case IB) but there seems to be no way to add some metadata about the transaction with the order.

If it is simply a fixed number, then it is easy to use current position size to scale into that number. But as soon as the final target size is based on some percentage of account size and perhaps some market conditions, the challenge presents itself.

• @RandyT Thanks everyone for the reply. Randy, when you say a fixed number, is it possible to implement without much hassels say 4 times scaling in of 100 share each after it crosses 1*ATR each time? and then waiting for trailing stop loss to hit for exit? Do you have example implementation?

• @Maxim-Korobov Thanks. Will go through Sizer. I am still just beginning :-)

• @Nagesh-G.R. You have the ability to call the sizer from `strategy.next()` with `self.getsizing()`. This, coupled with the fixed sizer would return the the size you have given the sizer as your target goal.

Couple that with Target Orders, https://www.backtrader.com/docu/order_target/order_target.html

That should allow you to check your current `self.position.size` against the sizer response. (note that if you are trading short, self.position.size will provide a negative number)

If I understand your goal in your example properly, you would set the sizer to be 400 shares and enter 25% of that number every time you see the signal until your `self.position.size` == 400.

It would be simple to add a feature to the FixedSize sizer to handle some of the checking of current position vs. a goal of scaling in 1/4 at a time for example. I may toss a PR over to @backtrader to do that later today.

Hope that helps.

• @RandyT Hi Randy, Many thanks. Your understanding of my goal accurate. In the example, I would like to limit the size to 400 and enter 25% each time I see a signal until I reach the max size of 400. Also I am sure you are already aware, but just to make it clear, one more variable that comes into play is stop loss. Even if I have entered only 25% of my planned total position as a part of first order, if stop loss it hit (say 1 ATR), it should exit that 25%. Same is the case when 50% (say 1 ATR from the latest entry of 2nd order), 75% (say 1 ATR from the latest entry of 3rd order) and 100% (say 1 ATR from the latest entry of 4th order) size. Thanks a lot.

• @Nagesh-G.R. All of the logic about the strategy execution can go in `strategy.next()`. It may even be reasonable to create a separate method that monitors setting stops, etc. If you hit a stop, the BT broker will take care of exiting your position.

This brings up another area that needs some investigation on my part. It is possible to setup trailing stops and other fancy stop orders with IB. It would probably make sense to look at expanding the backtest broker to support those things to enable a true comparison and transition from backtesting and live trading.

• @RandyT Thanks Randy. As a first step if its able to take care of scaling up, that would be good.

• @RandyT Any luck with this?

• Sorry for any confusion but I am not working on anything specifically here. I've implemented at least a half dozen systems thus far and they each have different implementations that are specific to them. There is no real magic here. My earlier post was my attempt to give you pointers to the tools you will need to investigate to do this.

@backtrader did merge some changes that I submitted back to the development branch that allows you to specify number of tranches that you would like to use when using the Fixed sizer. That will in turn, give back a size that is some portion of your full position size. That, coupled with the other suggestions I made above should allow you to accomplish your goal.

Good luck

• #PR 274 https://github.com/mementum/backtrader/pull/274 from @RandyT is included in the `development` branch with tranches.

• @RandyT said in Pyramiding support:
Thanks Randy for getting back.

});