Skip to content

Commit

Permalink
fix issue w/ incorrect prices for child strategies of v2 algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
fbertram committed Apr 12, 2024
1 parent 544bd05 commit b0d5310
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
5 changes: 4 additions & 1 deletion TuringTrader.Simulator/Simulator/v2/Core/Plotter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public void AddTargetAllocation()
var names = new Dictionary<string, string>();
var prices = new Dictionary<string, double>();

// 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)
Expand All @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions TuringTrader.Tests/v2/T208_Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}

Expand All @@ -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);
Expand Down

0 comments on commit b0d5310

Please sign in to comment.