vectorbt icon indicating copy to clipboard operation
vectorbt copied to clipboard

How to limit the number of assets in portfolio mode?

Open gordonhui112 opened this issue 4 years ago • 6 comments

gordonhui112 avatar Jun 11 '21 15:06 gordonhui112

You need to elaborate more on this.

polakowo avatar Jun 11 '21 15:06 polakowo

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 avatar Jun 11 '21 16:06 gordonhui112

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

polakowo avatar Jun 11 '21 16:06 polakowo

Thanks you. Because I want to check out the non-coupounding effect of the strategy.

gordonhui112 avatar Jun 12 '21 11:06 gordonhui112

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

polakowo avatar Jun 12 '21 11:06 polakowo

Thank you!

gordonhui112 avatar Jun 12 '21 11:06 gordonhui112