17 using System.Collections.Generic;
39 private readonly Dictionary<Symbol, string> _sectorCodeBySymbol =
new Dictionary<Symbol, string>();
47 : base(rebalancingDateRules.ToFunc())
58 : base(rebalancingFunc)
68 : this(rebalancingFunc != null ? (Func<DateTime, DateTime?>)(timeUtc => rebalancingFunc(timeUtc)) : null)
83 : this((Func<DateTime, DateTime?>)null)
93 : this(dt => dt.Add(timeSpan))
102 : this(resolution.ToTimeSpan())
114 return _sectorCodeBySymbol.ContainsKey(insight.
Symbol);
124 var result =
new Dictionary<Insight, double>();
126 var insightBySectorCode =
new Dictionary<string, List<Insight>>();
128 foreach (var insight
in activeInsights)
136 List<Insight> insights;
137 var sectorCode = _sectorCodeBySymbol[insight.Symbol];
138 if (insightBySectorCode.TryGetValue(sectorCode, out insights))
140 insights.Add(insight);
144 insightBySectorCode[sectorCode] =
new List<Insight> { insight };
149 var sectorPercent = insightBySectorCode.Count == 0 ? 0 : 1m / insightBySectorCode.Count;
151 foreach (var kvp
in insightBySectorCode)
153 var insights = kvp.Value;
156 var count = insights.Count;
157 var percent = count == 0 ? 0 : sectorPercent / count;
159 foreach (var insight
in insights)
161 result[insight] = (double)((
int)insight.Direction * percent);
175 foreach (var security
in changes.RemovedSecurities)
179 var symbol = security.Symbol;
180 if (_sectorCodeBySymbol.ContainsKey(symbol))
182 _sectorCodeBySymbol.Remove(symbol);
186 foreach (var security
in changes.AddedSecurities)
189 if (!
string.IsNullOrEmpty(sectorCode))
191 _sectorCodeBySymbol[security.Symbol] = sectorCode;
194 base.OnSecuritiesChanged(algorithm, changes);