Exemple #1
0
        /// <summary>
        /// Load the history provider from the Composer
        /// </summary>
        private HistoryProviderManager GetHistoryProvider()
        {
            var provider = new HistoryProviderManager();

            provider.InvalidConfigurationDetected += (sender, args) => { AlgorithmHandlers.Results.ErrorMessage(args.Message); };
            provider.NumericalPrecisionLimited    += (sender, args) =>
            {
                if (!_historyNumericalPrecisionLimitedWarningEmitted)
                {
                    _historyNumericalPrecisionLimitedWarningEmitted = true;
                    AlgorithmHandlers.Results.DebugMessage("Warning: when performing history requests, the start date will be adjusted if there are numerical precision errors in the factor files.");
                }
            };
            provider.StartDateLimited += (sender, args) =>
            {
                if (!_historyStartDateLimitedWarningEmitted)
                {
                    _historyStartDateLimitedWarningEmitted = true;
                    AlgorithmHandlers.Results.DebugMessage("Warning: when performing history requests, the start date will be adjusted if it is before the first known date for the symbol.");
                }
            };
            provider.DownloadFailed      += (sender, args) => { AlgorithmHandlers.Results.ErrorMessage(args.Message, args.StackTrace); };
            provider.ReaderErrorDetected += (sender, args) => { AlgorithmHandlers.Results.RuntimeError(args.Message, args.StackTrace); };

            return(provider);
        }
Exemple #2
0
        /// <summary>
        /// <see cref = "QuantBook" /> constructor.
        /// Provides access to data for quantitative analysis
        /// </summary>
        public QuantBook() : base()
        {
            try
            {
                using (Py.GIL())
                {
                    _pandas = Py.Import("pandas");
                }

                // Issue #4892 : Set start time relative to NY time
                // when the data is available from the previous day
                var newYorkTime   = DateTime.UtcNow.ConvertFromUtc(TimeZones.NewYork);
                var hourThreshold = Config.GetInt("qb-data-hour", 9);

                // If it is after our hour threshold; then we can use today
                if (newYorkTime.Hour >= hourThreshold)
                {
                    SetStartDate(newYorkTime);
                }
                else
                {
                    SetStartDate(newYorkTime - TimeSpan.FromDays(1));
                }

                // Sets PandasConverter
                SetPandasConverter();

                // Reset our composer; needed for re-creation of QuantBook
                Composer.Instance.Reset();
                var composer = Composer.Instance;

                // Create our handlers with our composer instance
                var algorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(composer);
                var systemHandlers    = LeanEngineSystemHandlers.FromConfiguration(composer);

                // init the API
                systemHandlers.Initialize();
                systemHandlers.LeanManager.Initialize(systemHandlers,
                                                      algorithmHandlers,
                                                      new BacktestNodePacket(),
                                                      new AlgorithmManager(false));
                systemHandlers.LeanManager.SetAlgorithm(this);

                algorithmHandlers.DataPermissionsManager.Initialize(new AlgorithmNodePacket(PacketType.BacktestNode)
                {
                    UserToken      = Config.Get("api-access-token"),
                    UserId         = Config.GetInt("job-user-id"),
                    ProjectId      = Config.GetInt("project-id"),
                    OrganizationId = Config.Get("job-organization-id"),
                    Version        = Globals.Version
                });

                algorithmHandlers.ObjectStore.Initialize(Config.Get("research-object-store-name", "QuantBook"),
                                                         Config.GetInt("job-user-id"),
                                                         Config.GetInt("project-id"),
                                                         Config.Get("api-access-token"),
                                                         new Controls
                {
                    // if <= 0 we disable periodic persistence and make it synchronous
                    PersistenceIntervalSeconds = -1,
                    StorageLimitMB             = Config.GetInt("storage-limit-mb", 5),
                    StorageFileCount           = Config.GetInt("storage-file-count", 100),
                    StoragePermissions         = (FileAccess)Config.GetInt("storage-permissions", (int)FileAccess.ReadWrite)
                });
                SetObjectStore(algorithmHandlers.ObjectStore);

                _dataCacheProvider = new ZipDataCacheProvider(algorithmHandlers.DataProvider);
                _dataProvider      = algorithmHandlers.DataProvider;

                var symbolPropertiesDataBase = SymbolPropertiesDatabase.FromDataFolder();
                var registeredTypes          = new RegisteredSecurityDataTypesProvider();
                var securityService          = new SecurityService(Portfolio.CashBook,
                                                                   MarketHoursDatabase,
                                                                   symbolPropertiesDataBase,
                                                                   this,
                                                                   registeredTypes,
                                                                   new SecurityCacheProvider(Portfolio));
                Securities.SetSecurityService(securityService);
                SubscriptionManager.SetDataManager(
                    new DataManager(new NullDataFeed(),
                                    new UniverseSelection(this, securityService, algorithmHandlers.DataPermissionsManager, algorithmHandlers.DataProvider),
                                    this,
                                    TimeKeeper,
                                    MarketHoursDatabase,
                                    false,
                                    registeredTypes,
                                    algorithmHandlers.DataPermissionsManager));

                var mapFileProvider = algorithmHandlers.MapFileProvider;
                HistoryProvider = new HistoryProviderManager();
                HistoryProvider.Initialize(
                    new HistoryProviderInitializeParameters(
                        null,
                        null,
                        algorithmHandlers.DataProvider,
                        _dataCacheProvider,
                        mapFileProvider,
                        algorithmHandlers.FactorFileProvider,
                        null,
                        true,
                        algorithmHandlers.DataPermissionsManager
                        )
                    );

                SetOptionChainProvider(new CachingOptionChainProvider(new BacktestingOptionChainProvider(_dataCacheProvider, mapFileProvider)));
                SetFutureChainProvider(new CachingFutureChainProvider(new BacktestingFutureChainProvider(_dataCacheProvider)));
            }
            catch (Exception exception)
            {
                throw new Exception("QuantBook.Main(): " + exception);
            }
        }