/* * 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 QuantConnect.Securities; namespace QuantConnect.Orders.TimeInForces { /// /// Good Til Date Time In Force - order expires and will be cancelled on a fixed date/time /// public class GoodTilDateTimeInForce : TimeInForce { /// /// The date/time on which the order will expire and will be cancelled /// /// The private set is required for JSON deserialization public DateTime Expiry { get; private set; } /// /// Initializes a new instance of the class /// /// This constructor is required for JSON deserialization private GoodTilDateTimeInForce() { } /// /// Initializes a new instance of the class /// public GoodTilDateTimeInForce(DateTime expiry) { Expiry = expiry; } /// /// Checks if an order is expired /// /// The security matching the order /// The order to be checked /// Returns true if the order has expired, false otherwise public override bool IsOrderExpired(Security security, Order order) { var exchangeHours = security.Exchange.Hours; var time = security.LocalTime; bool expired; switch (order.SecurityType) { case SecurityType.Forex: case SecurityType.Cfd: // With real brokerages (IB, Oanda, FXCM have been verified) FX orders expire at 5 PM NewYork time. // For now we use this fixed cut-off time, in future we might get this value from brokerage models, // to support custom brokerage implementations. expired = time.ConvertToUtc(exchangeHours.TimeZone) >= GetForexOrderExpiryDateTime(order); break; case SecurityType.Crypto: case SecurityType.CryptoFuture: // expires at midnight after expiry date expired = time.Date > Expiry.Date; break; case SecurityType.Equity: case SecurityType.Option: case SecurityType.Future: case SecurityType.FutureOption: case SecurityType.IndexOption: default: // expires at market close of expiry date expired = time >= exchangeHours.GetLastDailyMarketClose(Expiry.Date, false); break; } return expired; } /// /// Checks if an order fill is valid /// /// The security matching the order /// The order to be checked /// The order fill to be checked /// Returns true if the order fill can be emitted, false otherwise public override bool IsFillValid(Security security, Order order, OrderEvent fill) { return true; } /// /// Returns the expiry date and time (UTC) for a Forex order /// public DateTime GetForexOrderExpiryDateTime(Order order) { var cutOffTimeZone = TimeZones.NewYork; var cutOffTimeSpan = TimeSpan.FromHours(17); var expiryTime = Expiry.Date.Add(cutOffTimeSpan); if (order.Time.Date == Expiry.Date) { // expiry date same as order date var orderTime = order.Time.ConvertFromUtc(cutOffTimeZone); if (orderTime.TimeOfDay > cutOffTimeSpan) { // order submitted after 5 PM, expiry on next date expiryTime = expiryTime.AddDays(1); } } return expiryTime.ConvertToUtc(cutOffTimeZone); } } }