vectorbt icon indicating copy to clipboard operation
vectorbt copied to clipboard

How to backtest a portfolio not individual assets?

Open AlexanderMoonBit opened this issue 2 years ago • 5 comments

Hello there!

Vectorbt is very straightforward for backtesting individual assets but I can't find any information about a portfolio performance? My code works for the individual performance but how do I make it into a portfolio, where in case of 2 assets each can have a position of 50% and stats + plot shows not individual performance but aggregate?

Currently, I use the below to generate the pf where crypto_prices, entries and exits are DataFrames (e.g BTC-USD and ETH-USD)

pf = vbt.Portfolio.from_signals(crypto_prices, entries, exits, fees = 0.001, slippage=0.001)

Thanks!

AlexanderMoonBit avatar Jun 27 '22 08:06 AlexanderMoonBit

Pass group_by=True and cash_sharing=True

polakowo avatar Jun 27 '22 08:06 polakowo

Haha jeez so simple! So if I understand. If I have 10 assets, it will max take a 10% position even when the other assets are not invested by simply using group_by = True and cash_sharing = True.

AlexanderMoonBit avatar Jun 27 '22 08:06 AlexanderMoonBit

If you want an equally-weighted portfolio, don't use from_signals, use from_orders: https://vectorbt.dev/api/portfolio/base/#vectorbt.portfolio.base.Portfolio.from_orders

vbt.Portfolio.from_orders(
    close,
    1/2,  # allocate 50% to each asset, if there are three assets then use 1/3
    size_type='targetpercent',
    call_seq='auto',  # sell before buy
    group_by=True,
    cash_sharing=True,
    fees=0.001, 
    slippage=0.001
)

polakowo avatar Jun 27 '22 08:06 polakowo

Badass thanks a lot!

AlexanderMoonBit avatar Jun 27 '22 08:06 AlexanderMoonBit

@polakowo just a last clarification :)

With vbt.Portfolio.from_orders you shared above its always equal weight.

I meant that I have signals for true/false entries and exits and when there is an entry signal that asset can max take up 1/7 of the portfolio in case the portfolio universe is 7 assets. I would only want the pf to adjust when a signal changes meaning an asset is sold or bought.

Do I understand correctly then I would create an array_size based on my boolean signals and 1/7 logic like this

df_size

            BTC-USD.           ETH-USD.  etc.
date1.        0.14                 0
date 2        0                    0
etc.

Making your code becoming

vbt.Portfolio.from_orders(
    close,
    df_size,  #adjusted
    size_type='targetpercent',
    call_seq='auto',  # sell before buy
    group_by=True,
    cash_sharing=True,
    fees=0.001, 
    slippage=0.001
)

OR

Add to from_signals (size= 1/7) ?

AlexanderMoonBit avatar Jun 27 '22 09:06 AlexanderMoonBit