-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shift portfolio in sep module to avoid cyclic dependency (#349)
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |