-
With my understanding of (i) single asset, single trading signal I'm now trying to work out how to have multiple assets and multiple trading signals with a Setup
(I'm not sure what the keys for my When I build the What I've noticed
I'm able to access |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
@Sanjan611 here's an example: import pandas as pd
import vectorbt as vbt
price = pd.DataFrame({
'p1': [1, 2, 3, 4],
'p2': [5, 6, 7, 8]
})
price.columns.name = 'asset'
entries = pd.DataFrame({
'en1': [True, False, False, False],
'en2': [False, True, False, False],
'en3': [False, False, True, False],
'en4': [False, False, False, True]
})
entries.columns.name = 'entries'
exits = pd.DataFrame({
'ex1': [False, False, False, True],
'ex2': [False, False, False, True],
'ex3': [False, False, False, True],
'ex4': [False, False, False, True]
})
exits.columns.name = 'exits'
entries = entries.vbt.stack_index(pd.Index(['p1', 'p1', 'p2', 'p2'], name='asset'))
exits = exits.vbt.stack_index(pd.Index(['p1', 'p1', 'p2', 'p2'], name='asset'))
portfolio = vbt.Portfolio.from_signals(price, entries, exits) # not grouped portfolio
print(portfolio.total_return())
entries asset exits
en1 p1 ex1 3.000000
en2 p1 ex2 1.000000
en3 p2 ex3 0.142857
en4 p2 ex4 0.000000
Name: total_return, dtype: float64
print(portfolio.total_return(group_by='asset')) # group not grouped portfolio
asset
p1 2.000000
p2 0.071429
Name: total_return, dtype: float64
# or
portfolio = vbt.Portfolio.from_signals(price, entries, exits, group_by='asset') # grouped portfolio
print(portfolio.total_return())
asset
p1 2.000000
p2 0.071429
Name: total_return, dtype: float64
print(portfolio.total_return(group_by=False)) # ungroup grouped portfolio
entries asset exits
en1 p1 ex1 3.000000
en2 p1 ex2 1.000000
en3 p2 ex3 0.142857
en4 p2 ex4 0.000000
Name: total_return, dtype: float64 Broadcasting in |
Beta Was this translation helpful? Give feedback.
@Sanjan611 here's an example: