pmorissette / bt

bt - flexible backtesting for Python
http://pmorissette.github.io/bt
MIT License
2.25k stars 428 forks source link

Can bt be used to buy certain dollar amounts of stock (vs rebalancing to weights)? #385

Open brettelliot opened 2 years ago

brettelliot commented 2 years ago

Hi,

Let's say i want to build a trend strategy with a universe of 10 stocks. Id like the strategy to invest up to 10% of the total portfolio value when any stock is trending up. Say stock A goes up, and i end up buying 100 shares. When stock B goes up, I want to invest up to 10% of the current portfolio value (which is hopefully higher since we invested in stock A because it was going up). But I don't want to change anything about stock A's position. We should keep the 100 shares of stock A no matter what percentage of the portfolio they amount to until the strategy decides to sell stock A.

Can bt backtest a strategy like this?

Helikoptermann2 commented 8 months ago

@brettelliot Did you ever find a solution for this? I'm stuck with the exact same problem. I don't want the algo to rebalance with every transaction made and just keep the existing positions as they are

dqnne commented 4 weeks ago

@Helikoptermann2 Hey, I'm facing the same issue. Did you ever figure out what to do?

I feel like it would be possible with some custom algos but I don't know how to implement it exactly.

Helikoptermann2 commented 3 weeks ago

No, I did not find a solution and then stepped away from this package. But would still be interested in a solution

dqnne commented 3 weeks ago

Looking at the docs I see that the SecurityBase class has methods for allocate() and transact(), which are meant to buy/sell for a specific amount/quantity. I think we would just have to create a custom algo for buying/selling with these methods instead of using rebalance.

There's also the StrategyBase class which also has methods for allocate() and transact(). These seem to behave a little differently.

https://pmorissette.github.io/bt/bt.html#bt.core.SecurityBase https://pmorissette.github.io/bt/bt.html#bt.core.StrategyBase