17 using System.Collections.Generic;
28 private readonly
Symbol _symbol;
46 _roc =
new RateOfChange($
"{_symbol}.ROC({lookback})", lookback);
48 _roc.Updated += OnRateOfChangeUpdated;
54 public Dictionary<DateTime, double>
Returns => _window.ToDictionary(x => x.EndTime, x => (
double) x.Value);
61 public void Add(DateTime time, decimal value)
74 public bool Update(DateTime time, decimal value)
76 return _roc.Update(time, value);
84 _roc.Updated -= OnRateOfChangeUpdated;
104 if (_window.Samples > 0 && _window[0].EndTime == updated.
EndTime)
110 _window.Add(updated);
124 public static double[,]
FormReturnsMatrix(
this Dictionary<Symbol, ReturnsSymbolData> symbolData, IEnumerable<Symbol> symbols)
126 var returnsByDate = (from s in symbols join sd in symbolData on s equals sd.Key select sd.Value.Returns).ToList();
129 var alldates = returnsByDate.SelectMany(r => r.Keys).Distinct().ToList();
131 var max = symbolData.Count == 0 ? 0 : symbolData.Max(kvp => kvp.Value.Returns.Count);
134 if (max == alldates.Count)
136 return Accord.Math.Matrix.Create(alldates
138 .Select(d => returnsByDate.Select(s => s.GetValueOrDefault(d,
double.NaN)).ToArray())
139 .Where(r => !r.Select(Math.Abs).Sum().IsNaNOrZero())
144 var returnsByIndex = returnsByDate.Select((doubles, i) => doubles.Values.ToArray());
146 return Accord.Math.Matrix.Create(Enumerable.Range(0, max)
148 .Select(d => returnsByIndex.Select(s => s.Length < (d + 1) ?
double.NaN : s[d]).ToArray())
149 .Where(r => !r.Select(Math.Abs).Sum().IsNaNOrZero())