Lean  $LEAN_TAG$
LatestPriceFillModel.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Linq;
20 
22 {
23  /// <summary>
24  /// This fill model is provided for cases where the trade/quote distinction should be
25  /// ignored and the fill price should be determined from the latest pricing information.
26  /// </summary>
28  {
29  /// <summary>
30  /// Get the minimum and maximum price for this security in the last bar
31  /// Ignore the Trade/Quote distinction - fill with the latest pricing information
32  /// </summary>
33  /// <param name="asset">Security asset we're checking</param>
34  /// <param name="direction">The order direction, decides whether to pick bid or ask</param>
35  protected override Prices GetPrices(Security asset, OrderDirection direction)
36  {
37  var low = asset.Low;
38  var high = asset.High;
39  var open = asset.Open;
40  var close = asset.Close;
41  var current = asset.Price;
42  var endTime = asset.Cache.GetData()?.EndTime ?? DateTime.MinValue;
43 
44  if (direction == OrderDirection.Hold)
45  {
46  return new Prices(endTime, current, open, high, low, close);
47  }
48 
49  // Only fill with data types we are subscribed to
50  var subscriptionTypes = Parameters.ConfigProvider
51  .GetSubscriptionDataConfigs(asset.Symbol)
52  .Select(x => x.Type).ToList();
53 
54  // Tick
55  var tick = asset.Cache.GetData<Tick>();
56  if (subscriptionTypes.Contains(typeof(Tick)) && tick != null)
57  {
58  var price = direction == OrderDirection.Sell ? tick.BidPrice : tick.AskPrice;
59  if (price != 0m)
60  {
61  return new Prices(endTime, price, 0, 0, 0, 0);
62  }
63 
64  // If the ask/bid spreads are not available for ticks, try the price
65  price = tick.Price;
66  if (price != 0m)
67  {
68  return new Prices(endTime, price, 0, 0, 0, 0);
69  }
70  }
71 
72  // Get both the last trade and last quote
73  // Assume that the security has both a trade and quote subscription
74  // This should be true for crypto securities
75  var quoteBar = asset.Cache.GetData<QuoteBar>();
76  if (quoteBar != null)
77  {
78  var tradeBar = asset.Cache.GetData<TradeBar>();
79 
80  if (tradeBar != null && tradeBar.EndTime > quoteBar.EndTime)
81  {
82  // The latest pricing data came from a trade
83  return new Prices(tradeBar);
84  }
85  else
86  {
87  // The latest pricing data came from a quote
88  var bar = direction == OrderDirection.Sell ? quoteBar.Bid : quoteBar.Ask;
89  if (bar != null)
90  {
91  return new Prices(quoteBar.EndTime, bar);
92  }
93  }
94  }
95 
96  return new Prices(endTime, current, open, high, low, close);
97  }
98  }
99 }