namespace StockSharp.Algo;
///
/// The supplier of information on instruments, getting data from the collection.
///
public class CollectionSecurityProvider : ISecurityProvider
{
private readonly SynchronizedDictionary _inner = [];
///
/// Initializes a new instance of the .
///
public CollectionSecurityProvider()
{
}
///
/// Initializes a new instance of the .
///
/// The instruments collection.
public CollectionSecurityProvider(IEnumerable securities)
=> AddRange(securities);
///
public int Count => _inner.Count;
private Action> _added;
event Action> ISecurityProvider.Added
{
add => _added += value;
remove => _added -= value;
}
private Action> _removed;
event Action> ISecurityProvider.Removed
{
add => _removed += value;
remove => _removed -= value;
}
private Action _cleared;
event Action ISecurityProvider.Cleared
{
add => _cleared += value;
remove => _cleared -= value;
}
///
public Security LookupById(SecurityId id) => _inner.TryGetValue(id);
///
public IEnumerable Lookup(SecurityLookupMessage criteria) => _inner.SyncGet(d => d.Values.Filter(criteria));
SecurityMessage ISecurityMessageProvider.LookupMessageById(SecurityId id)
=> LookupById(id)?.ToMessage();
IEnumerable ISecurityMessageProvider.LookupMessages(SecurityLookupMessage criteria)
=> Lookup(criteria).Select(s => s.ToMessage());
///
/// Add security.
///
/// Security.
public void Add(Security security)
{
if (_inner.TryAdd2(security.ToSecurityId(), security))
_added?.Invoke([security]);
}
///
/// Remove security.
///
/// Security.
/// Check result.
public bool Remove(Security security)
{
if (security is null)
throw new ArgumentNullException(nameof(security));
RemoveRange([security]);
return true;
}
///
/// Add securities.
///
/// Securities.
public void AddRange(IEnumerable securities)
{
if (securities is null)
throw new ArgumentNullException(nameof(securities));
var added = new HashSet(securities);
foreach (var security in securities)
{
if (!_inner.TryAdd2(security.ToSecurityId(), security))
added.Remove(security);
}
_added?.Invoke(added);
}
///
/// Remove securities.
///
/// Securities.
public void RemoveRange(IEnumerable securities)
{
if (securities is null)
throw new ArgumentNullException(nameof(securities));
var removed = new HashSet(securities);
foreach (var security in securities)
{
if (!_inner.Remove(security.ToSecurityId()))
removed.Remove(security);
}
_removed?.Invoke(removed);
}
///
/// Clear.
///
public void Clear()
{
_inner.Clear();
_cleared?.Invoke();
}
}