22 using System.Collections.Generic;
39 private bool _isLiveMode;
40 private bool _modelsMismatchWarningSent;
55 _registeredTypes = registeredTypes;
56 _marketHoursDatabase = marketHoursDatabase;
57 _symbolPropertiesDatabase = symbolPropertiesDatabase;
58 _securityInitializerProvider = securityInitializerProvider;
59 _cacheProvider = cacheProvider;
60 _primaryExchangeProvider = primaryExchangeProvider;
61 _algorithm = algorithm;
70 List<SubscriptionDataConfig> subscriptionDataConfigList,
72 bool addToSymbolCache =
true,
76 configList.AddRange(subscriptionDataConfigList);
78 var dataTypes = Enumerable.Empty<Type>();
81 dataTypes =
new[] { type };
83 var exchangeHours = _marketHoursDatabase.GetEntry(symbol, dataTypes).ExchangeHours;
85 var defaultQuoteCurrency = _cashBook.AccountCurrency;
88 defaultQuoteCurrency = symbol.
Value.Substring(3);
97 var symbolProperties = _symbolPropertiesDatabase.GetSymbolProperties(
101 defaultQuoteCurrency);
104 if (addToSymbolCache)
110 var quoteCurrency = symbolProperties.QuoteCurrency;
111 if (!_cashBook.TryGetValue(quoteCurrency, out var quoteCash))
114 quoteCash = _cashBook.Add(quoteCurrency, 0, 0);
117 Cash baseCash =
null;
121 if (!_cashBook.TryGetValue(baseCurrencySymbol, out baseCash))
124 baseCash = _cashBook.Add(baseCurrencySymbol, 0, 0);
128 var cache = _cacheProvider.GetSecurityCache(symbol);
134 var primaryExchange =
135 _primaryExchangeProvider?.GetPrimaryExchange(symbol.
ID) ??
137 security =
new Equity.Equity(symbol, exchangeHours, quoteCash, symbolProperties, _cashBook, _registeredTypes, cache, primaryExchange);
142 security =
new Option.Option(symbol, exchangeHours, quoteCash,
new Option.OptionSymbolProperties(symbolProperties), _cashBook, _registeredTypes, cache, underlying);
147 security =
new IndexOption.IndexOption(symbol, exchangeHours, quoteCash,
new IndexOption.IndexOptionSymbolProperties(symbolProperties), _cashBook, _registeredTypes, cache, underlying);
152 var optionSymbolProperties =
new Option.OptionSymbolProperties(symbolProperties);
156 optionSymbolProperties.SetContractUnitOfTrade(1);
158 security =
new FutureOption.FutureOption(symbol, exchangeHours, quoteCash, optionSymbolProperties, _cashBook, _registeredTypes, cache, underlying);
162 security =
new Future.Future(symbol, exchangeHours, quoteCash, symbolProperties, _cashBook, _registeredTypes, cache, underlying);
166 security =
new Forex.Forex(symbol, exchangeHours, quoteCash, baseCash, symbolProperties, _cashBook, _registeredTypes, cache);
170 security =
new Cfd.Cfd(symbol, exchangeHours, quoteCash, symbolProperties, _cashBook, _registeredTypes, cache);
174 security =
new Index.Index(symbol, exchangeHours, quoteCash, symbolProperties, _cashBook, _registeredTypes, cache);
178 security =
new Crypto.Crypto(symbol, exchangeHours, quoteCash, baseCash, symbolProperties, _cashBook, _registeredTypes, cache);
182 security =
new CryptoFuture.CryptoFuture(symbol, exchangeHours, quoteCash, baseCash, symbolProperties, _cashBook, _registeredTypes, cache);
187 security =
new Security(symbol, exchangeHours, quoteCash, symbolProperties, _cashBook, _registeredTypes, cache);
195 security.
IsTradable = !configList.IsInternalFeed;
198 security.AddData(configList);
201 _securityInitializerProvider.SecurityInitializer.Initialize(security);
203 CheckCanonicalSecurityModels(security);
215 if ((_isLiveMode || isNotNormalized) && security.
Type ==
SecurityType.Equity)
230 return CreateSecurity(symbol,
new List<SubscriptionDataConfig> { subscriptionDataConfig }, leverage, addToSymbolCache, underlying);
239 _isLiveMode = isLiveMode;
246 private void CheckCanonicalSecurityModels(
Security security)
248 if (!_modelsMismatchWarningSent &&
249 _algorithm !=
null &&
251 _algorithm.Securities.TryGetValue(security.
Symbol.
Canonical, out var canonicalSecurity))
253 if (security.
FillModel.GetType() != canonicalSecurity.FillModel.GetType() ||
254 security.
FeeModel.GetType() != canonicalSecurity.FeeModel.GetType() ||
255 security.
BuyingPowerModel.GetType() != canonicalSecurity.BuyingPowerModel.GetType() ||
257 security.
SlippageModel.GetType() != canonicalSecurity.SlippageModel.GetType() ||
258 security.
VolatilityModel.GetType() != canonicalSecurity.VolatilityModel.GetType() ||
259 security.
SettlementModel.GetType() != canonicalSecurity.SettlementModel.GetType())
261 _modelsMismatchWarningSent =
true;
262 _algorithm.Debug($
"Warning: Security {security.Symbol} its canonical security {security.Symbol.Canonical} have at least one model of different types (fill, fee, buying power, margin interest rate, slippage, volatility, settlement). To avoid this, consider using a security initializer to set the right models to each security type according to your algorithm's requirements.");