vectorbt
vectorbt copied to clipboard
How to limit the number of assets in portfolio mode?
You need to elaborate more on this.
Thanks for replying!
My question is how to limit the number of assets holding, let's say maximum 5 assets for a asset universe of 100, at every moment during the backtest.
Thank you!
@gordonhui112 you can only limit your capital exposure, not the position itself. If you use Portfolio.from_order_func, you can write your own logic that has such a limit (such as if c.shares_now >= your_limit: return NoOrder). But using from_signals or from_orders won't give you that. I can add such an option to vectorbt in the next release but I'm curious why you want it?
Thanks you. Because I want to check out the non-coupounding effect of the strategy.
@gordonhui112 you can still specify a fixed amount of assets to trade using size in both from_signals and from_orders. By default it's np.inf, but setting it to a finite number should do the trick. If you using from_orders, you can also control the target position size using size_type='targetshares', so passing size=10 will limit your position size to 10. To close the position, just do size=0.
Thank you!