18 using System.Collections.Concurrent;
31 private static Cache _cache =
new Cache();
38 public static void Set(
string ticker,
Symbol symbol)
40 _cache.Symbols[ticker] = symbol;
41 _cache.Tickers[symbol] = ticker;
52 if (result.Item3 !=
null)
72 symbol = result.Item2;
88 return _cache.Tickers.TryGetValue(symbol, out ticker) ? ticker : symbol.
ID.
ToString();
99 return _cache.Tickers.TryGetValue(symbol, out ticker);
110 return _cache.Tickers.TryRemove(symbol, out ticker) && _cache.Symbols.TryRemove(ticker, out symbol);
121 return _cache.Symbols.TryRemove(ticker, out symbol) && _cache.Tickers.TryRemove(symbol, out ticker);
129 _cache =
new Cache();
132 private static Tuple<bool, Symbol, InvalidOperationException>
TryGetSymbol(
string ticker)
135 InvalidOperationException error =
null;
136 if (!_cache.TryGetSymbol(ticker, out symbol))
140 var search = $
"{ticker.ToUpperInvariant()}.";
141 var match = _cache.Symbols.Where(kvp => kvp.Key.StartsWith(search)).ToList();
143 if (match.Count == 0)
148 else if (match.Count == 1)
151 symbol = match.Single().Value;
153 else if (match.Count > 1)
156 error =
new InvalidOperationException(
157 Messages.SymbolCache.MultipleMatchingTickersLocated(match.Select(kvp => kvp.Key)));
161 return Tuple.Create(symbol !=
null, symbol, error);
166 public readonly ConcurrentDictionary<string, Symbol> Symbols =
new ConcurrentDictionary<string, Symbol>(StringComparer.OrdinalIgnoreCase);
167 public readonly ConcurrentDictionary<Symbol, string> Tickers =
new ConcurrentDictionary<Symbol, string>();
176 public bool TryGetSymbol(
string ticker, out Symbol symbol)
178 if (Symbols.TryGetValue(ticker, out symbol))
182 SecurityIdentifier sid;
183 if (SecurityIdentifier.TryParse(ticker, out sid))
185 symbol =
new Symbol(sid, sid.Symbol);