/* * 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.Linq; using QuantConnect.Data; using QuantConnect.Orders; using QuantConnect.Interfaces; using QuantConnect.Securities; using System.Collections.Generic; using QuantConnect.Securities.Future; namespace QuantConnect.Algorithm.CSharp { /// /// Regression algorithm asserting that market orders are supported on extended market hours for futures. /// public class MarketOrdersAreSupportedOnExtendedHoursForFuturesRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition { private Future _continuousContract; private Future _futureContract; public override void Initialize() { SetStartDate(2013, 10, 6); SetEndDate(2013, 10, 10); _continuousContract = AddFuture(Futures.Indices.SP500EMini, dataNormalizationMode: DataNormalizationMode.BackwardsRatio, dataMappingMode: DataMappingMode.LastTradingDay, contractDepthOffset: 0, extendedMarketHours: true ); _futureContract = AddFutureContract(FuturesChain(_continuousContract.Symbol).First(), extendedMarketHours: true); } public override void OnData(Slice slice) { if (Time.TimeOfDay.Hours > 18 && !Portfolio.Invested) { var futureContractMarketOrder = MarketOrder(_futureContract.Symbol, 1); var continuousContractMarketOrder = MarketOrder(_continuousContract.Mapped, 1); if (futureContractMarketOrder.Status == OrderStatus.Invalid || continuousContractMarketOrder.Status == OrderStatus.Invalid) { throw new RegressionTestException($"Market orders should be allowed for futures outside of regular market hours"); } } } public override void OnEndOfAlgorithm() { if (Transactions.GetOrders().Any(order => order.Status != OrderStatus.Filled )) { throw new RegressionTestException("Not all orders were filled"); } } public override void OnOrderEvent(OrderEvent orderEvent) { // 13:30 and 21:00 UTC are 9:30 and 17 New york, which are the regular market hours litimits for this security if (orderEvent.Status == OrderStatus.Filled && !Securities[orderEvent.Symbol].Exchange.DateTimeIsOpen(orderEvent.UtcTime) && (orderEvent.UtcTime.TimeOfDay >= new TimeSpan(13, 30, 0) && orderEvent.UtcTime.TimeOfDay < new TimeSpan(21, 0, 0))) { throw new RegressionTestException($"Order should have been filled during extended market hours"); } } /// /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. /// public bool CanRunLocally { get; } = true; /// /// This is used by the regression test system to indicate which languages this algorithm is written in. /// public List Languages { get; } = new() { Language.CSharp }; /// /// Data Points count of all timeslices of algorithm /// public long DataPoints => 50978; /// /// Data Points count of the algorithm history /// public int AlgorithmHistoryDataPoints => 1; /// /// Final status of the algorithm /// public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; /// /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm /// public Dictionary ExpectedStatistics => new Dictionary { {"Total Orders", "2"}, {"Average Win", "0%"}, {"Average Loss", "0%"}, {"Compounding Annual Return", "-0.626%"}, {"Drawdown", "2.400%"}, {"Expectancy", "0"}, {"Start Equity", "100000"}, {"End Equity", "99991.4"}, {"Net Profit", "-0.009%"}, {"Sharpe Ratio", "1.959"}, {"Sortino Ratio", "4.862"}, {"Probabilistic Sharpe Ratio", "53.060%"}, {"Loss Rate", "0%"}, {"Win Rate", "0%"}, {"Profit-Loss Ratio", "0"}, {"Alpha", "0.239"}, {"Beta", "0.694"}, {"Annual Standard Deviation", "0.177"}, {"Annual Variance", "0.031"}, {"Information Ratio", "2.05"}, {"Tracking Error", "0.093"}, {"Treynor Ratio", "0.501"}, {"Total Fees", "$4.30"}, {"Estimated Strategy Capacity", "$15000000.00"}, {"Lowest Capacity Asset", "ES VU1EHIDJYLMP"}, {"Portfolio Turnover", "33.51%"}, {"Drawdown Recovery", "3"}, {"OrderListHash", "523d76e17b23ee0c94ded9f826cb8c22"} }; } }