Example #1
0
        /// <summary>
        /// Converts a SecurityDefinition to a <see cref="Symbol" />
        /// </summary>
        /// <param name="securityDefinition">Security definition</param>
        /// <param name="tradingDate">
        /// The date that the stock was being traded. This is used to resolve
        /// the ticker that the stock was trading under on this date.
        /// </param>
        /// <returns>Symbol if matching Lean Symbol was found on the trading date, null otherwise</returns>
        private Symbol SecurityDefinitionToSymbol(SecurityDefinition securityDefinition, DateTime tradingDate)
        {
            if (securityDefinition == null)
            {
                return(null);
            }

            var market          = securityDefinition.SecurityIdentifier.Market;
            var mapFileResolver = _mapFileProvider.Get(market);

            // Get the first ticker the symbol traded under, and then lookup the
            // trading date to get the ticker on the trading date.
            var mapFile = mapFileResolver
                          .ResolveMapFile(securityDefinition.SecurityIdentifier.Symbol, securityDefinition.SecurityIdentifier.Date);

            // The mapped ticker will be null if the map file is null or there's
            // no entry found for the given trading date.
            var mappedTicker = mapFile?.GetMappedSymbol(tradingDate, null);

            // If we're null, then try again; get the last entry of the map file and use
            // it as the Symbol we return to the caller.
            mappedTicker ??= mapFile?
            .LastOrDefault()?
            .MappedSymbol;

            return(string.IsNullOrWhiteSpace(mappedTicker)
                ? null
                : new Symbol(securityDefinition.SecurityIdentifier, mappedTicker));
        }
Example #2
0
        /// <summary>
        /// Get's the security definitions using a lazy initialization
        /// </summary>
        private IEnumerable <SecurityDefinition> GetSecurityDefinitions()
        {
            if (_securityDefinitions != null)
            {
                return(_securityDefinitions);
            }

            if (!SecurityDefinition.TryRead(_dataProvider, _securitiesDefinitionKey, out _securityDefinitions))
            {
                _securityDefinitions = new List <SecurityDefinition>();
                Log.Error($"SecurityDefinitionSymbolResolver(): No security definitions data loaded from file: {_securitiesDefinitionKey}");
            }
            return(_securityDefinitions);
        }
Example #3
0
        /// <summary>
        /// Creates an instance of the symbol resolver
        /// </summary>
        /// <param name="dataProvider">Data provider used to obtain symbol mappings data</param>
        /// <param name="securitiesDefinitionKey">Location to read the securities definition data from</param>
        public SecurityDefinitionSymbolResolver(IDataProvider dataProvider = null, string securitiesDefinitionKey = null)
        {
            securitiesDefinitionKey ??= Path.Combine(Globals.DataFolder, "symbol-properties", "security-database.csv");

            _dataProvider = dataProvider ??
                            Composer.Instance.GetExportedValueByTypeName <IDataProvider>(
                Config.Get("data-provider", "QuantConnect.Lean.Engine.DataFeeds.DefaultDataProvider"));

            if (!SecurityDefinition.TryRead(_dataProvider, securitiesDefinitionKey, out _securityDefinitions))
            {
                Log.Error($"SecurityDefinitionSymbolResolver(): No security definitions data loaded from file: {securitiesDefinitionKey}");
            }

            _mapFileProvider = Composer.Instance.GetExportedValueByTypeName <IMapFileProvider>(Config.Get("map-file-provider", "LocalDiskMapFileProvider"));
            _mapFileProvider.Initialize(_dataProvider);
        }