Lean  $LEAN_TAG$
PearsonCorrelationPairsTradingAlphaModel.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using MathNet.Numerics.Statistics;
17 using QuantConnect.Data;
19 using System;
20 using System.Collections.Generic;
21 using System.Linq;
22 
24 {
25  /// <summary>
26  /// This alpha model is designed to rank every pair combination by its pearson correlation
27  /// and trade the pair with the hightest correlation
28  /// This model generates alternating long ratio/short ratio insights emitted as a group
29  /// </summary>
31  {
32  private readonly int _lookback;
33  private readonly Resolution _resolution;
34  private readonly double _minimumCorrelation;
35  private Tuple<Symbol, Symbol> _bestPair;
36 
37  /// <summary>
38  /// Initializes a new instance of the <see cref="PearsonCorrelationPairsTradingAlphaModel"/> class
39  /// </summary>
40  /// <param name="lookback">Lookback period of the analysis</param>
41  /// <param name="resolution">Analysis resolution</param>
42  /// <param name="threshold">The percent [0, 100] deviation of the ratio from the mean before emitting an insight</param>
43  /// <param name="minimumCorrelation">The minimum correlation to consider a tradable pair</param>
44  public PearsonCorrelationPairsTradingAlphaModel(int lookback = 15, Resolution resolution = Resolution.Minute, decimal threshold = 1m, double minimumCorrelation = .5)
45  : base(lookback, resolution, threshold)
46  {
47  _lookback = lookback;
48  _resolution = resolution;
49  _minimumCorrelation = minimumCorrelation;
50  }
51 
52  /// <summary>
53  /// Event fired each time the we add/remove securities from the data feed
54  /// </summary>
55  /// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
56  /// <param name="changes">The security additions and removals from the algorithm</param>
57  public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
58  {
60 
61  var symbols = Securities.Select(x => x.Symbol).ToArray();
62 
63  var history = algorithm.History(symbols, _lookback, _resolution);
64 
65  var vectors = GetPriceVectors(history);
66 
67  if (vectors.LongLength == 0)
68  {
69  algorithm.Debug($"PearsonCorrelationPairsTradingAlphaModel.OnSecuritiesChanged(): The requested historical data does not have series of prices with the same date/time. Please consider increasing the looback period. Current lookback: {_lookback}");
70  }
71  else
72  {
73  var pearsonMatrix = Correlation.PearsonMatrix(vectors).UpperTriangle();
74 
75  var maxValue = pearsonMatrix.Enumerate().Where(x => Math.Abs(x) < 1).Max();
76  if (maxValue >= _minimumCorrelation)
77  {
78  var maxTuple = pearsonMatrix.Find(x => x == maxValue);
79  _bestPair = Tuple.Create(symbols[maxTuple.Item1], symbols[maxTuple.Item2]);
80  }
81  }
82 
83  base.OnSecuritiesChanged(algorithm, changes);
84  }
85 
86  /// <summary>
87  /// Check whether the assets pass a pairs trading test
88  /// </summary>
89  /// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
90  /// <param name="asset1">The first asset's symbol in the pair</param>
91  /// <param name="asset2">The second asset's symbol in the pair</param>
92  /// <returns>True if the statistical test for the pair is successful</returns>
93  public override bool HasPassedTest(QCAlgorithm algorithm, Symbol asset1, Symbol asset2)
94  {
95  return _bestPair != null && asset1 == _bestPair.Item1 && asset2 == _bestPair.Item2;
96  }
97 
98  private double[][] GetPriceVectors(IEnumerable<Slice> slices)
99  {
100  var symbols = Securities.Select(x => x.Symbol).ToArray();
101  var timeZones = Securities.ToDictionary(x => x.Symbol, y => y.Exchange.TimeZone);
102 
103  // Special case: daily data and securities from different timezone
104  var isDailyAndMultipleTimeZone = _resolution == Resolution.Daily && timeZones.Values.Distinct().Count() > 1;
105 
106  var bars = new List<BaseData>();
107 
108  if (isDailyAndMultipleTimeZone)
109  {
110  bars.AddRange(slices
111  .GroupBy(x => x.Time.Date)
112  .Where(x => x.Sum(k => k.Count) == symbols.Length)
113  .SelectMany(x => x.SelectMany(y => y.Values)));
114  }
115  else
116  {
117  bars.AddRange(slices
118  .Where(x => x.Count == symbols.Length)
119  .SelectMany(x => x.Values));
120  }
121 
122  return bars
123  .GroupBy(x => x.Symbol)
124  .Select(x =>
125  {
126  var array = x.Select(b => Math.Log((double)b.Price)).ToArray();
127  if (array.Length > 1)
128  {
129  for (var i = array.Length - 1; i > 0; i--)
130  {
131  array[i] = array[i] - array[i - 1];
132  }
133  array[0] = array[1];
134  return array;
135  }
136  else
137  {
138  return new double[0];
139  }
140  }).ToArray();
141  }
142  }
143 }