Skip to content

Commit b4cfa52

Browse files
committed
Update regression algorithm
1 parent fcd584e commit b4cfa52

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

Algorithm.CSharp/InsufficientMarginOrderUpdateRegressionAlgorithm.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class InsufficientMarginOrderUpdateRegressionAlgorithm : QCAlgorithm, IRe
2929
private OrderTicket _stopOrderTicket;
3030
private OrderTicket _limitOrderTicket;
3131
private OrderTicket _trailingStopOrderTicket;
32+
private bool _updatesReady;
3233

3334
public override void Initialize()
3435
{
@@ -78,33 +79,43 @@ public override void OnData(Slice data)
7879
TrailingAmount = 0.01m,
7980
};
8081
_trailingStopOrderTicket.Update(updateTrailingStopOrderSettings);
82+
_updatesReady = true;
8183
}
8284
}
8385

8486
public override void OnOrderEvent(OrderEvent orderEvent)
8587
{
86-
// Check if the order is invalid
87-
if (_stopOrderTicket != null && orderEvent.OrderId == _stopOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
88+
if (_updatesReady && orderEvent.Status == OrderStatus.Submitted)
8889
{
89-
throw new RegressionTestException($"Order {_stopOrderTicket.OrderId} with symbol {_stopOrderTicket.Symbol} should have been invalid due to insufficient margin after the update, but its current status is {_stopOrderTicket.Status}.");
90-
}
90+
// All updates have been enqueued and should be rejected one by one
91+
if (orderEvent.OrderId == _stopOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
92+
{
93+
throw new RegressionTestException($"The stop order update should have been rejected due to insufficient margin");
94+
}
9195

92-
// Check if limit order is invalid due to insufficient margin after update
93-
if (_limitOrderTicket != null && orderEvent.Id == _limitOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
94-
{
95-
throw new RegressionTestException($"Order {_limitOrderTicket.OrderId} with symbol {_limitOrderTicket.Symbol} should have been invalid due to insufficient margin after the update, but its current status is {_limitOrderTicket.Status}.");
96-
}
96+
if (orderEvent.Id == _limitOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
97+
{
98+
throw new RegressionTestException($"The limit order update should have been rejected due to insufficient margin");
99+
}
97100

98-
// Check if trailing stop order is invalid due to insufficient margin after update
99-
if (_trailingStopOrderTicket != null && orderEvent.Id == _trailingStopOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
100-
{
101-
throw new RegressionTestException($"Order {_trailingStopOrderTicket.OrderId} with symbol {_trailingStopOrderTicket.Symbol} should have been invalid due to insufficient margin after the update, but its current status is {_trailingStopOrderTicket.Status}.");
101+
if (orderEvent.Id == _trailingStopOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
102+
{
103+
throw new RegressionTestException($"The trailing stop order update should have been rejected due to insufficient margin");
104+
}
102105
}
103106
}
104107

105108
public override void OnEndOfAlgorithm()
106109
{
110+
// Updates were rejected, so all orders should be in Filled status
107111
var orders = Transactions.GetOrders().ToList();
112+
foreach (var order in orders)
113+
{
114+
if (order.Status != OrderStatus.Filled)
115+
{
116+
throw new RegressionTestException($"Order {order.Id} with symbol {order.Symbol} should have been filled, but its current status is {order.Status}.");
117+
}
118+
}
108119
}
109120

110121
/// <summary>
@@ -137,14 +148,14 @@ public override void OnEndOfAlgorithm()
137148
/// </summary>
138149
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
139150
{
140-
{"Total Orders", "5"},
151+
{"Total Orders", "4"},
141152
{"Average Win", "0%"},
142153
{"Average Loss", "0%"},
143154
{"Compounding Annual Return", "0%"},
144155
{"Drawdown", "0%"},
145156
{"Expectancy", "0"},
146157
{"Start Equity", "100000.00"},
147-
{"End Equity", "91982.00"},
158+
{"End Equity", "90809.64"},
148159
{"Net Profit", "0%"},
149160
{"Sharpe Ratio", "0"},
150161
{"Sortino Ratio", "0"},
@@ -160,10 +171,10 @@ public override void OnEndOfAlgorithm()
160171
{"Tracking Error", "0"},
161172
{"Treynor Ratio", "0"},
162173
{"Total Fees", "$0.00"},
163-
{"Estimated Strategy Capacity", "$250000.00"},
174+
{"Estimated Strategy Capacity", "$99000.00"},
164175
{"Lowest Capacity Asset", "EURUSD 8G"},
165-
{"Portfolio Turnover", "3074.60%"},
166-
{"OrderListHash", "39c85ecfd3308fd77636d56af3a094e9"}
176+
{"Portfolio Turnover", "6777.62%"},
177+
{"OrderListHash", "505feaf1ae70ead2d7ab78ea257d7342"}
167178
};
168179
}
169180
}

Engine/TransactionHandlers/BrokerageTransactionHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,9 @@ private OrderResponse HandleUpdateOrderRequest(UpdateOrderRequest request)
943943
// If the order is not part of a ComboLegLimit update, validate sufficient buying power
944944
if (order.GroupOrderManager == null)
945945
{
946-
var validationResult = ValidateSufficientBuyingPowerForOrders(order, request);
946+
var updatedOrder = order.Clone();
947+
updatedOrder.ApplyUpdateOrderRequest(request);
948+
var validationResult = ValidateSufficientBuyingPowerForOrders(updatedOrder, request);
947949
if (validationResult != null)
948950
{
949951
return validationResult;

0 commit comments

Comments
 (0)