Skip to content

Commit 5e148a0

Browse files
authored
Add orders associated with closed trades on streaming (#9282)
1 parent a25e041 commit 5e148a0

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

Common/Statistics/Trade.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,35 @@ public TimeSpan Duration
143143
/// The IDs of the orders related to this trade
144144
/// </summary>
145145
public HashSet<int> OrderIds { get; init; } = new HashSet<int>();
146+
147+
/// <summary>
148+
/// Creates a new instance of the <see cref="Trade"/> class
149+
/// </summary>
150+
public Trade()
151+
{
152+
}
153+
154+
/// <summary>
155+
/// Creates a new instance of the <see cref="Trade"/> class by copying another trade
156+
/// </summary>
157+
/// <param name="other">The trade to copy</param>
158+
public Trade(Trade other)
159+
{
160+
Id = other.Id;
161+
_symbols = other._symbols != null ? [.. other._symbols] : null;
162+
EntryTime = other.EntryTime;
163+
EntryPrice = other.EntryPrice;
164+
Direction = other.Direction;
165+
Quantity = other.Quantity;
166+
ExitTime = other.ExitTime;
167+
ExitPrice = other.ExitPrice;
168+
ProfitLoss = other.ProfitLoss;
169+
TotalFees = other.TotalFees;
170+
MAE = other.MAE;
171+
MFE = other.MFE;
172+
EndTradeDrawdown = other.EndTradeDrawdown;
173+
IsWin = other.IsWin;
174+
OrderIds = [.. other.OrderIds];
175+
}
146176
}
147177
}

Engine/Results/BaseResultsHandler.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ protected virtual List<Trade> GetDeltaTrades(List<Trade> trades, string lastTrad
469469
{
470470
LastTradeId = trade.Id;
471471
deltaTrades ??= new List<Trade>();
472-
deltaTrades.Add(trade);
472+
var packetTrade = new PacketTrade(trade, trade.OrderIds.Select(Algorithm.Transactions.GetOrderById).Where(order => order != null).ToList());
473+
deltaTrades.Add(packetTrade);
473474
if (shouldStop(deltaTrades.Count))
474475
{
475476
break;
@@ -1234,5 +1235,20 @@ protected virtual void UpdateBenchmarkValue(DateTime time, bool force = false)
12341235
_benchmarkValue = new ReferenceWrapper<decimal>(Algorithm.Benchmark.Evaluate(time).SmartRounding());
12351236
}
12361237
}
1238+
1239+
/// <summary>
1240+
/// Helper class to store a trade along with its associated orders.
1241+
/// Used when sending trade updates to the front end.
1242+
/// </summary>
1243+
private class PacketTrade : Trade
1244+
{
1245+
public List<Order> Orders { get; set; }
1246+
1247+
public PacketTrade(Trade trade, List<Order> orders)
1248+
: base(trade)
1249+
{
1250+
Orders = orders;
1251+
}
1252+
}
12371253
}
12381254
}

0 commit comments

Comments
 (0)