Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting with custom benchmark_rets got error (VectorBT used undefined attribute 'obj') #726

Open
daviddwlee84 opened this issue Jul 4, 2024 · 0 comments

Comments

@daviddwlee84
Copy link

When I tried to override the default benchmark returns #247 .

I found that VectorBT is try to broadcast it to an undefined "obj".

AttributeError: 'Portfolio' object has no attribute 'obj'

benchmark_rets = broadcast_to(benchmark_rets, self.obj)

Reproduction code

Simulate comparing two Portfolio results

import vectorbt as vbt
import numpy as np
import pandas as pd

close1 = pd.DataFrame(
    {
        "symbol_a": np.abs(np.random.randn(10)) * 10,
        "symbol_b": np.abs(np.random.randn(10)) * 10,
    }
)
close2 = close1[["symbol_a"]]

raw_signal1 = np.random.randn(10)
signal1_a = (raw_signal1 > 0.5) * 1 + (raw_signal1 < -0.5) * -1
signal1_b = (raw_signal1 > 0.5) * 1 + (raw_signal1 < -0.5) * -1
signal1 = pd.DataFrame({"symbol_a": signal1_a, "symbol_b": signal1_b})

raw_signal2 = pd.Series(np.random.randn(10), name="symbol_a")
signal2 = (raw_signal2 > 0.5) * 1 + (raw_signal2 < -0.5) * -1

pf1 = vbt.Portfolio.from_signals(
    close1, entries=signal1 == 1, exits=signal1 == -1, freq="T"
)
pf2 = vbt.Portfolio.from_signals(
    close2, entries=signal2 == 1, exits=signal2 == -1, freq="T"
)

# ok
print(pf1.stats(settings=dict(benchmark_rets=pf2.returns())))
# ok
print(pf1.stats(column="symbol_a", settings=dict(benchmark_rets=pf2.returns())))
# ok
print(pf2.stats(settings=dict(benchmark_rets=pf1['symbol_a'].returns())))

# ---- error ----

# works if I comment the bug line
pf1.plot(
    column="symbol_a",
    settings=dict(benchmark_rets=pf2.returns()),
    subplots=[
        "orders",
        "trade_pnl",
        "cum_returns",
        "drawdowns",
        "underwater",
        "asset_flow",
        "asset_value",
        "assets",
        "cash",
        "cash_flow",
        "gross_exposure",
        "net_exposure",
        "trades",
        "value",
    ],
)

# works if I comment the bug line
pf2.plot(
    column="symbol_a",
    settings=dict(benchmark_rets=pf1.returns()),
    subplots=[
        "orders",
        "trade_pnl",
        "cum_returns",
        "drawdowns",
        "underwater",
        "asset_flow",
        "asset_value",
        "assets",
        "cash",
        "cash_flow",
        "gross_exposure",
        "net_exposure",
        "trades",
        "value",
    ],
)

Version

  • vectorbt: 0.26.1
  • Python 3.8.13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant