/* * 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 QuantConnect.Orders; using QuantConnect.Interfaces; using System.Collections.Generic; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Execution; using QuantConnect.Algorithm.Framework.Portfolio; using QuantConnect.Algorithm.Framework.Selection; namespace QuantConnect.Algorithm.CSharp { /// /// Regression algorithm for the SpreadExecutionModel. /// This algorithm shows how the execution model works to /// submit orders only when the price is on desirably tight spread. /// /// /// /// public class SpreadExecutionModelRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition { /// /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// public override void Initialize() { SetStartDate(2013, 10, 7); SetEndDate(2013, 10, 11); SetUniverseSelection(new ManualUniverseSelectionModel( QuantConnect.Symbol.Create("AIG", SecurityType.Equity, Market.USA), QuantConnect.Symbol.Create("BAC", SecurityType.Equity, Market.USA), QuantConnect.Symbol.Create("IBM", SecurityType.Equity, Market.USA), QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA))); // using hourly rsi to generate more insights SetAlpha(new RsiAlphaModel(14, Resolution.Hour)); SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel()); SetExecution(new SpreadExecutionModel()); InsightsGenerated += OnInsightsGenerated; } private void OnInsightsGenerated(IAlgorithm algorithm, GeneratedInsightsCollection eventdata) { Log($"{Time}: {string.Join(", ", eventdata)}"); } /// /// Order fill event handler. On an order fill update the resulting information is passed to this method. /// /// Order event details containing details of the events /// This method can be called asynchronously and so should only be used by seasoned C# experts. Ensure you use proper locks on thread-unsafe objects public override void OnOrderEvent(OrderEvent orderEvent) { Debug($"Purchased Stock: {orderEvent.Symbol}"); } /// /// 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 => 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, Language.Python }; /// /// Data Points count of all timeslices of algorithm /// public long DataPoints => 15643; /// /// Data Points count of the algorithm history /// public int AlgorithmHistoryDataPoints => 56; /// /// 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", "19"}, {"Average Win", "0.62%"}, {"Average Loss", "-0.19%"}, {"Compounding Annual Return", "398.867%"}, {"Drawdown", "1.500%"}, {"Expectancy", "1.395"}, {"Start Equity", "100000"}, {"End Equity", "102076.08"}, {"Net Profit", "2.076%"}, {"Sharpe Ratio", "10.465"}, {"Sortino Ratio", "21.499"}, {"Probabilistic Sharpe Ratio", "70.084%"}, {"Loss Rate", "44%"}, {"Win Rate", "56%"}, {"Profit-Loss Ratio", "3.31"}, {"Alpha", "0.533"}, {"Beta", "1.115"}, {"Annual Standard Deviation", "0.261"}, {"Annual Variance", "0.068"}, {"Information Ratio", "8.837"}, {"Tracking Error", "0.086"}, {"Treynor Ratio", "2.453"}, {"Total Fees", "$40.66"}, {"Estimated Strategy Capacity", "$1900000.00"}, {"Lowest Capacity Asset", "IBM R735QTJ8XC9X"}, {"Portfolio Turnover", "146.73%"}, {"Drawdown Recovery", "1"}, {"OrderListHash", "36ab6f7236250f7a064b77af9b4870c4"} }; } }