using System; using System.Linq; using System.Text.Json.Nodes; const decimal StartCash = 10_000m; const decimal BuyPrice = 50_000m; const decimal SellPrice = 51_000m; const decimal Bought = 0.10m; const decimal FeeRate = 0.001m; var scenarios = new[] { (Name: "A", FeeInBase: false, Step: 0.0001m), (Name: "B", FeeInBase: true, Step: 0.0001m), (Name: "C", FeeInBase: true, Step: 0.001m) }; var rows = scenarios.Select(scenario => Replay(scenario.FeeInBase, scenario.Step)).ToArray(); if (rows[0][4] != 89.9m || rows[1][4] != 89.8051m || rows[2][2] != 0.0009m || rows[2][4] != 89.851m) { throw new InvalidOperationException("The ledger does not match the example."); } var output = new JsonArray(); for (int i = 0; i < scenarios.Length; i++) { var row = new JsonArray(JsonValue.Create(scenarios[i].Name)); foreach (decimal value in rows[i]) { row.Add((JsonNode?)JsonValue.Create(value)); } output.Add((JsonNode)row); } Console.WriteLine(output.ToJsonString()); static decimal[] Replay(bool feeInBase, decimal step) { decimal baseFee = feeInBase ? Bought * FeeRate : 0m; decimal quoteFee = feeInBase ? 0m : Bought * BuyPrice * FeeRate; decimal balance = Bought - baseFee; decimal sold = decimal.Floor(balance / step) * step; decimal remainder = balance - sold; decimal cash = StartCash - Bought * BuyPrice - quoteFee + sold * SellPrice * (1m - FeeRate); decimal markedProfit = cash + remainder * SellPrice - StartCash; return [balance, sold, remainder, cash, markedProfit]; }