18 using System.Collections;
20 using System.Collections.Generic;
29 private List<IPortfolioTarget> _enumerable;
30 private List<KeyValuePair<Symbol, IPortfolioTarget>> _kvpEnumerable;
31 private readonly Dictionary<Symbol, IPortfolioTarget> _targets =
new ();
42 return _targets.Count;
56 return _targets.Count == 0;
69 public ICollection<Symbol>
Keys
75 return _targets.Keys.ToList();
84 public ICollection<IPortfolioTarget>
Values
88 var result = _enumerable;
93 result = _enumerable = _targets.Values.ToList();
115 _kvpEnumerable =
null;
116 _targets[target.
Symbol] = target;
125 public void Add(KeyValuePair<Symbol, IPortfolioTarget> target)
146 public void AddRange(IEnumerable<IPortfolioTarget> targets)
151 _kvpEnumerable =
null;
152 foreach (var item
in targets)
154 _targets[item.Symbol] = item;
166 AddRange((IEnumerable<IPortfolioTarget>)targets);
177 _kvpEnumerable =
null;
188 foreach (var target
in this)
190 var security = algorithm.Securities[target.Symbol];
191 var holdings = security.Holdings.Quantity;
193 if (Math.Abs(target.Quantity - holdings) < security.SymbolProperties.LotSize)
216 return _targets.ContainsKey(target.
Symbol);
225 public bool Contains(KeyValuePair<Symbol, IPortfolioTarget> target)
239 return _targets.ContainsKey(symbol);
252 _targets.Values.CopyTo(array, arrayIndex);
261 public void CopyTo(KeyValuePair<Symbol, IPortfolioTarget>[] array,
int arrayIndex)
263 WithDictionary(d => d.CopyTo(array, arrayIndex));
275 if (_targets.Remove(symbol))
278 _kvpEnumerable =
null;
290 public bool Remove(KeyValuePair<Symbol, IPortfolioTarget> target)
292 return Remove(target.Value);
310 if (_targets.TryGetValue(target.
Symbol, out existing))
313 if (existing.Equals(target))
333 return _targets.TryGetValue(symbol, out target);
348 return _targets[symbol];
356 _kvpEnumerable =
null;
357 _targets[symbol] = value;
366 IEnumerator<KeyValuePair<Symbol, IPortfolioTarget>> IEnumerable<KeyValuePair<Symbol, IPortfolioTarget>>.GetEnumerator()
368 var result = _kvpEnumerable;
373 _kvpEnumerable = result = _targets.ToList();
376 return result.GetEnumerator();
386 var result = _enumerable;
391 _enumerable = result = _targets.Values.ToList();
394 return result.GetEnumerator();
403 IEnumerator IEnumerable.GetEnumerator()
405 return GetEnumerator();
411 private void WithDictionary(Action<IDictionary<Symbol, IPortfolioTarget>> action)
432 return this.OrderTargetsByMarginImpact(algorithm);