forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderTicket.cs
615 lines (549 loc) · 22.5 KB
/
OrderTicket.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using QuantConnect.Securities;
using static QuantConnect.StringExtensions;
namespace QuantConnect.Orders
{
/// <summary>
/// Provides a single reference to an order for the algorithm to maintain. As the order gets
/// updated this ticket will also get updated
/// </summary>
public sealed class OrderTicket
{
private readonly object _orderEventsLock = new object();
private readonly object _updateRequestsLock = new object();
private readonly object _cancelRequestLock = new object();
private Order _order;
private OrderStatus? _orderStatusOverride;
private CancelOrderRequest _cancelRequest;
private decimal _quantityFilled;
private decimal _averageFillPrice;
private readonly int _orderId;
private readonly List<OrderEvent> _orderEvents;
private readonly SubmitOrderRequest _submitRequest;
private readonly ManualResetEvent _orderStatusClosedEvent;
private readonly List<UpdateOrderRequest> _updateRequests;
private readonly ManualResetEvent _orderSetEvent;
// we pull this in to provide some behavior/simplicity to the ticket API
private readonly SecurityTransactionManager _transactionManager;
/// <summary>
/// Gets the order id of this ticket
/// </summary>
public int OrderId
{
get { return _orderId; }
}
/// <summary>
/// Gets the current status of this order ticket
/// </summary>
public OrderStatus Status
{
get
{
if (_orderStatusOverride.HasValue) return _orderStatusOverride.Value;
return _order == null ? OrderStatus.New : _order.Status;
}
}
/// <summary>
/// Gets the symbol being ordered
/// </summary>
public Symbol Symbol
{
get { return _submitRequest.Symbol; }
}
/// <summary>
/// Gets the <see cref="Symbol"/>'s <see cref="SecurityType"/>
/// </summary>
public SecurityType SecurityType
{
get { return _submitRequest.SecurityType; }
}
/// <summary>
/// Gets the number of units ordered
/// </summary>
public decimal Quantity
{
get { return _order == null ? _submitRequest.Quantity : _order.Quantity; }
}
/// <summary>
/// Gets the average fill price for this ticket. If no fills have been processed
/// then this will return a value of zero.
/// </summary>
public decimal AverageFillPrice
{
get { return _averageFillPrice; }
}
/// <summary>
/// Gets the total qantity filled for this ticket. If no fills have been processed
/// then this will return a value of zero.
/// </summary>
public decimal QuantityFilled
{
get { return _quantityFilled; }
}
/// <summary>
/// Gets the time this order was last updated
/// </summary>
public DateTime Time
{
get { return GetMostRecentOrderRequest().Time; }
}
/// <summary>
/// Gets the type of order
/// </summary>
public OrderType OrderType
{
get { return _submitRequest.OrderType; }
}
/// <summary>
/// Gets the order's current tag
/// </summary>
public string Tag
{
get { return _order == null ? _submitRequest.Tag : _order.Tag; }
}
/// <summary>
/// Gets the <see cref="SubmitOrderRequest"/> that initiated this order
/// </summary>
public SubmitOrderRequest SubmitRequest
{
get { return _submitRequest; }
}
/// <summary>
/// Gets a list of <see cref="UpdateOrderRequest"/> containing an item for each
/// <see cref="UpdateOrderRequest"/> that was sent for this order id
/// </summary>
public IReadOnlyList<UpdateOrderRequest> UpdateRequests
{
get
{
lock (_updateRequestsLock)
{
return _updateRequests.ToList();
}
}
}
/// <summary>
/// Gets the <see cref="CancelOrderRequest"/> if this order was canceled. If this order
/// was not canceled, this will return null
/// </summary>
public CancelOrderRequest CancelRequest
{
get
{
lock (_cancelRequestLock)
{
return _cancelRequest;
}
}
}
/// <summary>
/// Gets a list of all order events for this ticket
/// </summary>
public IReadOnlyList<OrderEvent> OrderEvents
{
get
{
lock (_orderEventsLock)
{
return _orderEvents.ToList();
}
}
}
/// <summary>
/// Gets a wait handle that can be used to wait until this order has filled
/// </summary>
public WaitHandle OrderClosed
{
get { return _orderStatusClosedEvent; }
}
/// <summary>
/// Returns true if the order has been set for this ticket
/// </summary>
public bool HasOrder => _order != null;
/// <summary>
/// Gets a wait handle that can be used to wait until the order has been set
/// </summary>
public WaitHandle OrderSet => _orderSetEvent;
/// <summary>
/// Initializes a new instance of the <see cref="OrderTicket"/> class
/// </summary>
/// <param name="transactionManager">The transaction manager used for submitting updates and cancels for this ticket</param>
/// <param name="submitRequest">The order request that initiated this order ticket</param>
public OrderTicket(SecurityTransactionManager transactionManager, SubmitOrderRequest submitRequest)
{
_submitRequest = submitRequest;
_orderId = submitRequest.OrderId;
_transactionManager = transactionManager;
_orderEvents = new List<OrderEvent>();
_updateRequests = new List<UpdateOrderRequest>();
_orderStatusClosedEvent = new ManualResetEvent(false);
_orderSetEvent = new ManualResetEvent(false);
}
/// <summary>
/// Gets the specified field from the ticket
/// </summary>
/// <param name="field">The order field to get</param>
/// <returns>The value of the field</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public decimal Get(OrderField field)
{
switch (field)
{
case OrderField.LimitPrice:
if (_submitRequest.OrderType == OrderType.Limit)
{
return AccessOrder<LimitOrder>(this, field, o => o.LimitPrice, r => r.LimitPrice);
}
if (_submitRequest.OrderType == OrderType.StopLimit)
{
return AccessOrder<StopLimitOrder>(this, field, o => o.LimitPrice, r => r.LimitPrice);
}
break;
case OrderField.StopPrice:
if (_submitRequest.OrderType == OrderType.StopLimit)
{
return AccessOrder<StopLimitOrder>(this, field, o => o.StopPrice, r => r.StopPrice);
}
if (_submitRequest.OrderType == OrderType.StopMarket)
{
return AccessOrder<StopMarketOrder>(this, field, o => o.StopPrice, r => r.StopPrice);
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(field), field, null);
}
throw new ArgumentException(Invariant($"Unable to get field {field} on order of type {_submitRequest.OrderType}"));
}
/// <summary>
/// Submits an <see cref="UpdateOrderRequest"/> with the <see cref="SecurityTransactionManager"/> to update
/// the ticket with data specified in <paramref name="fields"/>
/// </summary>
/// <param name="fields">Defines what properties of the order should be updated</param>
/// <returns>The <see cref="OrderResponse"/> from updating the order</returns>
public OrderResponse Update(UpdateOrderFields fields)
{
_transactionManager.UpdateOrder(new UpdateOrderRequest(_transactionManager.UtcTime, SubmitRequest.OrderId, fields));
return _updateRequests.Last().Response;
}
/// <summary>
/// Submits an <see cref="UpdateOrderRequest"/> with the <see cref="SecurityTransactionManager"/> to update
/// the ticket with tag specified in <paramref name="tag"/>
/// </summary>
/// <param name="tag"></param>
/// <returns><see cref="OrderResponse"/> from updating the order</returns>
public OrderResponse UpdateTag(string tag)
{
var fields = new UpdateOrderFields()
{
Tag = tag
};
return Update(fields);
}
/// <summary>
/// Submits an <see cref="UpdateOrderRequest"/> with the <see cref="SecurityTransactionManager"/> to update
/// the ticket with quantity specified in <paramref name="quantity"/> and with tag specified in <paramref name="quantity"/>
/// </summary>
/// <param name="quantity"></param>
/// <param name="tag"></param>
/// <returns><see cref="OrderResponse"/> from updating the order</returns>
public OrderResponse UpdateQuantity(decimal quantity, string tag = null)
{
var fields = new UpdateOrderFields()
{
Quantity = quantity,
Tag = tag
};
return Update(fields);
}
/// <summary>
/// Submits an <see cref="UpdateOrderRequest"/> with the <see cref="SecurityTransactionManager"/> to update
/// the ticker with limitprice specified in <paramref name="limitPrice"/> and with tag specified in <paramref name="quantity"/>
/// </summary>
/// <param name="limitPrice"></param>
/// <param name="tag"></param>
/// <returns><see cref="OrderResponse"/> from updating the order</returns>
public OrderResponse UpdateLimitPrice(decimal limitPrice, string tag = null)
{
var fields = new UpdateOrderFields()
{
LimitPrice = limitPrice,
Tag = tag
};
return Update(fields);
}
/// <summary>
/// Submits an <see cref="UpdateOrderRequest"/> with the <see cref="SecurityTransactionManager"/> to update
/// the ticker with stopprice specified in <paramref name="stopPrice"/> and with tag specified in <paramref name="quantity"/>
/// </summary>
/// <param name="stopPrice"></param>
/// <param name="tag"></param>
/// <returns><see cref="OrderResponse"/> from updating the order</returns>
public OrderResponse UpdateStopPrice(decimal stopPrice, string tag = null)
{
var fields = new UpdateOrderFields()
{
StopPrice = stopPrice,
Tag = tag
};
return Update(fields);
}
/// <summary>
/// Submits a new request to cancel this order
/// </summary>
public OrderResponse Cancel(string tag = null)
{
var request = new CancelOrderRequest(_transactionManager.UtcTime, OrderId, tag);
lock (_cancelRequestLock)
{
// don't submit duplicate cancel requests
if (_cancelRequest != null)
{
return OrderResponse.Error(request, OrderResponseErrorCode.RequestCanceled,
Invariant($"Order {OrderId} has already received a cancellation request.")
);
}
}
_transactionManager.ProcessRequest(request);
lock (_cancelRequestLock)
{
if (_cancelRequest != null)
{
return _cancelRequest.Response;
}
}
throw new ArgumentException("CancelRequest is null.");
}
/// <summary>
/// Gets the most recent <see cref="OrderResponse"/> for this ticket
/// </summary>
/// <returns>The most recent <see cref="OrderResponse"/> for this ticket</returns>
public OrderResponse GetMostRecentOrderResponse()
{
return GetMostRecentOrderRequest().Response;
}
/// <summary>
/// Gets the most recent <see cref="OrderRequest"/> for this ticket
/// </summary>
/// <returns>The most recent <see cref="OrderRequest"/> for this ticket</returns>
public OrderRequest GetMostRecentOrderRequest()
{
lock (_cancelRequestLock)
{
if (_cancelRequest != null)
{
return _cancelRequest;
}
}
var lastUpdate = _updateRequests.LastOrDefault();
if (lastUpdate != null)
{
return lastUpdate;
}
return SubmitRequest;
}
/// <summary>
/// Adds an order event to this ticket
/// </summary>
/// <param name="orderEvent">The order event to be added</param>
internal void AddOrderEvent(OrderEvent orderEvent)
{
lock (_orderEventsLock)
{
_orderEvents.Add(orderEvent);
if (orderEvent.FillQuantity != 0)
{
// keep running totals of quantity filled and the average fill price so we
// don't need to compute these on demand
_quantityFilled += orderEvent.FillQuantity;
var quantityWeightedFillPrice = _orderEvents.Where(x => x.Status.IsFill())
.Aggregate(0m, (d, x) => d + x.AbsoluteFillQuantity*x.FillPrice);
_averageFillPrice = quantityWeightedFillPrice/Math.Abs(_quantityFilled);
}
}
// fire the wait handle indicating this order is closed
if (orderEvent.Status.IsClosed())
{
_orderStatusClosedEvent.Set();
}
}
/// <summary>
/// Updates the internal order object with the current state
/// </summary>
/// <param name="order">The order</param>
internal void SetOrder(Order order)
{
if (_order != null && _order.Id != order.Id)
{
throw new ArgumentException("Order id mismatch");
}
_order = order;
_orderSetEvent.Set();
}
/// <summary>
/// Adds a new <see cref="UpdateOrderRequest"/> to this ticket.
/// </summary>
/// <param name="request">The recently processed <see cref="UpdateOrderRequest"/></param>
internal void AddUpdateRequest(UpdateOrderRequest request)
{
if (request.OrderId != OrderId)
{
throw new ArgumentException("Received UpdateOrderRequest for incorrect order id.");
}
lock (_updateRequestsLock)
{
_updateRequests.Add(request);
}
}
/// <summary>
/// Sets the <see cref="CancelOrderRequest"/> for this ticket. This can only be performed once.
/// </summary>
/// <remarks>
/// This method is thread safe.
/// </remarks>
/// <param name="request">The <see cref="CancelOrderRequest"/> that canceled this ticket.</param>
/// <returns>False if the the CancelRequest has already been set, true if this call set it</returns>
internal bool TrySetCancelRequest(CancelOrderRequest request)
{
if (request.OrderId != OrderId)
{
throw new ArgumentException("Received CancelOrderRequest for incorrect order id.");
}
lock (_cancelRequestLock)
{
if (_cancelRequest != null)
{
return false;
}
_cancelRequest = request;
}
return true;
}
/// <summary>
/// Creates a new <see cref="OrderTicket"/> that represents trying to cancel an order for which no ticket exists
/// </summary>
public static OrderTicket InvalidCancelOrderId(SecurityTransactionManager transactionManager, CancelOrderRequest request)
{
var submit = new SubmitOrderRequest(OrderType.Market, SecurityType.Base, Symbol.Empty, 0, 0, 0, DateTime.MaxValue, request.Tag);
submit.SetResponse(OrderResponse.UnableToFindOrder(request));
submit.SetOrderId(request.OrderId);
var ticket = new OrderTicket(transactionManager, submit);
request.SetResponse(OrderResponse.UnableToFindOrder(request));
ticket.TrySetCancelRequest(request);
ticket._orderStatusOverride = OrderStatus.Invalid;
return ticket;
}
/// <summary>
/// Creates a new <see cref="OrderTicket"/> that represents trying to update an order for which no ticket exists
/// </summary>
public static OrderTicket InvalidUpdateOrderId(SecurityTransactionManager transactionManager, UpdateOrderRequest request)
{
var submit = new SubmitOrderRequest(OrderType.Market, SecurityType.Base, Symbol.Empty, 0, 0, 0, DateTime.MaxValue, request.Tag);
submit.SetResponse(OrderResponse.UnableToFindOrder(request));
submit.SetOrderId(request.OrderId);
var ticket = new OrderTicket(transactionManager, submit);
request.SetResponse(OrderResponse.UnableToFindOrder(request));
ticket.AddUpdateRequest(request);
ticket._orderStatusOverride = OrderStatus.Invalid;
return ticket;
}
/// <summary>
/// Creates a new <see cref="OrderTicket"/> that represents trying to submit a new order that had errors embodied in the <paramref name="response"/>
/// </summary>
public static OrderTicket InvalidSubmitRequest(SecurityTransactionManager transactionManager, SubmitOrderRequest request, OrderResponse response)
{
request.SetResponse(response);
return new OrderTicket(transactionManager, request) { _orderStatusOverride = OrderStatus.Invalid };
}
/// <summary>
/// Creates a new <see cref="OrderTicket"/> that is invalidated because the algorithm was in the middle of warm up still
/// </summary>
public static OrderTicket InvalidWarmingUp(SecurityTransactionManager transactionManager, SubmitOrderRequest submit)
{
submit.SetResponse(OrderResponse.WarmingUp(submit));
var ticket = new OrderTicket(transactionManager, submit);
ticket._orderStatusOverride = OrderStatus.Invalid;
return ticket;
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
var counts = Invariant($"Request Count: {RequestCount()} Response Count: {ResponseCount()}");
if (_order != null)
{
return Invariant($"{OrderId}: {_order} {counts}");
}
return Invariant($"{OrderId}: {counts}");
}
private int ResponseCount()
{
var count = (_submitRequest.Response == OrderResponse.Unprocessed ? 0 : 1) +
_updateRequests.Count(x => x.Response != OrderResponse.Unprocessed);
lock (_cancelRequestLock)
{
count += _cancelRequest == null || _cancelRequest.Response == OrderResponse.Unprocessed ? 0 : 1;
}
return count;
}
private int RequestCount()
{
var count = 1 + _updateRequests.Count;
lock (_cancelRequestLock)
{
count += _cancelRequest == null ? 0 : 1;
}
return count;
}
/// <summary>
/// This is provided for API backward compatibility and will resolve to the order ID, except during
/// an error, where it will return the integer value of the <see cref="OrderResponseErrorCode"/> from
/// the most recent response
/// </summary>
public static implicit operator int(OrderTicket ticket)
{
var response = ticket.GetMostRecentOrderResponse();
if (response != null && response.IsError)
{
return (int) response.ErrorCode;
}
return ticket.OrderId;
}
private static decimal AccessOrder<T>(OrderTicket ticket, OrderField field, Func<T, decimal> orderSelector, Func<SubmitOrderRequest, decimal> requestSelector)
where T : Order
{
var order = ticket._order;
if (order == null)
{
return requestSelector(ticket._submitRequest);
}
var typedOrder = order as T;
if (typedOrder != null)
{
return orderSelector(typedOrder);
}
throw new ArgumentException(Invariant($"Unable to access property {field} on order of type {order.Type}"));
}
}
}