Skip to content

Commit 1120635

Browse files
added capital reinvested test
1 parent 5843a14 commit 1120635

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

tests/test_strategy.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,44 @@ def test_simulation_fungible_futures(self):
305305
self.assert_simulation_equal(sim_res, exp_sim_res)
306306

307307
def test_simulation_fungible_reinvest_futures(self):
308-
pass
308+
RISK_TARGET = 0.12
309+
portfolio = self.make_portfolio(["ES"])
310+
sig_val = 1
311+
signal = self.make_signal(portfolio) * sig_val
312+
313+
sim_res = portfolio.simulate(signal, tradeables=False, reinvest=True,
314+
risk_target=RISK_TARGET)
315+
316+
rets = self.splice_returns(
317+
[("ESH2015", "2015-01-02", "2015-03-17"),
318+
("ESM2015", "2015-03-18", "2015-03-23")]
319+
)
320+
rets.iloc[0] = 0
321+
es_hlds1 = (1 + rets.loc[:"2015-03-17"]).cumprod() * self.CAPITAL * sig_val * RISK_TARGET # NOQA
322+
NEW_CAPITAL = es_hlds1.diff().sum() + self.CAPITAL
323+
rets2 = rets.loc["2015-03-17":]
324+
rets2.iloc[0] = 0
325+
es_hlds2 = (1 + rets2.loc["2015-03-17":]).cumprod() * NEW_CAPITAL * sig_val * RISK_TARGET # NOQA
326+
es_hlds = pd.concat([es_hlds1.iloc[:-1], es_hlds2], axis=0)
327+
es_hlds = pd.DataFrame(es_hlds)
328+
es_hlds.columns = ["ES1"]
329+
330+
es_pnl = pd.concat([es_hlds1.diff(), es_hlds2.diff().iloc[1:]], axis=0)
331+
es_pnl.loc["2015-01-02"] = 0
332+
# account for missing settlement pricing data from sources
333+
trdble_dates = portfolio.tradeable_dates()
334+
hlds_exp = es_hlds.reindex(trdble_dates).fillna(method="ffill")
335+
pnls_exp = es_pnl.reindex(trdble_dates).fillna(value=0)
336+
pnls_exp.name = None
337+
338+
es_pre_rebal_hlds = es_hlds1.loc["2015-03-17"]
339+
vals = [self.CAPITAL * sig_val * RISK_TARGET,
340+
NEW_CAPITAL * sig_val * RISK_TARGET - es_pre_rebal_hlds]
341+
trds_exp = pd.DataFrame(
342+
vals,
343+
index=pd.DatetimeIndex(["2015-01-02", "2015-03-17"]),
344+
columns=["ES1"]
345+
)
346+
347+
exp_sim_res = self.make_container(hlds_exp, trds_exp, pnls_exp)
348+
self.assert_simulation_equal(sim_res, exp_sim_res)

0 commit comments

Comments
 (0)