16 using System.Collections.Generic;
18 using System.Runtime.CompilerServices;
30 public static partial class Messages
37 [MethodImpl(MethodImplOptions.AggressiveInlining)]
38 public static string ToString(Orders.CancelOrderRequest request)
40 return Invariant($
@"{request.Time.ToStringInvariant()} UTC: Cancel Order: ({request.OrderId}) - {
41 request.Tag} Status: {request.Status}");
50 [MethodImpl(MethodImplOptions.AggressiveInlining)]
51 public static string InsufficientBuyingPowerForOrders(Dictionary<Orders.Order, Securities.Security> securities,
54 var ids =
string.Join(
",", securities.Keys.Select(o => o.Id));
55 var values =
string.Join(
",", securities.Select(o => o.Key.GetValue(o.Value).SmartRounding()));
56 return $
@"Order Error: ids: [{ids}], Insufficient buying power to complete orders (Value:[{values}]), Reason: {
57 hasSufficientBuyingPowerResult.Reason}.";
66 [MethodImpl(MethodImplOptions.AggressiveInlining)]
67 public static string Tag(Orders.LimitIfTouchedOrder order)
73 [MethodImpl(MethodImplOptions.AggressiveInlining)]
74 public static string ToString(Orders.LimitIfTouchedOrder order)
77 return Invariant($
@"{Order.ToString(order)} at trigger {currencySymbol}{order.TriggerPrice.SmartRounding()
78 } limit {currencySymbol}{order.LimitPrice.SmartRounding()}");
87 [MethodImpl(MethodImplOptions.AggressiveInlining)]
88 public static string Tag(Orders.LimitOrder order)
94 [MethodImpl(MethodImplOptions.AggressiveInlining)]
95 public static string ToString(Orders.LimitOrder order)
98 return Invariant($
"{Order.ToString(order)} at limit {currencySymbol}{order.LimitPrice.SmartRounding()}");
107 [MethodImpl(MethodImplOptions.AggressiveInlining)]
108 public static string ToString(Orders.Order order)
110 var tag =
string.IsNullOrEmpty(order.Tag) ?
string.Empty : $
": {order.Tag}";
111 return Invariant($
@"OrderId: {order.Id} (BrokerId: {string.Join(",
", order.BrokerId)}) {order.Status} {
112 order.Type} order for {order.Quantity} unit{(order.Quantity == 1 ? "" : "s
")} of {order.Symbol}{tag}");
121 [MethodImpl(MethodImplOptions.AggressiveInlining)]
122 public static string ToString(Orders.OrderEvent orderEvent)
124 var message = Invariant($
@"Time: {orderEvent.UtcTime} OrderID: {orderEvent.OrderId} EventID: {
125 orderEvent.Id} Symbol: {orderEvent.Symbol.Value} Status: {orderEvent.Status} Quantity: {orderEvent.Quantity}");
128 if (orderEvent.FillQuantity != 0)
130 message += Invariant($
@" FillQuantity: {orderEvent.FillQuantity
131 } FillPrice: {currencySymbol}{orderEvent.FillPrice.SmartRounding()}");
134 if (orderEvent.LimitPrice.HasValue)
136 message += Invariant($
" LimitPrice: {currencySymbol}{orderEvent.LimitPrice.Value.SmartRounding()}");
139 if (orderEvent.StopPrice.HasValue)
141 message += Invariant($
" StopPrice: {currencySymbol}{orderEvent.StopPrice.Value.SmartRounding()}");
144 if (orderEvent.TrailingAmount.HasValue)
146 var trailingAmountString =
TrailingStopOrder.TrailingAmount(orderEvent.TrailingAmount.Value,
147 orderEvent.TrailingAsPercentage ??
false, currencySymbol);
148 message += $
" TrailingAmount: {trailingAmountString}";
151 if (orderEvent.TriggerPrice.HasValue)
153 message += Invariant($
" TriggerPrice: {currencySymbol}{orderEvent.TriggerPrice.Value.SmartRounding()}");
157 if (orderEvent.OrderFee.Value.Amount != 0m)
159 message += Invariant($
" OrderFee: {orderEvent.OrderFee}");
163 if (!
string.IsNullOrEmpty(orderEvent.Message))
165 message += Invariant($
" Message: {orderEvent.Message}");
168 if (orderEvent.Symbol.SecurityType.IsOption())
170 message += Invariant($
" IsAssignment: {orderEvent.IsAssignment}");
176 [MethodImpl(MethodImplOptions.AggressiveInlining)]
177 public static string ShortToString(Orders.OrderEvent orderEvent)
179 var message = Invariant($
"{orderEvent.UtcTime} OID:{orderEvent.OrderId} {orderEvent.Symbol.Value} {orderEvent.Status} Q:{orderEvent.Quantity}");
182 if (orderEvent.FillQuantity != 0)
184 message += Invariant($
" FQ:{orderEvent.FillQuantity} FP:{currencySymbol}{orderEvent.FillPrice.SmartRounding()}");
187 if (orderEvent.LimitPrice.HasValue)
189 message += Invariant($
" LP:{currencySymbol}{orderEvent.LimitPrice.Value.SmartRounding()}");
192 if (orderEvent.StopPrice.HasValue)
194 message += Invariant($
" SP:{currencySymbol}{orderEvent.StopPrice.Value.SmartRounding()}");
197 if (orderEvent.TrailingAmount.HasValue)
199 var trailingAmountString =
TrailingStopOrder.TrailingAmount(orderEvent.TrailingAmount.Value,
200 orderEvent.TrailingAsPercentage ??
false, currencySymbol);
201 message += $
" TA: {trailingAmountString}";
204 if (orderEvent.TriggerPrice.HasValue)
206 message += Invariant($
" TP:{currencySymbol}{orderEvent.TriggerPrice.Value.SmartRounding()}");
210 if (orderEvent.OrderFee.Value.Amount != 0m)
212 message += Invariant($
" OF:{currencySymbol}{orderEvent.OrderFee}");
216 if (!
string.IsNullOrEmpty(orderEvent.Message))
218 message += Invariant($
" M:{orderEvent.Message}");
221 if (orderEvent.Symbol.SecurityType.IsOption())
223 message += Invariant($
" IA:{orderEvent.IsAssignment}");
235 [MethodImpl(MethodImplOptions.AggressiveInlining)]
236 public static string ToString(Orders.OrderRequest request)
238 return Invariant($
"{request.Time} UTC: Order: ({request.OrderId}) - {request.Tag} Status: {request.Status}");
247 public static string DefaultErrorMessage =
"An unexpected error occurred.";
249 public static string UnprocessedOrderResponseErrorMessage =
"The request has not yet been processed.";
251 [MethodImpl(MethodImplOptions.AggressiveInlining)]
252 public static string ToString(Orders.OrderResponse response)
254 if (response == Orders.OrderResponse.Unprocessed)
256 return "Unprocessed";
259 if (response.IsError)
261 return Invariant($
"Error: {response.ErrorCode} - {response.ErrorMessage}");
267 [MethodImpl(MethodImplOptions.AggressiveInlining)]
268 public static string InvalidStatus(Orders.OrderRequest request, Orders.Order order)
270 return Invariant($
"Unable to update order with id {request.OrderId} because it already has {order.Status} status.");
273 [MethodImpl(MethodImplOptions.AggressiveInlining)]
274 public static string InvalidNewStatus(Orders.OrderRequest request, Orders.Order order)
276 return Invariant($
@"Unable to update or cancel order with id {
277 request.OrderId} and status {order.Status} because the submit confirmation has not been received yet.");
280 [MethodImpl(MethodImplOptions.AggressiveInlining)]
281 public static string UnableToFindOrder(Orders.OrderRequest request)
283 return Invariant($
"Unable to locate order with id {request.OrderId}.");
286 [MethodImpl(MethodImplOptions.AggressiveInlining)]
287 public static string ZeroQuantity(Orders.OrderRequest request)
289 return Invariant($
"Unable to {request.OrderRequestType.ToLower()} order with id {request.OrderId} that has zero quantity.");
292 [MethodImpl(MethodImplOptions.AggressiveInlining)]
293 public static string MissingSecurity(Orders.SubmitOrderRequest request)
295 return Invariant($
"You haven't requested {request.Symbol} data. Add this with AddSecurity() in the Initialize() Method.");
298 [MethodImpl(MethodImplOptions.AggressiveInlining)]
299 public static string WarmingUp(Orders.OrderRequest request)
301 return Invariant($
@"This operation is not allowed in Initialize or during warm up: OrderRequest.{
302 request.OrderRequestType}. Please move this code to the OnWarmupFinished() method.");
311 [MethodImpl(MethodImplOptions.AggressiveInlining)]
312 public static string GetFieldError(Orders.OrderTicket ticket,
OrderField field)
314 return Invariant($
"Unable to get field {field} on order of type {ticket.SubmitRequest.OrderType}");
317 [MethodImpl(MethodImplOptions.AggressiveInlining)]
318 public static string CancelRequestAlreadySubmitted(Orders.OrderTicket ticket)
320 return Invariant($
"Order {ticket.OrderId} has already received a cancellation request.");
323 [MethodImpl(MethodImplOptions.AggressiveInlining)]
324 public static string ToString(Orders.OrderTicket ticket, Orders.Order order,
int requestCount,
int responseCount)
326 var counts = Invariant($
"Request Count: {requestCount} Response Count: {responseCount}");
329 return Invariant($
"{ticket.OrderId}: {order} {counts}");
332 return Invariant($
"{ticket.OrderId}: {counts}");
341 [MethodImpl(MethodImplOptions.AggressiveInlining)]
342 public static string Tag(Orders.StopLimitOrder order)
348 [MethodImpl(MethodImplOptions.AggressiveInlining)]
349 public static string ToString(Orders.StopLimitOrder order)
352 return Invariant($
@"{Order.ToString(order)} at stop {currencySymbol}{order.StopPrice.SmartRounding()
353 } limit {currencySymbol}{order.LimitPrice.SmartRounding()}");
362 [MethodImpl(MethodImplOptions.AggressiveInlining)]
363 public static string Tag(Orders.StopMarketOrder order)
369 [MethodImpl(MethodImplOptions.AggressiveInlining)]
370 public static string ToString(Orders.StopMarketOrder order)
373 return Invariant($
"{Order.ToString(order)} at stop {currencySymbol}{order.StopPrice.SmartRounding()}");
382 [MethodImpl(MethodImplOptions.AggressiveInlining)]
383 public static string Tag(Orders.TrailingStopOrder order)
385 return Invariant($
"Trailing Amount: {TrailingAmount(order)}");
388 [MethodImpl(MethodImplOptions.AggressiveInlining)]
389 public static string ToString(Orders.TrailingStopOrder order)
392 return Invariant($
@"{Order.ToString(order)} at stop {currencySymbol}{order.StopPrice.SmartRounding()}. Trailing amount: {
393 TrailingAmountImpl(order, currencySymbol)}");
396 [MethodImpl(MethodImplOptions.AggressiveInlining)]
397 public static string TrailingAmount(Orders.TrailingStopOrder order)
402 [MethodImpl(MethodImplOptions.AggressiveInlining)]
403 public static string TrailingAmount(decimal trailingAmount,
bool trailingAsPercentage,
string priceCurrency)
405 return trailingAsPercentage ? Invariant($
"{trailingAmount * 100}%") : Invariant($
"{priceCurrency}{trailingAmount}");
408 [MethodImpl(MethodImplOptions.AggressiveInlining)]
409 private static string TrailingAmountImpl(Orders.TrailingStopOrder order,
string currencySymbol)
411 return TrailingAmount(order.TrailingAmount, order.TrailingAsPercentage, currencySymbol);
420 [MethodImpl(MethodImplOptions.AggressiveInlining)]
421 public static string ToString(Orders.SubmitOrderRequest request)
424 var proxy = Orders.Order.CreateOrder(request);
425 return Invariant($
"{request.Time} UTC: Submit Order: ({request.OrderId}) - {proxy} {request.Tag} Status: {request.Status}");
434 [MethodImpl(MethodImplOptions.AggressiveInlining)]
435 public static string ToString(Orders.UpdateOrderRequest request)
437 var updates =
new List<string>(4);
438 if (request.Quantity.HasValue)
440 updates.Add(Invariant($
"Quantity: {request.Quantity.Value}"));
442 if (request.LimitPrice.HasValue)
444 updates.Add(Invariant($
"LimitPrice: {request.LimitPrice.Value.SmartRounding()}"));
446 if (request.StopPrice.HasValue)
448 updates.Add(Invariant($
"StopPrice: {request.StopPrice.Value.SmartRounding()}"));
450 if (request.TrailingAmount.HasValue)
452 updates.Add(Invariant($
"TrailingAmount: {request.TrailingAmount.Value.SmartRounding()}"));
454 if (request.TriggerPrice.HasValue)
456 updates.Add(Invariant($
"TriggerPrice: {request.TriggerPrice.Value.SmartRounding()}"));
459 return Invariant($
@"{request.Time} UTC: Update Order: ({request.OrderId}) - {string.Join(",
", updates)} {
460 request.Tag} Status: {request.Status}");