/*
* 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.Algorithm.Framework.Alphas;
using QuantConnect.Algorithm.Framework.Execution;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Algorithm.Framework.Risk;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Interfaces;
using System.Collections.Generic;
using System.Linq;
namespace QuantConnect.Algorithm.CSharp
{
///
/// Framework algorithm that uses the .
/// This model extendes and uses Pearson correlation
/// to rank the pairs trading candidates and use the best candidate to trade.
///
public class PearsonCorrelationPairsTradingAlphaModelFrameworkAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
public override void Initialize()
{
SetStartDate(2013, 10, 07);
SetEndDate(2013, 10, 11);
var symbols = new[] { "SPY", "AIG", "BAC", "IBM" }
.Select(ticker => QuantConnect.Symbol.Create(ticker, SecurityType.Equity, Market.USA))
.ToList();
// Manually add SPY and AIG when the algorithm starts
SetUniverseSelection(new ManualUniverseSelectionModel(symbols.Take(2)));
// At midnight, add all securities every day except on the last data
// With this procedure, the Alpha Model will experience multiple universe changes
AddUniverseSelection(new ScheduledUniverseSelectionModel(
DateRules.EveryDay(), TimeRules.Midnight,
dt => dt < EndDate.AddDays(-1) ? symbols : Enumerable.Empty()));
SetAlpha(new PearsonCorrelationPairsTradingAlphaModel(252, Resolution.Daily));
SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel());
SetExecution(new ImmediateExecutionModel());
SetRiskManagement(new NullRiskManagementModel());
}
public override void OnEndOfAlgorithm()
{
// We have removed all securities from the universe. The Alpha Model should remove the consolidator
var consolidatorCount = SubscriptionManager.Subscriptions.Sum(s => s.Consolidators.Count);
if (consolidatorCount > 0)
{
throw new RegressionTestException($"The number of consolidator is should be zero. Actual: {consolidatorCount}");
}
}
///
/// 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, Language.Python };
///
/// Data Points count of all timeslices of algorithm
///
public long DataPoints => 14089;
///
/// Data Points count of the algorithm history
///
public int AlgorithmHistoryDataPoints => 1008;
///
/// 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", "6"},
{"Average Win", "0.99%"},
{"Average Loss", "-0.84%"},
{"Compounding Annual Return", "24.021%"},
{"Drawdown", "0.800%"},
{"Expectancy", "0.089"},
{"Start Equity", "100000"},
{"End Equity", "100295.35"},
{"Net Profit", "0.295%"},
{"Sharpe Ratio", "4.205"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "61.706%"},
{"Loss Rate", "50%"},
{"Win Rate", "50%"},
{"Profit-Loss Ratio", "1.18"},
{"Alpha", "0.08"},
{"Beta", "0.06"},
{"Annual Standard Deviation", "0.047"},
{"Annual Variance", "0.002"},
{"Information Ratio", "-8.305"},
{"Tracking Error", "0.214"},
{"Treynor Ratio", "3.313"},
{"Total Fees", "$31.60"},
{"Estimated Strategy Capacity", "$3200000.00"},
{"Lowest Capacity Asset", "AIG R735QTJ8XC9X"},
{"Portfolio Turnover", "80.47%"},
{"Drawdown Recovery", "0"},
{"OrderListHash", "476d54ac7295563a79add3a80310a0a8"}
};
}
}