/* * 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.Securities; namespace QuantConnect.Algorithm.CSharp { /// /// This example demonstrates how to add futures with daily resolution. /// /// /// /// public class BasicTemplateFuturesDailyAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition { private Symbol _contractSymbol; protected virtual Resolution Resolution => Resolution.Daily; // S&P 500 EMini futures private const string RootSP500 = Futures.Indices.SP500EMini; // Gold futures private const string RootGold = Futures.Metals.Gold; /// /// Initialize your algorithm and add desired assets. /// public override void Initialize() { SetStartDate(2013, 10, 08); SetEndDate(2014, 10, 10); SetCash(1000000); var futureSP500 = AddFuture(RootSP500, Resolution); var futureGold = AddFuture(RootGold, Resolution); // 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 && IsMarketOpen(contract.Symbol)) { _contractSymbol = contract.Symbol; MarketOrder(_contractSymbol, 1); } } } 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 => 13259; /// /// 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", "92"}, {"Average Win", "0.09%"}, {"Average Loss", "-0.01%"}, {"Compounding Annual Return", "-0.415%"}, {"Drawdown", "0.400%"}, {"Expectancy", "-0.811"}, {"Net Profit", "-0.418%"}, {"Sharpe Ratio", "-1.675"}, {"Probabilistic Sharpe Ratio", "0%"}, {"Loss Rate", "98%"}, {"Win Rate", "2%"}, {"Profit-Loss Ratio", "7.67"}, {"Alpha", "-0.003"}, {"Beta", "-0.001"}, {"Annual Standard Deviation", "0.002"}, {"Annual Variance", "0"}, {"Information Ratio", "-1.392"}, {"Tracking Error", "0.089"}, {"Treynor Ratio", "4.265"}, {"Total Fees", "$170.20"}, {"Estimated Strategy Capacity", "$55000.00"}, {"Lowest Capacity Asset", "ES VP274HSU1AF5"}, {"Fitness Score", "0.009"}, {"Kelly Criterion Estimate", "0"}, {"Kelly Criterion Probability Value", "0"}, {"Sortino Ratio", "-0.736"}, {"Return Over Maximum Drawdown", "-0.992"}, {"Portfolio Turnover", "0.025"}, {"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", "ec657d3287f35cec85b4b9cd5c3adb7f"} }; } }