/* * 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; using QuantConnect.Interfaces; using QuantConnect.Orders; using QuantConnect.Securities; using QuantConnect.Securities.Future; namespace QuantConnect.Algorithm.CSharp { /// /// This example demonstrates how to add futures with daily resolution. /// /// /// /// public class BasicTemplateFuturesDailyAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition { protected virtual Resolution Resolution => Resolution.Daily; protected virtual bool ExtendedMarketHours => false; // S&P 500 EMini futures private const string RootSP500 = Futures.Indices.SP500EMini; // Gold futures private const string RootGold = Futures.Metals.Gold; private Future _futureSP500; private Future _futureGold; /// /// Initialize your algorithm and add desired assets. /// public override void Initialize() { SetStartDate(2013, 10, 08); SetEndDate(2014, 10, 10); SetCash(1000000); _futureSP500 = AddFuture(RootSP500, Resolution, extendedMarketHours: ExtendedMarketHours); _futureGold = AddFuture(RootGold, Resolution, extendedMarketHours: ExtendedMarketHours); // set our expiry filter for this futures chain // SetFilter method accepts TimeSpan objects or integer for days. // The following statements yield the same filtering criteria _futureSP500.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182)); _futureGold.SetFilter(0, 182); } /// /// Event - v3.0 DATA EVENT HANDLER: (Pattern) Basic template for user to override for receiving all subscription data in a single event /// /// The current slice of data keyed by symbol string public override void OnData(Slice slice) { if (!Portfolio.Invested) { foreach(var chain in slice.FutureChains) { // find the front contract expiring no earlier than in 90 days var contract = ( from futuresContract in chain.Value.OrderBy(x => x.Expiry) where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract ).FirstOrDefault(); // if found, trade it if (contract != null) { // Let's check if market is actually open to place market orders. For example: for daily resolution, data can come at a // time when market is closed, like 7:00PM. if (Securities[contract.Symbol].Exchange.ExchangeOpen) { MarketOrder(contract.Symbol, 1); } else { // MOO are not allowed for futures, so to make sure, use limit order instead. We use a very big limit price here // to make the order fill on next bar. LimitOrder(contract.Symbol, 1, contract.AskPrice * 2); } } } } else { // Same as above, let's check if market is open to place market orders. if (Portfolio.Values.Any(x => !Securities[x.Symbol].Exchange.ExchangeOpen)) { foreach (var holdings in Portfolio.Values.OrderBy(x => x.Symbol)) { // use a very low limit price here to make the order fill on next bar. LimitOrder(holdings.Symbol, -holdings.Quantity, 1m); } } else { Liquidate(); } } foreach (var changedEvent in slice.SymbolChangedEvents.Values) { if (Time.TimeOfDay != TimeSpan.Zero) { throw new Exception($"{Time} unexpected symbol changed event {changedEvent}!"); } } } /// /// 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 virtual bool CanRunLocally { get; } = true; /// /// This is used by the regression test system to indicate which languages this algorithm is written in. /// public virtual Language[] Languages { get; } = { Language.CSharp, Language.Python }; /// /// Data Points count of all timeslices of algorithm /// public virtual long DataPoints => 11721; /// /// Data Points count of the algorithm history /// public virtual int AlgorithmHistoryDataPoints => 0; /// /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm /// public virtual Dictionary ExpectedStatistics => new Dictionary { {"Total Trades", "56"}, {"Average Win", "0.30%"}, {"Average Loss", "-3.89%"}, {"Compounding Annual Return", "-15.100%"}, {"Drawdown", "15.200%"}, {"Expectancy", "-0.784"}, {"Net Profit", "-15.207%"}, {"Sharpe Ratio", "-1.13"}, {"Probabilistic Sharpe Ratio", "0.000%"}, {"Loss Rate", "80%"}, {"Win Rate", "20%"}, {"Profit-Loss Ratio", "0.08"}, {"Alpha", "-0.093"}, {"Beta", "-0.081"}, {"Annual Standard Deviation", "0.091"}, {"Annual Variance", "0.008"}, {"Information Ratio", "-1.694"}, {"Tracking Error", "0.132"}, {"Treynor Ratio", "1.279"}, {"Total Fees", "$99.90"}, {"Estimated Strategy Capacity", "$130000000.00"}, {"Lowest Capacity Asset", "ES VP274HSU1AF5"}, {"Fitness Score", "0.011"}, {"Kelly Criterion Estimate", "0"}, {"Kelly Criterion Probability Value", "0"}, {"Sortino Ratio", "-0.225"}, {"Return Over Maximum Drawdown", "-0.99"}, {"Portfolio Turnover", "0.028"}, {"Total Insights Generated", "0"}, {"Total Insights Closed", "0"}, {"Total Insights Analysis Completed", "0"}, {"Long Insight Count", "0"}, {"Short Insight Count", "0"}, {"Long/Short Ratio", "100%"}, {"Estimated Monthly Alpha Value", "$0"}, {"Total Accumulated Estimated Alpha Value", "$0"}, {"Mean Population Estimated Insight Value", "$0"}, {"Mean Population Direction", "0%"}, {"Mean Population Magnitude", "0%"}, {"Rolling Averaged Population Direction", "0%"}, {"Rolling Averaged Population Magnitude", "0%"}, {"OrderListHash", "b05e6b7d992a1c42edeef553b9f6c1f1"} }; } }