live: record each session's target and its frictionless return; compare shows Ideal and Gap
A $10k book in whole shares holds SNDK at 17.8% for a 10% target, so the live record mixes the strategy with rounding and fee noise (2026-09-25..10-12: overnight_recovery_top10 -9.0% live vs -7.65% at its own weights). Each daily_log entry now stores the target held from that close and ideal_return, the previous target's return to this close with fractional shares, no fees, no resize band and no idle cash. compare prints Ideal and Gap (live minus ideal) over the recorded days. The recorded returns compound to the frictionless ledger (new contract test); an in-memory rerun of 10-09/10-12 on live states reproduces the recorded equity to the cent. systemd: SuccessExitStatus=143 on the monitor and overnight-paper units, so a stop or restart no longer logs 'Failed' and the journal shows only crashes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -52,6 +52,26 @@ class ExecutionContracts(unittest.TestCase):
|
||||
self.assertAlmostEqual(state["daily_equity"][str(day.date())], batch["equity"].loc[day], places=2)
|
||||
self.assertEqual(state["trade_log"], batch["trade_log"])
|
||||
|
||||
def test_recorded_ideal_return_compounds_to_the_frictionless_ledger(self):
|
||||
dates = pd.bdate_range("2026-01-05", periods=6)
|
||||
prices = pd.DataFrame({"A": [100, 110, 104, 70, 90, 95], "B": [20, 21, 22, 22, 23, 21.5]}, index=dates)
|
||||
targets = pd.DataFrame([[.5, .5], [.5, .5], [0, .9], [0, 1], [.6, .4], [.6, .4]],
|
||||
index=dates, columns=prices.columns)
|
||||
ideal = simulate_book(targets, prices, 10000, transaction_cost=0, fixed_fee=0, integer_shares=False,
|
||||
resize_band=0, min_trade_value=0)
|
||||
state = trader.init_state("us", "test", 10000)
|
||||
for day in dates: # live-like: whole shares, fees, the 10% band
|
||||
trader._run_session(state, "us", day, "B", prices, prices, {"tx_cost": .001, "fixed_fee": 2.},
|
||||
True, targets.loc[day])
|
||||
log = state["daily_log"]
|
||||
self.assertNotIn("ideal_return", log[0])
|
||||
self.assertEqual(log[2]["target"], {"B": 0.9})
|
||||
growth = np.cumprod([1 + e["ideal_return"] for e in log[1:]])
|
||||
np.testing.assert_allclose(growth, ideal["equity"].iloc[1:] / ideal["equity"].iloc[0], rtol=1e-6)
|
||||
window = trader._ideal_window(state)
|
||||
self.assertEqual(window["start"], str(dates[0].date()))
|
||||
self.assertAlmostEqual(window["ideal"], growth[-1] - 1)
|
||||
|
||||
def test_a_delisted_stock_is_settled_at_its_last_print_in_both_ledgers(self):
|
||||
dates = pd.bdate_range("2026-01-05", periods=9)
|
||||
# A is acquired: last print on day 2, out of the index from day 4.
|
||||
|
||||
Reference in New Issue
Block a user