/*
* 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 MathNet.Numerics.Statistics;
using QuantConnect.Data;
using QuantConnect.Data.UniverseSelection;
using System;
using System.Collections.Generic;
using System.Linq;
namespace QuantConnect.Algorithm.Framework.Alphas
{
///
/// This alpha model is designed to rank every pair combination by its pearson correlation
/// and trade the pair with the hightest correlation
/// This model generates alternating long ratio/short ratio insights emitted as a group
///
public class PearsonCorrelationPairsTradingAlphaModel : BasePairsTradingAlphaModel
{
private readonly int _lookback;
private readonly Resolution _resolution;
private readonly double _minimumCorrelation;
private Tuple _bestPair;
///
/// Initializes a new instance of the class
///
/// Lookback period of the analysis
/// Analysis resolution
/// The percent [0, 100] deviation of the ratio from the mean before emitting an insight
/// The minimum correlation to consider a tradable pair
public PearsonCorrelationPairsTradingAlphaModel(int lookback = 15, Resolution resolution = Resolution.Minute, decimal threshold = 1m, double minimumCorrelation = .5)
: base(lookback, resolution, threshold)
{
_lookback = lookback;
_resolution = resolution;
_minimumCorrelation = minimumCorrelation;
}
///
/// Event fired each time the we add/remove securities from the data feed
///
/// The algorithm instance that experienced the change in securities
/// The security additions and removals from the algorithm
public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
{
NotifiedSecurityChanges.UpdateCollection(Securities, changes);
var symbols = Securities.Select(x => x.Symbol).ToArray();
var history = algorithm.History(symbols, _lookback, _resolution);
var vectors = GetPriceVectors(history);
if (vectors.LongLength == 0)
{
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}");
}
else
{
var pearsonMatrix = Correlation.PearsonMatrix(vectors).UpperTriangle();
var maxValue = pearsonMatrix.Enumerate().Where(x => Math.Abs(x) < 1).Max();
if (maxValue >= _minimumCorrelation)
{
var maxTuple = pearsonMatrix.Find(x => x == maxValue);
_bestPair = Tuple.Create(symbols[maxTuple.Item1], symbols[maxTuple.Item2]);
}
}
base.OnSecuritiesChanged(algorithm, changes);
}
///
/// Check whether the assets pass a pairs trading test
///
/// The algorithm instance that experienced the change in securities
/// The first asset's symbol in the pair
/// The second asset's symbol in the pair
/// True if the statistical test for the pair is successful
public override bool HasPassedTest(QCAlgorithm algorithm, Symbol asset1, Symbol asset2)
{
return _bestPair != null && asset1 == _bestPair.Item1 && asset2 == _bestPair.Item2;
}
private double[][] GetPriceVectors(IEnumerable slices)
{
var symbols = Securities.Select(x => x.Symbol).ToArray();
var timeZones = Securities.ToDictionary(x => x.Symbol, y => y.Exchange.TimeZone);
// Special case: daily data and securities from different timezone
var isDailyAndMultipleTimeZone = _resolution == Resolution.Daily && timeZones.Values.Distinct().Count() > 1;
var bars = new List();
if (isDailyAndMultipleTimeZone)
{
bars.AddRange(slices
.GroupBy(x => x.Time.Date)
.Where(x => x.Sum(k => k.Count) == symbols.Length)
.SelectMany(x => x.SelectMany(y => y.Values)));
}
else
{
bars.AddRange(slices
.Where(x => x.Count == symbols.Length)
.SelectMany(x => x.Values));
}
return bars
.GroupBy(x => x.Symbol)
.Select(x =>
{
var array = x.Select(b => Math.Log((double)b.Price)).ToArray();
if (array.Length > 1)
{
for (var i = array.Length - 1; i > 0; i--)
{
array[i] = array[i] - array[i - 1];
}
array[0] = array[1];
return array;
}
else
{
return new double[0];
}
}).ToArray();
}
}
}