17 using System.Collections.Generic;
30 private static readonly
object DataFolderSymbolPropertiesDatabaseLock =
new object();
32 private Dictionary<SecurityDatabaseKey, SymbolProperties> _entries;
33 private readonly Dictionary<SecurityDatabaseKey, SymbolProperties> _customEntries;
34 private IReadOnlyDictionary<SecurityDatabaseKey, SecurityDatabaseKey> _keyBySecurityType;
42 var allEntries =
new Dictionary<SecurityDatabaseKey, SymbolProperties>();
43 var entriesBySecurityType =
new Dictionary<SecurityDatabaseKey, SecurityDatabaseKey>();
45 foreach (var keyValuePair
in FromCsvFile(file))
47 if (allEntries.ContainsKey(keyValuePair.Key))
54 if (!entriesBySecurityType.ContainsKey(key))
56 entriesBySecurityType[key] = keyValuePair.Key;
58 allEntries[keyValuePair.Key] = keyValuePair.Value;
61 _entries = allEntries;
62 _customEntries =
new();
63 _keyBySecurityType = entriesBySecurityType;
75 return _entries.ContainsKey(key);
103 if (_keyBySecurityType.TryGetValue(key, out result))
105 market = result.Market;
128 if (!_entries.TryGetValue(key, out symbolProperties))
137 if (_entries.TryGetValue(key, out symbolProperties))
139 return symbolProperties;
144 if (!_entries.TryGetValue(
new SecurityDatabaseKey(market,
null, securityType), out symbolProperties))
151 return symbolProperties;
162 foreach (var entry
in _entries)
165 var symbolProperties = entry.Value;
167 if (key.Market == market && key.SecurityType == securityType)
169 yield
return new KeyValuePair<SecurityDatabaseKey, SymbolProperties>(key, symbolProperties);
181 foreach (var entry
in _entries)
184 var symbolProperties = entry.Value;
186 if (key.Market == market)
188 yield
return new KeyValuePair<SecurityDatabaseKey, SymbolProperties>(key, symbolProperties);
204 lock (DataFolderSymbolPropertiesDatabaseLock)
206 _entries[key] = properties;
207 _customEntries[key] = properties;
219 lock (DataFolderSymbolPropertiesDatabaseLock)
221 if (_dataFolderSymbolPropertiesDatabase ==
null)
226 return _dataFolderSymbolPropertiesDatabase;
234 private static IEnumerable<KeyValuePair<SecurityDatabaseKey, SymbolProperties>> FromCsvFile(
string file)
236 if (!File.Exists(file))
242 foreach (var line
in File.ReadLines(file).Where(x => !x.StartsWith(
"#") && !
string.IsNullOrWhiteSpace(x)).Skip(1))
244 SecurityDatabaseKey key;
246 if (key ==
null || entry ==
null)
251 yield
return new KeyValuePair<SecurityDatabaseKey, SymbolProperties>(key, entry);
263 var csv = line.Split(
',');
266 if (!csv[2].TryParseSecurityType(out securityType))
275 securityType: securityType);
279 quoteCurrency: csv[4],
280 contractMultiplier: csv[5].ToDecimal(),
281 minimumPriceVariation: csv[6].ToDecimalAllowExponent(),
282 lotSize: csv[7].ToDecimal(),
283 marketTicker: HasValidValue(csv, 8) ? csv[8] :
string.Empty,
284 minimumOrderSize: HasValidValue(csv, 9) ? csv[9].ToDecimal() :
null,
285 priceMagnifier: HasValidValue(csv, 10) ? csv[10].ToDecimal() : 1,
286 strikeMultiplier: HasValidValue(csv, 11) ? csv[11].ToDecimal() : 1);
289 private static bool HasValidValue(
string[] array, uint position)
291 return array.Length > position && !
string.IsNullOrEmpty(array[position]);
297 internal void ReloadEntries()
299 lock (DataFolderSymbolPropertiesDatabaseLock)
301 _dataFolderSymbolPropertiesDatabase =
null;
303 var fileEntries = newInstance._entries.Where(x => !_customEntries.ContainsKey(x.Key));
304 var newEntries = fileEntries.Concat(_customEntries).ToDictionary();
305 _entries = newEntries;
306 _keyBySecurityType = newInstance._keyBySecurityType.ToReadOnlyDictionary();