From b0d5310a4644292aa2219c6a69d34f62b8836811 Mon Sep 17 00:00:00 2001 From: Felix Bertram Date: Thu, 11 Apr 2024 23:03:07 -0700 Subject: [PATCH] fix issue w/ incorrect prices for child strategies of v2 algorithms --- .../Simulator/v2/Core/Plotter.cs | 5 ++++- TuringTrader.Tests/v2/T208_Algorithm.cs | 16 ++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/TuringTrader.Simulator/Simulator/v2/Core/Plotter.cs b/TuringTrader.Simulator/Simulator/v2/Core/Plotter.cs index e1678c46..c40e507e 100644 --- a/TuringTrader.Simulator/Simulator/v2/Core/Plotter.cs +++ b/TuringTrader.Simulator/Simulator/v2/Core/Plotter.cs @@ -86,6 +86,9 @@ public void AddTargetAllocation() var names = new Dictionary(); var prices = new Dictionary(); + // NOTE: the v2 wrapper for v1 child algorithms doesn't advance SimDate. + // As a workaround, we get lastSimDate from the top-level v2 algorithm. + var lastSimDate = Algorithm.SimDate; var lastRebalanceDate = Algorithm.Account.TradeLog.Last().OrderTicket.SubmitDate; void addAssetAllocation(Algorithm algo, double scale = 1.0) @@ -112,7 +115,7 @@ void addAssetAllocation(Algorithm algo, double scale = 1.0) { holdings[ticker] = 0.0; names[ticker] = asset.Description; - prices[ticker] = asset.Close[0]; + prices[ticker] = asset.Close[lastSimDate]; } holdings[ticker] += kv.Value * scale; diff --git a/TuringTrader.Tests/v2/T208_Algorithm.cs b/TuringTrader.Tests/v2/T208_Algorithm.cs index 2e5b13e6..6dff55f6 100644 --- a/TuringTrader.Tests/v2/T208_Algorithm.cs +++ b/TuringTrader.Tests/v2/T208_Algorithm.cs @@ -160,11 +160,6 @@ public override void Run() var algo1 = new SwitchHalfTime_v1 { HoldFirst = true, }; var algo2 = new SwitchHalfTime_v1 { HoldFirst = false, }; - // NOTE: we can access the v2 wrapper via the - // asset's meta information - var algo1v2 = Asset(algo1).Meta.Generator; - var algo2v2 = Asset(algo2).Meta.Generator; - SimLoop(() => { if (IsFirstBar) @@ -174,11 +169,15 @@ public override void Run() } }); - NumChildTrades = algo1v2.Account.TradeLog.Count + algo2v2.Account.TradeLog.Count; - Plotter.AddTargetAllocation(); Plotter.AddTradeLog(); Plotter.AddHistoricalAllocations(); + + // NOTE: we can access the v2 wrapper via the + // asset's meta information + var algo1v2 = Asset(algo1).Meta.Generator; + var algo2v2 = Asset(algo2).Meta.Generator; + NumChildTrades = algo1v2.Account.TradeLog.Count + algo2v2.Account.TradeLog.Count; } } @@ -195,7 +194,8 @@ public void Test_v1() var alloc = algo.Plotter.AllData[Simulator.Plotter.SheetNames.HOLDINGS]; Assert.AreEqual(alloc.Count, 1); Assert.AreEqual((string)alloc[0]["Symbol"], "$SPX"); - Assert.AreEqual(double.Parse(((string)alloc[0]["Allocation"]).TrimEnd('%')), 125.00, 1e-5); // v2 test: 125.35 + Assert.AreEqual(125.00, double.Parse(((string)alloc[0]["Allocation"]).TrimEnd('%')), 1e-5); // v2 test: 125.35 + Assert.AreEqual(3839.50, double.Parse(((string)alloc[0]["Price"]).TrimStart('$')), 1e-03); // value from 12/29/2021 var last = algo.Plotter.AllData[Simulator.Plotter.SheetNames.LAST_REBALANCE]; Assert.IsTrue(last.Count == 1);