Skip to content

Commit

Permalink
shift portfolio in sep module to avoid cyclic dependency (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm authored Feb 18, 2024
1 parent b51536b commit 73e9178
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cvx/simulator/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,13 @@ def from_returns(cls, returns):

prices = returns2prices(returns)
return cls(prices=prices)

@classmethod
def from_weights(cls, weights, prices, initial_aum=1e6):
"""Build Futures Portfolio from weights"""
builder = cls(prices=prices, initial_aum=initial_aum)
for t, state in builder:
builder.weights = weights.loc[t[-1]]
builder.aum = state.aum

return builder
11 changes: 11 additions & 0 deletions cvx/simulator/shifter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cvx.simulator import Builder, Portfolio


def shift(portfolio: Portfolio, periods: int = 0):
"""Shift the portfolio by a number of periods"""
# shift the weights
weights = portfolio.weights.shift(periods)
builder = Builder.from_weights(
weights=weights, prices=portfolio.prices, initial_aum=portfolio.aum.iloc[0]
)
return builder.build()
26 changes: 26 additions & 0 deletions tests/test_applications/test_shifter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# from cvx.simulator import Builder
import pandas as pd

from cvx.simulator import Builder
from cvx.simulator.shifter import shift


def test_portfolio_cumulated(prices):
builder = Builder(prices=prices[["A", "B"]].tail(10), initial_aum=1e6)

for t, state in builder:
# hold one share in both assets
builder.cashposition = [1e5, 4e5]

# reduce the available aum by the costs
builder.aum = state.aum # - costs

portfolio = builder.build()
print(portfolio.weights)
print(portfolio.aum)

ppp = shift(portfolio=portfolio, periods=2)
print(ppp.weights)
print(ppp.aum)

pd.testing.assert_frame_equal(portfolio.weights.shift(2), ppp.weights)

0 comments on commit 73e9178

Please sign in to comment.