17 using System.Collections.Generic;
36 private readonly
int _lookback;
37 private readonly
int _period;
40 private readonly Dictionary<Symbol, ReturnsSymbolData> _symbolDataDict;
57 : this(rebalancingDateRules.ToFunc(), portfolioBias, lookback, period, resolution, optimizer)
76 : this(rebalanceResolution.ToTimeSpan(), portfolioBias, lookback, period, resolution, optimizer)
95 : this(dt => dt.Add(timeSpan), portfolioBias, lookback, period, resolution, optimizer)
120 : this((Func<DateTime, DateTime?>)null, portfolioBias, lookback, period, resolution, optimizer)
141 : this(rebalancingFunc != null ? (Func<DateTime, DateTime?>)(timeUtc => rebalancingFunc(timeUtc)) : null,
167 : base(rebalancingFunc)
171 throw new ArgumentException(
"Long position must be allowed in RiskParityPortfolioConstructionModel.");
174 _lookback = lookback;
176 _resolution = resolution;
180 _symbolDataDict =
new Dictionary<Symbol, ReturnsSymbolData>();
190 var targets =
new Dictionary<Insight, double>();
193 if (activeInsights.IsNullOrEmpty())
198 var symbols = activeInsights.Select(x => x.Symbol).ToList();
201 var returns = _symbolDataDict.FormReturnsMatrix(symbols);
204 var w = _optimizer.Optimize(returns);
210 foreach (var symbol
in symbols)
212 var weight = w[sidx];
213 targets[activeInsights.First(insight => insight.Symbol == symbol)] = weight;
229 base.OnSecuritiesChanged(algorithm, changes);
231 foreach (var removed
in changes.RemovedSecurities)
233 _symbolDataDict.Remove(removed.Symbol, out var removedSymbolData);
234 algorithm.UnregisterIndicator(removedSymbolData.ROC);
237 if (changes.AddedSecurities.Count == 0)
243 foreach (var added
in changes.AddedSecurities)
245 if (!_symbolDataDict.ContainsKey(added.Symbol))
248 _symbolDataDict[added.Symbol] = symbolData;
249 algorithm.RegisterIndicator(added.Symbol, symbolData.ROC, _resolution);
254 algorithm.History(changes.AddedSecurities.Select(security => security.Symbol), _lookback * _period, _resolution)
258 if (_symbolDataDict.TryGetValue(bar.Symbol, out symbolData))
260 symbolData.Update(bar.EndTime, bar.Value);