Lean  $LEAN_TAG$
DayTimeInForce.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;
18 
20 {
21  /// <summary>
22  /// Day Time In Force - order expires at market close
23  /// </summary>
24  public class DayTimeInForce : TimeInForce
25  {
26  /// <summary>
27  /// Checks if an order is expired
28  /// </summary>
29  /// <param name="security">The security matching the order</param>
30  /// <param name="order">The order to be checked</param>
31  /// <returns>Returns true if the order has expired, false otherwise</returns>
32  public override bool IsOrderExpired(Security security, Order order)
33  {
34  var exchangeHours = security.Exchange.Hours;
35 
36  var orderTime = order.Time.ConvertFromUtc(exchangeHours.TimeZone);
37  var time = security.LocalTime;
38 
39  bool expired;
40  switch (order.SecurityType)
41  {
42  case SecurityType.Forex:
43  case SecurityType.Cfd:
44  // With real brokerages (IB, Oanda, FXCM have been verified) FX orders expire at 5 PM NewYork time.
45  // For now we use this fixed cut-off time, in future we might get this value from brokerage models,
46  // to support custom brokerage implementations.
47 
48  var cutOffTimeZone = TimeZones.NewYork;
49  var cutOffTimeSpan = TimeSpan.FromHours(17);
50 
51  orderTime = order.Time.ConvertFromUtc(cutOffTimeZone);
52  var expiryTime = orderTime.Date.Add(cutOffTimeSpan);
53  if (orderTime.TimeOfDay > cutOffTimeSpan)
54  {
55  // order submitted after 5 PM, expiry on next date
56  expiryTime = expiryTime.AddDays(1);
57  }
58 
59  expired = time.ConvertTo(exchangeHours.TimeZone, cutOffTimeZone) >= expiryTime;
60  break;
61 
62  case SecurityType.Crypto:
63  case SecurityType.CryptoFuture:
64  // expires at midnight UTC
65  expired = time.Date > orderTime.Date;
66  break;
67 
68  case SecurityType.Equity:
69  case SecurityType.Option:
70  case SecurityType.Future:
71  case SecurityType.FutureOption:
72  case SecurityType.IndexOption:
73  default:
74  // expires at market close
75  expired = time >= exchangeHours.GetNextMarketClose(orderTime, false);
76  break;
77  }
78 
79  return expired;
80  }
81 
82  /// <summary>
83  /// Checks if an order fill is valid
84  /// </summary>
85  /// <param name="security">The security matching the order</param>
86  /// <param name="order">The order to be checked</param>
87  /// <param name="fill">The order fill to be checked</param>
88  /// <returns>Returns true if the order fill can be emitted, false otherwise</returns>
89  public override bool IsFillValid(Security security, Order order, OrderEvent fill)
90  {
91  return true;
92  }
93  }
94 }