-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathFillModel.cs
More file actions
1187 lines (1035 loc) · 52.5 KB
/
Copy pathFillModel.cs
File metadata and controls
1187 lines (1035 loc) · 52.5 KB
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 QuantConnect.Data.Market;
using QuantConnect.Python;
using QuantConnect.Orders.Fees;
using QuantConnect.Securities;
using QuantConnect.Util;
namespace QuantConnect.Orders.Fills
{
/// <summary>
/// Provides a base class for all fill models
/// </summary>
public class FillModel : IFillModel
{
/// <summary>
/// The parameters instance to be used by the different XxxxFill() implementations
/// </summary>
protected FillModelParameters Parameters { get; set; }
/// <summary>
/// This is required due to a limitation in PythonNet to resolved overriden methods.
/// When Python calls a C# method that calls a method that's overriden in python it won't
/// run the python implementation unless the call is performed through python too.
/// </summary>
protected FillModelPythonWrapper PythonWrapper { get; set; }
/// <summary>
/// Used to set the <see cref="FillModelPythonWrapper"/> instance if any
/// </summary>
public void SetPythonWrapper(FillModelPythonWrapper pythonWrapper)
{
PythonWrapper = pythonWrapper;
}
/// <summary>
/// Return an order event with the fill details
/// </summary>
/// <param name="parameters">A <see cref="FillModelParameters"/> object containing the security and order</param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
public virtual Fill Fill(FillModelParameters parameters)
{
// Important: setting the parameters is required because it is
// consumed by the different XxxxFill() implementations
Parameters = parameters;
var orderEvents = new List<OrderEvent>(1);
switch (parameters.Order.Type)
{
case OrderType.Market:
orderEvents.Add(PythonWrapper != null
? PythonWrapper.MarketFill(parameters.Security, parameters.Order as MarketOrder)
: MarketFill(parameters.Security, parameters.Order as MarketOrder));
break;
case OrderType.Limit:
orderEvents.Add(PythonWrapper != null
? PythonWrapper.LimitFill(parameters.Security, parameters.Order as LimitOrder)
: LimitFill(parameters.Security, parameters.Order as LimitOrder));
break;
case OrderType.LimitIfTouched:
orderEvents.Add(PythonWrapper != null
? PythonWrapper.LimitIfTouchedFill(parameters.Security, parameters.Order as LimitIfTouchedOrder)
: LimitIfTouchedFill(parameters.Security, parameters.Order as LimitIfTouchedOrder));
break;
case OrderType.StopMarket:
orderEvents.Add(PythonWrapper != null
? PythonWrapper.StopMarketFill(parameters.Security, parameters.Order as StopMarketOrder)
: StopMarketFill(parameters.Security, parameters.Order as StopMarketOrder));
break;
case OrderType.TrailingStop:
orderEvents.Add(PythonWrapper != null
? PythonWrapper.TrailingStopFill(parameters.Security, parameters.Order as TrailingStopOrder)
: TrailingStopFill(parameters.Security, parameters.Order as TrailingStopOrder));
break;
case OrderType.StopLimit:
orderEvents.Add(PythonWrapper != null
? PythonWrapper.StopLimitFill(parameters.Security, parameters.Order as StopLimitOrder)
: StopLimitFill(parameters.Security, parameters.Order as StopLimitOrder));
break;
case OrderType.MarketOnOpen:
orderEvents.Add(PythonWrapper != null
? PythonWrapper.MarketOnOpenFill(parameters.Security, parameters.Order as MarketOnOpenOrder)
: MarketOnOpenFill(parameters.Security, parameters.Order as MarketOnOpenOrder));
break;
case OrderType.MarketOnClose:
orderEvents.Add(PythonWrapper != null
? PythonWrapper.MarketOnCloseFill(parameters.Security, parameters.Order as MarketOnCloseOrder)
: MarketOnCloseFill(parameters.Security, parameters.Order as MarketOnCloseOrder));
break;
case OrderType.ComboMarket:
orderEvents = PythonWrapper != null
? PythonWrapper.ComboMarketFill(parameters.Order, parameters)
: ComboMarketFill(parameters.Order, parameters);
break;
case OrderType.ComboLimit:
orderEvents = PythonWrapper != null
? PythonWrapper.ComboLimitFill(parameters.Order, parameters)
: ComboLimitFill(parameters.Order, parameters);
break;
case OrderType.ComboLegLimit:
orderEvents = PythonWrapper != null
? PythonWrapper.ComboLegLimitFill(parameters.Order, parameters)
: ComboLegLimitFill(parameters.Order, parameters);
break;
default:
throw new ArgumentOutOfRangeException();
}
return new Fill(orderEvents);
}
/// <summary>
/// Default combo market fill model for the base security class. Fills at the last traded price for each leg.
/// </summary>
/// <param name="order">Order to fill</param>
/// <param name="parameters">Fill parameters for the order</param>
/// <returns>Order fill information detailing the average price and quantity filled for each leg. If any of the fills fails, none of the orders will be filled and the returned list will be empty</returns>
public virtual List<OrderEvent> ComboMarketFill(Order order, FillModelParameters parameters)
{
var fills = new List<OrderEvent>(parameters.SecuritiesForOrders.Count);
foreach (var kvp in parameters.SecuritiesForOrders.OrderBy(x => x.Key.Id))
{
var targetOrder = kvp.Key;
var security = kvp.Value;
var fill = InternalMarketFill(security, targetOrder, targetOrder.Quantity);
if (fill.Status != OrderStatus.Filled)
{
return new List<OrderEvent>();
}
fills.Add(fill);
}
return fills;
}
/// <summary>
/// Default combo limit fill model for the base security class. Fills at the sum of prices for the assets of every leg.
/// </summary>
/// <param name="order">Order to fill</param>
/// <param name="parameters">Fill parameters for the order</param>
/// <returns>Order fill information detailing the average price and quantity filled for each leg. If any of the fills fails, none of the orders will be filled and the returned list will be empty</returns>
public virtual List<OrderEvent> ComboLimitFill(Order order, FillModelParameters parameters)
{
// aggregate the prices from all the securities
var fillParameters = new List<ComboLimitOrderLegParameters>(parameters.SecuritiesForOrders.Count);
foreach (var kvp in parameters.SecuritiesForOrders.OrderBy(x => x.Key.Id))
{
var targetOrder = kvp.Key;
var security = kvp.Value;
var prices = GetPricesCheckingPythonWrapper(security, targetOrder.Direction);
if (prices.EndTime.ConvertToUtc(security.Exchange.TimeZone) < targetOrder.Time)
{
// do not fill on stale data
return new List<OrderEvent>();
}
fillParameters.Add(new ComboLimitOrderLegParameters
{
Security = security,
Order = targetOrder,
Prices = prices
});
}
var currentPrice = fillParameters.Aggregate(0m, (accumulatedPrice, p) => accumulatedPrice + p.Price);
var limitPrice = order.GroupOrderManager.LimitPrice;
var fills = new List<OrderEvent>(fillParameters.Count);
switch (order.GroupOrderManager.Direction)
{
case OrderDirection.Buy:
//Buy limit seeks lowest price
if (currentPrice < limitPrice)
{
for (var i = 0; i < fillParameters.Count; i++)
{
var targetParameters = fillParameters[i];
var utcTime = targetParameters.Security.LocalTime.ConvertToUtc(targetParameters.Security.Exchange.TimeZone);
var fill = new OrderEvent(targetParameters.Order, utcTime, OrderFee.Zero);
//Set order fill:
fill.Status = OrderStatus.Filled;
fill.FillPrice = targetParameters.Prices.Low;
// assume the order completely filled
fill.FillQuantity = targetParameters.Order.Quantity;
fills.Add(fill);
}
}
break;
case OrderDirection.Sell:
//Sell limit seeks highest price possible
if (currentPrice > limitPrice)
{
for (var i = 0; i < fillParameters.Count; i++)
{
var targetParameters = fillParameters[i];
var utcTime = targetParameters.Security.LocalTime.ConvertToUtc(targetParameters.Security.Exchange.TimeZone);
var fill = new OrderEvent(targetParameters.Order, utcTime, OrderFee.Zero);
//Set order fill:
fill.Status = OrderStatus.Filled;
fill.FillPrice = targetParameters.Prices.High;
// assume the order completely filled
fill.FillQuantity = targetParameters.Order.Quantity;
fills.Add(fill);
}
}
break;
}
return fills;
}
/// <summary>
/// Default combo limit fill model for the base security class. Fills at the limit price for each leg
/// </summary>
/// <param name="order">Order to fill</param>
/// <param name="parameters">Fill parameters for the order</param>
/// <returns>Order fill information detailing the average price and quantity filled for each leg. If any of the fills fails, none of the orders will be filled and the returned list will be empty</returns>
public virtual List<OrderEvent> ComboLegLimitFill(Order order, FillModelParameters parameters)
{
var fills = new List<OrderEvent>(order.GroupOrderManager.OrderIds.Count);
foreach (var kvp in parameters.SecuritiesForOrders.OrderBy(x => x.Key.Id))
{
var targetOrder = kvp.Key;
var security = kvp.Value;
var fill = InternalLimitFill(security, targetOrder, (targetOrder as ComboLegLimitOrder).LimitPrice,
targetOrder.Quantity);
if (fill.Status != OrderStatus.Filled)
{
return new List<OrderEvent>();
}
fills.Add(fill);
}
return fills;
}
/// <summary>
/// Default market fill model for the base security class. Fills at the last traded price.
/// </summary>
/// <param name="asset">Security asset we're filling</param>
/// <param name="order">Order packet to model</param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
public virtual OrderEvent MarketFill(Security asset, MarketOrder order)
{
return InternalMarketFill(asset, order, order.Quantity);
}
/// <summary>
/// Default market fill model for the base security class. Fills at the last traded price.
/// </summary>
private OrderEvent InternalMarketFill(Security asset, Order order, decimal quantity)
{
//Default order event to return.
var utcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);
var fill = new OrderEvent(order, utcTime, OrderFee.Zero);
if (order.Status == OrderStatus.Canceled) return fill;
// make sure the exchange is open/normal market hours before filling
if (!IsExchangeOpen(asset, order, false)) return fill;
var orderDirection = order.Direction;
var prices = GetPricesCheckingPythonWrapper(asset, orderDirection);
var pricesEndTimeUtc = prices.EndTime.ConvertToUtc(asset.Exchange.TimeZone);
// if the order is filled on stale (fill-forward) data, set a warning message on the order event
if (pricesEndTimeUtc.Add(Parameters.StalePriceTimeSpan) < order.Time)
{
fill.Message = Messages.FillModel.FilledAtStalePrice(asset, prices);
}
//Order [fill]price for a market order model is the current security price
fill.FillPrice = prices.Current;
fill.Status = OrderStatus.Filled;
//Calculate the model slippage: e.g. 0.01c
var slip = asset.SlippageModel.GetSlippageApproximation(asset, order);
//Apply slippage
switch (orderDirection)
{
case OrderDirection.Buy:
fill.FillPrice += slip;
break;
case OrderDirection.Sell:
fill.FillPrice -= slip;
break;
}
// assume the order completely filled
fill.FillQuantity = quantity;
return fill;
}
/// <summary>
/// Default stop fill model implementation in base class security. (Stop Market Order Type)
/// </summary>
/// <param name="asset">Security asset we're filling</param>
/// <param name="order">Order packet to model</param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
/// <seealso cref="MarketFill(Security, MarketOrder)"/>
public virtual OrderEvent StopMarketFill(Security asset, StopMarketOrder order)
{
//Default order event to return.
var utcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);
var fill = new OrderEvent(order, utcTime, OrderFee.Zero);
//If its cancelled don't need anymore checks:
if (order.Status == OrderStatus.Canceled) return fill;
// make sure the exchange is open/normal market hours before filling
if (!IsExchangeOpen(asset, order, false)) return fill;
//Get the range of prices in the last bar:
var prices = GetPricesCheckingPythonWrapper(asset, order.Direction);
var pricesEndTime = prices.EndTime.ConvertToUtc(asset.Exchange.TimeZone);
// do not fill on stale data
if (pricesEndTime <= order.Time) return fill;
//Calculate the model slippage: e.g. 0.01c
var slip = asset.SlippageModel.GetSlippageApproximation(asset, order);
//Check if the Stop Order was filled: opposite to a limit order
switch (order.Direction)
{
case OrderDirection.Sell:
//-> 1.1 Sell Stop: If Price below setpoint, Sell:
if (prices.Low < order.StopPrice)
{
fill.Status = OrderStatus.Filled;
// Assuming worse case scenario fill - fill at lowest of the stop & asset price.
fill.FillPrice = Math.Min(order.StopPrice, prices.Current - slip);
// assume the order completely filled
fill.FillQuantity = order.Quantity;
}
break;
case OrderDirection.Buy:
//-> 1.2 Buy Stop: If Price Above Setpoint, Buy:
if (prices.High > order.StopPrice)
{
fill.Status = OrderStatus.Filled;
// Assuming worse case scenario fill - fill at highest of the stop & asset price.
fill.FillPrice = Math.Max(order.StopPrice, prices.Current + slip);
// assume the order completely filled
fill.FillQuantity = order.Quantity;
}
break;
}
return fill;
}
/// <summary>
/// Default trailing stop fill model implementation in base class security. (Trailing Stop Order Type)
/// </summary>
/// <param name="asset">Security asset we're filling</param>
/// <param name="order">Order packet to model</param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
public virtual OrderEvent TrailingStopFill(Security asset, TrailingStopOrder order)
{
// Default order event to return.
var utcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);
var fill = new OrderEvent(order, utcTime, OrderFee.Zero);
// If its canceled don't need anymore checks:
if (order.Status == OrderStatus.Canceled) return fill;
// Make sure the exchange is open/normal market hours before filling
if (!IsExchangeOpen(asset, order, false)) return fill;
// Get the range of prices in the last bar:
var prices = GetPricesCheckingPythonWrapper(asset, order.Direction);
var pricesEndTime = prices.EndTime.ConvertToUtc(asset.Exchange.TimeZone);
// Do not fill on stale data
if (pricesEndTime <= order.Time) return fill;
// Calculate the model slippage: e.g. 0.01c
var slip = asset.SlippageModel.GetSlippageApproximation(asset, order);
switch (order.Direction)
{
case OrderDirection.Sell:
// Fill sell if market price drops below stop price
if (prices.Low <= order.StopPrice)
{
fill.Status = OrderStatus.Filled;
// Assuming worse case scenario fill - fill at lowest of the stop & asset price.
fill.FillPrice = Math.Min(order.StopPrice, prices.Current - slip);
// assume the order completely filled
fill.FillQuantity = order.Quantity;
}
break;
case OrderDirection.Buy:
// Fill buy if market price rises above stop price
if (prices.High >= order.StopPrice)
{
fill.Status = OrderStatus.Filled;
// Assuming worse case scenario fill - fill at highest of the stop & asset price.
fill.FillPrice = Math.Max(order.StopPrice, prices.Current + slip);
// assume the order completely filled
fill.FillQuantity = order.Quantity;
}
break;
}
// Update the stop price:
// NOTE: Doing this after attempting to fill the order in the following cases:
// - Sell: if low < stop price, order is filled. If we were to update the stop price before and it is moved towards the high price
// placing the stop price above the low price, it will not trigger a fill.
// - Buy: if high > stop price, order is filled. If we were to update the stop price before and it is moved towards the low price
// placing the stop price below the high price, it will not trigger a fill.
if (fill.Status != OrderStatus.Filled &&
TrailingStopOrder.TryUpdateStopPrice(order.Direction == OrderDirection.Sell ? prices.High : prices.Low, order.StopPrice,
order.TrailingAmount, order.TrailingAsPercentage, order.Direction, out var updatedStopPrice))
{
order.StopPrice = updatedStopPrice;
Parameters.OnOrderUpdated(order);
}
return fill;
}
/// <summary>
/// Default stop limit fill model implementation in base class security. (Stop Limit Order Type)
/// </summary>
/// <param name="asset">Security asset we're filling</param>
/// <param name="order">Order packet to model</param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
/// <seealso cref="StopMarketFill(Security, StopMarketOrder)"/>
/// <remarks>
/// There is no good way to model limit orders with OHLC because we never know whether the market has
/// gapped past our fill price. We have to make the assumption of a fluid, high volume market.
///
/// Stop limit orders we also can't be sure of the order of the H - L values for the limit fill. The assumption
/// was made the limit fill will be done with closing price of the bar after the stop has been triggered..
/// </remarks>
public virtual OrderEvent StopLimitFill(Security asset, StopLimitOrder order)
{
//Default order event to return.
var utcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);
var fill = new OrderEvent(order, utcTime, OrderFee.Zero);
//If its cancelled don't need anymore checks:
if (order.Status == OrderStatus.Canceled) return fill;
// make sure the exchange is open before filling -- allow pre/post market fills to occur
if (!IsExchangeOpen(asset, order))
{
return fill;
}
//Get the range of prices in the last bar:
var prices = GetPricesCheckingPythonWrapper(asset, order.Direction);
var pricesEndTime = prices.EndTime.ConvertToUtc(asset.Exchange.TimeZone);
// do not fill on stale data
if (pricesEndTime <= order.Time) return fill;
//Check if the Stop Order was filled: opposite to a limit order
switch (order.Direction)
{
case OrderDirection.Buy:
//-> 1.2 Buy Stop: If Price Above Setpoint, Buy:
if (prices.High > order.StopPrice || order.StopTriggered)
{
if (!order.StopTriggered)
{
order.StopTriggered = true;
Parameters.OnOrderUpdated(order);
}
// Fill the limit order, using closing price of bar:
// Note > Can't use minimum price, because no way to be sure minimum wasn't before the stop triggered.
if (prices.Current < order.LimitPrice)
{
fill.Status = OrderStatus.Filled;
fill.FillPrice = Math.Min(prices.High, order.LimitPrice);
// assume the order completely filled
fill.FillQuantity = order.Quantity;
}
}
break;
case OrderDirection.Sell:
//-> 1.1 Sell Stop: If Price below setpoint, Sell:
if (prices.Low < order.StopPrice || order.StopTriggered)
{
if (!order.StopTriggered)
{
order.StopTriggered = true;
Parameters.OnOrderUpdated(order);
}
// Fill the limit order, using minimum price of the bar
// Note > Can't use minimum price, because no way to be sure minimum wasn't before the stop triggered.
if (prices.Current > order.LimitPrice)
{
fill.Status = OrderStatus.Filled;
fill.FillPrice = Math.Max(prices.Low, order.LimitPrice);
// assume the order completely filled
fill.FillQuantity = order.Quantity;
}
}
break;
}
return fill;
}
/// <summary>
/// Default limit if touched fill model implementation in base class security. (Limit If Touched Order Type)
/// </summary>
/// <param name="asset"></param>
/// <param name="order"></param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
/// <remarks>
/// There is no good way to model limit orders with OHLC because we never know whether the market has
/// gapped past our fill price. We have to make the assumption of a fluid, high volume market.
///
/// With Limit if Touched orders, whether or not a trigger is surpassed is determined by the high (low)
/// of the previous tradebar when making a sell (buy) request. Following the behaviour of
/// <see cref="StopLimitFill"/>, current quote information is used when determining fill parameters
/// (e.g., price, quantity) as the tradebar containing the incoming data is not yet consolidated.
/// This conservative approach, however, can lead to trades not occuring as would be expected when
/// compared to future consolidated data.
/// </remarks>
public virtual OrderEvent LimitIfTouchedFill(Security asset, LimitIfTouchedOrder order)
{
//Default order event to return.
var utcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);
var fill = new OrderEvent(order, utcTime, OrderFee.Zero);
//If its cancelled don't need anymore checks:
if (order.Status == OrderStatus.Canceled) return fill;
// Fill only if open or extended
if (!IsExchangeOpen(asset, order))
{
return fill;
}
// Get the range of prices in the last bar:
var tradeHigh = 0m;
var tradeLow = 0m;
var pricesEndTime = DateTime.MinValue;
var subscribedTypes = GetSubscribedTypes(asset);
if (subscribedTypes.Contains(typeof(Tick)))
{
var trade = GetPricesCheckingPythonWrapper(asset, order.Direction);
if (trade != null)
{
tradeHigh = trade.Current;
tradeLow = trade.Current;
pricesEndTime = trade.EndTime.ConvertToUtc(asset.Exchange.TimeZone);
}
}
else if (subscribedTypes.Contains(typeof(TradeBar)))
{
var tradeBar = asset.Cache.GetData<TradeBar>();
if (tradeBar != null)
{
tradeHigh = tradeBar.High;
tradeLow = tradeBar.Low;
pricesEndTime = tradeBar.EndTime.ConvertToUtc(asset.Exchange.TimeZone);
}
}
// do not fill on stale data
if (pricesEndTime <= order.Time) return fill;
switch (order.Direction)
{
case OrderDirection.Sell:
if (tradeHigh >= order.TriggerPrice || order.TriggerTouched)
{
order.TriggerTouched = true;
//-> 1.1 Limit surpassed: Sell.
if (GetAskPrice(asset, out pricesEndTime) >= order.LimitPrice)
{
fill.Status = OrderStatus.Filled;
fill.FillPrice = order.LimitPrice;
// assume the order completely filled
fill.FillQuantity = order.Quantity;
}
}
break;
case OrderDirection.Buy:
if (tradeLow <= order.TriggerPrice || order.TriggerTouched)
{
order.TriggerTouched = true;
//-> 1.2 Limit surpassed: Buy.
if (GetBidPrice(asset, out pricesEndTime) <= order.LimitPrice)
{
fill.Status = OrderStatus.Filled;
fill.FillPrice = order.LimitPrice;
// assume the order completely filled
fill.FillQuantity = order.Quantity;
}
}
break;
}
return fill;
}
/// <summary>
/// Default limit order fill model in the base security class.
/// </summary>
/// <param name="asset">Security asset we're filling</param>
/// <param name="order">Order packet to model</param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
/// <seealso cref="StopMarketFill(Security, StopMarketOrder)"/>
/// <seealso cref="MarketFill(Security, MarketOrder)"/>
public virtual OrderEvent LimitFill(Security asset, LimitOrder order)
{
return InternalLimitFill(asset, order, order.LimitPrice, order.Quantity);
}
/// <summary>
/// Default limit order fill model in the base security class.
/// </summary>
private OrderEvent InternalLimitFill(Security asset, Order order, decimal limitPrice, decimal quantity)
{
//Initialise;
var utcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);
var fill = new OrderEvent(order, utcTime, OrderFee.Zero);
//If its cancelled don't need anymore checks:
if (order.Status == OrderStatus.Canceled) return fill;
// make sure the exchange is open before filling -- allow pre/post market fills to occur
if (!IsExchangeOpen(asset, order))
{
return fill;
}
//Get the range of prices in the last bar:
var orderDirection = order.Direction;
var prices = GetPricesCheckingPythonWrapper(asset, orderDirection);
var pricesEndTime = prices.EndTime.ConvertToUtc(asset.Exchange.TimeZone);
// do not fill on stale data
if (pricesEndTime <= order.Time) return fill;
//-> Valid Live/Model Order:
switch (orderDirection)
{
case OrderDirection.Buy:
//Buy limit seeks lowest price
if (prices.Low < limitPrice)
{
//Set order fill:
fill.Status = OrderStatus.Filled;
// fill at the worse price this bar or the limit price, this allows far out of the money limits
// to be executed properly
fill.FillPrice = Math.Min(prices.High, limitPrice);
// assume the order completely filled
fill.FillQuantity = quantity;
}
break;
case OrderDirection.Sell:
//Sell limit seeks highest price possible
if (prices.High > limitPrice)
{
fill.Status = OrderStatus.Filled;
// fill at the worse price this bar or the limit price, this allows far out of the money limits
// to be executed properly
fill.FillPrice = Math.Max(prices.Low, limitPrice);
// assume the order completely filled
fill.FillQuantity = quantity;
}
break;
}
return fill;
}
/// <summary>
/// Market on Open Fill Model. Return an order event with the fill details
/// </summary>
/// <param name="asset">Asset we're trading with this order</param>
/// <param name="order">Order to be filled</param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
public virtual OrderEvent MarketOnOpenFill(Security asset, MarketOnOpenOrder order)
{
if (asset.Exchange.Hours.IsMarketAlwaysOpen)
{
throw new InvalidOperationException(Messages.FillModel.MarketNeverCloses(asset, OrderType.MarketOnOpen));
}
var utcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);
var fill = new OrderEvent(order, utcTime, OrderFee.Zero);
if (order.Status == OrderStatus.Canceled) return fill;
// MOO should never fill on the same bar or on stale data
// Imagine the case where we have a thinly traded equity, ASUR, and another liquid
// equity, say SPY, SPY gets data every minute but ASUR, if not on fill forward, maybe
// have large gaps, in which case the currentBar.EndTime will be in the past
// ASUR | | | [order] | | | | | | |
// SPY | | | | | | | | | | | | | | | | | | | |
var currentBar = asset.GetLastData();
var localOrderTime = order.Time.ConvertFromUtc(asset.Exchange.TimeZone);
if (currentBar == null || localOrderTime >= currentBar.EndTime) return fill;
// if the MOO was submitted during market the previous day, wait for a day to turn over
if (asset.Exchange.DateTimeIsOpen(localOrderTime) && localOrderTime.Date == asset.LocalTime.Date)
{
return fill;
}
// wait until market open
// make sure the exchange is open/normal market hours before filling
if (!IsExchangeOpen(asset, false)) return fill;
fill.FillPrice = GetPricesCheckingPythonWrapper(asset, order.Direction).Open;
fill.Status = OrderStatus.Filled;
//Calculate the model slippage: e.g. 0.01c
var slip = asset.SlippageModel.GetSlippageApproximation(asset, order);
//Apply slippage
switch (order.Direction)
{
case OrderDirection.Buy:
fill.FillPrice += slip;
// assume the order completely filled
fill.FillQuantity = order.Quantity;
break;
case OrderDirection.Sell:
fill.FillPrice -= slip;
// assume the order completely filled
fill.FillQuantity = order.Quantity;
break;
}
return fill;
}
/// <summary>
/// Market on Close Fill Model. Return an order event with the fill details
/// </summary>
/// <param name="asset">Asset we're trading with this order</param>
/// <param name="order">Order to be filled</param>
/// <returns>Order fill information detailing the average price and quantity filled.</returns>
public virtual OrderEvent MarketOnCloseFill(Security asset, MarketOnCloseOrder order)
{
if (asset.Exchange.Hours.IsMarketAlwaysOpen)
{
throw new InvalidOperationException(Messages.FillModel.MarketNeverCloses(asset, OrderType.MarketOnClose));
}
var utcTime = asset.LocalTime.ConvertToUtc(asset.Exchange.TimeZone);
var fill = new OrderEvent(order, utcTime, OrderFee.Zero);
if (order.Status == OrderStatus.Canceled) return fill;
var localOrderTime = order.Time.ConvertFromUtc(asset.Exchange.TimeZone);
var nextMarketClose = asset.Exchange.Hours.GetNextMarketClose(localOrderTime, false);
// wait until market closes after the order time
if (asset.LocalTime < nextMarketClose)
{
return fill;
}
// LocalTime has reached or passed market close, proceed to fill
fill.FillPrice = GetPricesCheckingPythonWrapper(asset, order.Direction).Close;
fill.Status = OrderStatus.Filled;
//Calculate the model slippage: e.g. 0.01c
var slip = asset.SlippageModel.GetSlippageApproximation(asset, order);
//Apply slippage
switch (order.Direction)
{
case OrderDirection.Buy:
fill.FillPrice += slip;
// assume the order completely filled
fill.FillQuantity = order.Quantity;
break;
case OrderDirection.Sell:
fill.FillPrice -= slip;
// assume the order completely filled
fill.FillQuantity = order.Quantity;
break;
}
return fill;
}
/// <summary>
/// Get current ask price for subscribed data
/// This method will try to get the most recent ask price data, so it will try to get tick quote first, then quote bar.
/// If no quote, tick or bar, is available (e.g. hourly data), use trade data with preference to tick data.
/// </summary>
/// <param name="asset">Security which has subscribed data types</param>
/// <param name="endTime">Timestamp of the most recent data type</param>
private decimal GetAskPrice(Security asset, out DateTime endTime)
{
var subscribedTypes = GetSubscribedTypes(asset);
List<Tick> ticks = null;
var isTickSubscribed = subscribedTypes.Contains(typeof(Tick));
if (isTickSubscribed)
{
ticks = asset.Cache.GetAll<Tick>().ToList();
var quote = ticks.LastOrDefault(x => x.TickType == TickType.Quote && x.AskPrice > 0);
if (quote != null)
{
endTime = quote.EndTime;
return quote.AskPrice;
}
}
if (subscribedTypes.Contains(typeof(QuoteBar)))
{
var quoteBar = asset.Cache.GetData<QuoteBar>();
if (quoteBar != null)
{
endTime = quoteBar.EndTime;
return quoteBar.Ask?.Close ?? quoteBar.Close;
}
}
if (isTickSubscribed)
{
var trade = ticks.LastOrDefault(x => x.TickType == TickType.Trade && x.Price > 0);
if (trade != null)
{
endTime = trade.EndTime;
return trade.Price;
}
}
if (subscribedTypes.Contains(typeof(TradeBar)))
{
var tradeBar = asset.Cache.GetData<TradeBar>();
if (tradeBar != null)
{
endTime = tradeBar.EndTime;
return tradeBar.Close;
}
}
throw new InvalidOperationException(Messages.FillModel.NoMarketDataToGetAskPriceForFilling(asset));
}
/// <summary>
/// Get current bid price for subscribed data
/// This method will try to get the most recent bid price data, so it will try to get tick quote first, then quote bar.
/// If no quote, tick or bar, is available (e.g. hourly data), use trade data with preference to tick data.
/// </summary>
/// <param name="asset">Security which has subscribed data types</param>
/// <param name="endTime">Timestamp of the most recent data type</param>
private decimal GetBidPrice(Security asset, out DateTime endTime)
{
var subscribedTypes = GetSubscribedTypes(asset);
List<Tick> ticks = null;
var isTickSubscribed = subscribedTypes.Contains(typeof(Tick));
if (isTickSubscribed)
{
ticks = asset.Cache.GetAll<Tick>().ToList();
var quote = ticks.LastOrDefault(x => x.TickType == TickType.Quote && x.BidPrice > 0);
if (quote != null)
{
endTime = quote.EndTime;
return quote.BidPrice;
}
}
if (subscribedTypes.Contains(typeof(QuoteBar)))
{
var quoteBar = asset.Cache.GetData<QuoteBar>();
if (quoteBar != null)
{
endTime = quoteBar.EndTime;
return quoteBar.Bid?.Close ?? quoteBar.Close;
}
}
if (isTickSubscribed)
{
var trade = ticks.LastOrDefault(x => x.TickType == TickType.Trade && x.Price > 0);
if (trade != null)
{
endTime = trade.EndTime;
return trade.Price;
}
}
if (subscribedTypes.Contains(typeof(TradeBar)))
{
var tradeBar = asset.Cache.GetData<TradeBar>();
if (tradeBar != null)
{
endTime = tradeBar.EndTime;
return tradeBar.Close;
}
}
throw new InvalidOperationException(Messages.FillModel.NoMarketDataToGetBidPriceForFilling(asset));
}
/// <summary>
/// Get data types the Security is subscribed to
/// </summary>
/// <param name="asset">Security which has subscribed data types</param>
protected virtual HashSet<Type> GetSubscribedTypes(Security asset)
{
var subscribedTypes = Parameters
.ConfigProvider
// even though data from internal configurations are not sent to the algorithm.OnData they still drive security cache and data
// this is specially relevant for the continuous contract underlying mapped contracts which are internal configurations
.GetSubscriptionDataConfigs(asset.Symbol, includeInternalConfigs: true)
.ToHashSet(x => x.Type);
if (subscribedTypes.Count == 0)
{
throw new InvalidOperationException(Messages.FillModel.NoDataSubscriptionFoundForFilling(asset));
}
return subscribedTypes;
}
/// <summary>
/// Helper method to determine if the exchange is open before filling. Will allow pre/post market fills to occur based on configuration
/// </summary>
/// <param name="asset">Security which has subscribed data types</param>
private bool IsExchangeOpen(Security asset)
{
// even though data from internal configurations are not sent to the algorithm.OnData they still drive security cache and data
// this is specially relevant for the continuous contract underlying mapped contracts which are internal configurations
var configs = Parameters.ConfigProvider.GetSubscriptionDataConfigs(asset.Symbol, includeInternalConfigs: true);
if (configs.Count == 0)
{
throw new InvalidOperationException(Messages.FillModel.NoDataSubscriptionFoundForFilling(asset));
}
var hasNonInternals = false;
var exchangeOpenNonInternals = false;
var exchangeOpenInternals = false;
for (int i = 0; i < configs.Count; i++)
{
var config = configs[i];
if (config.IsInternalFeed)
{
exchangeOpenInternals |= config.ExtendedMarketHours;
}
else
{
hasNonInternals = true;
exchangeOpenNonInternals |= config.ExtendedMarketHours;
}
}