public void OptionExersiceWhenFullyInvested()
        {
            var algorithm = new AlgorithmStub();
            algorithm.SetFinishedWarmingUp();
            var backtestingTransactionHandler = new BacktestingTransactionHandler();
            algorithm.Transactions.SetOrderProcessor(backtestingTransactionHandler);
            backtestingTransactionHandler.Initialize(algorithm, new BacktestingBrokerage(algorithm), new TestResultHandler());

            const decimal price = 2600m;
            var time = new DateTime(2020, 10, 14);
            var expDate = new DateTime(2021, 3, 19);

            // For this symbol we dont have any history, but only one date and margins line
            var ticker = QuantConnect.Securities.Futures.Indices.SP500EMini;
            var future = Symbol.CreateFuture(ticker, Market.CME, expDate);
            var symbol = Symbol.CreateOption(future, Market.CME, OptionStyle.American, OptionRight.Call, 2550m,
                new DateTime(2021, 3, 19));

            var optionSecurity = algorithm.AddOptionContract(symbol);
            optionSecurity.Underlying = algorithm.AddFutureContract(future);

            optionSecurity.Underlying.SetMarketPrice(new Tick { Value = price, Time = time });
            optionSecurity.SetMarketPrice(new Tick { Value = 150, Time = time });
            optionSecurity.Holdings.SetHoldings(1.5m, 10);

            var request = optionSecurity.CreateDelistedSecurityOrderRequest(algorithm.UtcTime);

            var ticket = algorithm.Transactions.AddOrder(request);

            Assert.AreEqual(OrderStatus.Filled, ticket.Status);
        }
Exemplo n.º 2
0
        public void TestAlgorithmManagerSpeed()
        {
            var algorithmManager = new AlgorithmManager(false);
            var algorithm        = PerformanceBenchmarkAlgorithms.SingleSecurity_Second;
            var job          = new BacktestNodePacket(1, 2, "3", null, 9m, $"{nameof(AlgorithmManagerTests)}.{nameof(TestAlgorithmManagerSpeed)}");
            var feed         = new MockDataFeed();
            var transactions = new BacktestingTransactionHandler();
            var results      = new BacktestingResultHandler();
            var realtime     = new BacktestingRealTimeHandler();
            var leanManager  = new NullLeanManager();
            var alphas       = new NullAlphaHandler();
            var token        = new CancellationToken();

            algorithm.Initialize();
            results.Initialize(job, new QuantConnect.Messaging.Messaging(), new Api.Api(), feed, new BacktestingSetupHandler(), transactions);
            results.SetAlgorithm(algorithm);
            transactions.Initialize(algorithm, new BacktestingBrokerage(algorithm), results);
            feed.Initialize(algorithm, job, results, null, null, null);

            Log.Trace("Starting algorithm manager loop to process " + feed.Count + " time slices");
            var sw = Stopwatch.StartNew();

            algorithmManager.Run(job, algorithm, feed, transactions, results, realtime, leanManager, alphas, token);
            sw.Stop();

            var thousands = feed.Count / 1000d;
            var seconds   = sw.Elapsed.TotalSeconds;

            Log.Trace("COUNT: " + feed.Count + "  KPS: " + thousands / seconds);
        }
Exemplo n.º 3
0
        public void InvalidOrderRequestWontSetTicketAsProcessed()
        {
            //Initializes the transaction handler
            var transactionHandler = new BacktestingTransactionHandler();

            transactionHandler.Initialize(_algorithm, new BacktestingBrokerage(_algorithm), new BacktestingResultHandler());

            // Creates the order
            var security     = _algorithm.Securities[Ticker];
            var orderRequest = new SubmitOrderRequest(OrderType.Market, security.Type, security.Symbol, 600, 0, 0, 0, DateTime.Now, "");

            // Mock the the order processor
            var orderProcessorMock = new Mock <IOrderProcessor>();

            orderProcessorMock.Setup(m => m.GetOrderTicket(It.IsAny <int>())).Returns(new OrderTicket(_algorithm.Transactions, orderRequest));
            _algorithm.Transactions.SetOrderProcessor(orderProcessorMock.Object);

            var ticket = transactionHandler.AddOrder(orderRequest);

            var ticket2 = transactionHandler.AddOrder(orderRequest);

            // 600 after round off becomes 0 -> order is not placed
            Assert.IsTrue(orderRequest.Response.IsProcessed);
            Assert.IsTrue(orderRequest.Response.IsError);
            Assert.IsTrue(orderRequest.Response.ErrorMessage
                          .Contains("Cannot process submit request because order with id {0} already exists"));
        }
Exemplo n.º 4
0
        public void Initialize()
        {
            _algorithm = new QCAlgorithm();
            _algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(_algorithm));
            _portfolio = _algorithm.Portfolio;
            _portfolio.CashBook.Add("EUR", 0, 1.20m);
            _portfolio.CashBook.Add("BTC", 0, 15000m);
            _portfolio.CashBook.Add("ETH", 0, 1000m);

            _algorithm.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);

            _transactionHandler = new BacktestingTransactionHandler();
            _brokerage          = new BacktestingBrokerage(_algorithm);
            _transactionHandler.Initialize(_algorithm, _brokerage, new TestResultHandler());

            _algorithm.Transactions.SetOrderProcessor(_transactionHandler);

            var tz = TimeZones.NewYork;

            _btcusd = new Crypto(
                SecurityExchangeHours.AlwaysOpen(tz),
                _portfolio.CashBook[Currencies.USD],
                new SubscriptionDataConfig(typeof(TradeBar), Symbols.BTCUSD, Resolution.Minute, tz, tz, true, false, false),
                new SymbolProperties("BTCUSD", Currencies.USD, 1, 0.01m, 0.00000001m),
                ErrorCurrencyConverter.Instance
                );

            _ethusd = new Crypto(
                SecurityExchangeHours.AlwaysOpen(tz),
                _portfolio.CashBook[Currencies.USD],
                new SubscriptionDataConfig(typeof(TradeBar), Symbols.ETHUSD, Resolution.Minute, tz, tz, true, false, false),
                new SymbolProperties("ETHUSD", Currencies.USD, 1, 0.01m, 0.00000001m),
                ErrorCurrencyConverter.Instance
                );

            _btceur = new Crypto(
                SecurityExchangeHours.AlwaysOpen(tz),
                _portfolio.CashBook["EUR"],
                new SubscriptionDataConfig(typeof(TradeBar), Symbols.BTCEUR, Resolution.Minute, tz, tz, true, false, false),
                new SymbolProperties("BTCEUR", "EUR", 1, 0.01m, 0.00000001m),
                ErrorCurrencyConverter.Instance
                );

            _ethbtc = new Crypto(
                SecurityExchangeHours.AlwaysOpen(tz),
                _portfolio.CashBook["BTC"],
                new SubscriptionDataConfig(typeof(TradeBar), Symbols.ETHBTC, Resolution.Minute, tz, tz, true, false, false),
                new SymbolProperties("ETHBTC", "BTC", 1, 0.00001m, 0.00000001m),
                ErrorCurrencyConverter.Instance
                );

            _buyingPowerModel = new CashBuyingPowerModel();

            _timeKeeper = new LocalTimeKeeper(new DateTime(2019, 4, 22), DateTimeZone.Utc);
            _btcusd.SetLocalTimeKeeper(_timeKeeper);
            _ethusd.SetLocalTimeKeeper(_timeKeeper);
            _btceur.SetLocalTimeKeeper(_timeKeeper);
            _ethbtc.SetLocalTimeKeeper(_timeKeeper);
        }
Exemplo n.º 5
0
        public void TestAlgorithmManagerSpeed()
        {
            var algorithm        = PerformanceBenchmarkAlgorithms.SingleSecurity_Second;
            var algorithmManager = new AlgorithmManager(false);
            var job  = new BacktestNodePacket(1, 2, "3", null, 9m, $"{nameof(AlgorithmManagerTests)}.{nameof(TestAlgorithmManagerSpeed)}");
            var feed = new MockDataFeed();
            var marketHoursDatabase      = MarketHoursDatabase.FromDataFolder();
            var symbolPropertiesDataBase = SymbolPropertiesDatabase.FromDataFolder();
            var dataPermissionManager    = new DataPermissionManager();
            var dataManager = new DataManager(feed,
                                              new UniverseSelection(
                                                  algorithm,
                                                  new SecurityService(algorithm.Portfolio.CashBook,
                                                                      marketHoursDatabase,
                                                                      symbolPropertiesDataBase,
                                                                      algorithm,
                                                                      RegisteredSecurityDataTypesProvider.Null,
                                                                      new SecurityCacheProvider(algorithm.Portfolio)),
                                                  dataPermissionManager,
                                                  new DefaultDataProvider()),
                                              algorithm,
                                              algorithm.TimeKeeper,
                                              marketHoursDatabase,
                                              false,
                                              RegisteredSecurityDataTypesProvider.Null,
                                              dataPermissionManager);

            algorithm.SubscriptionManager.SetDataManager(dataManager);
            var transactions     = new BacktestingTransactionHandler();
            var results          = new BacktestingResultHandler();
            var realtime         = new BacktestingRealTimeHandler();
            var leanManager      = new NullLeanManager();
            var alphas           = new NullAlphaHandler();
            var token            = new CancellationToken();
            var nullSynchronizer = new NullSynchronizer(algorithm);

            algorithm.Initialize();
            algorithm.PostInitialize();

            results.Initialize(job, new QuantConnect.Messaging.Messaging(), new Api.Api(), transactions);
            results.SetAlgorithm(algorithm, algorithm.Portfolio.TotalPortfolioValue);
            transactions.Initialize(algorithm, new BacktestingBrokerage(algorithm), results);
            feed.Initialize(algorithm, job, results, null, null, null, dataManager, null, null);

            Log.Trace("Starting algorithm manager loop to process " + nullSynchronizer.Count + " time slices");
            var sw = Stopwatch.StartNew();

            algorithmManager.Run(job, algorithm, nullSynchronizer, transactions, results, realtime, leanManager, alphas, token);
            sw.Stop();

            realtime.Exit();
            results.Exit();
            var thousands = nullSynchronizer.Count / 1000d;
            var seconds   = sw.Elapsed.TotalSeconds;

            Log.Trace("COUNT: " + nullSynchronizer.Count + "  KPS: " + thousands / seconds);
        }
Exemplo n.º 6
0
        public void AppliesDividendsOnce()
        {
            // init algorithm
            var algorithm = new AlgorithmStub();

            algorithm.SetLiveMode(true);
            var dividend    = new Dividend(Symbols.SPY, DateTime.UtcNow, 10m, 100m);
            var feed        = new TestDividendDataFeed(algorithm, dividend);
            var dataManager = new DataManager(feed, new UniverseSelection(feed, algorithm), algorithm.Settings, algorithm.TimeKeeper);

            algorithm.SubscriptionManager.SetDataManager(dataManager);
            algorithm.AddSecurities(equities: new List <string> {
                "SPY"
            });
            algorithm.Securities[Symbols.SPY].Holdings.SetHoldings(100m, 1);
            algorithm.PostInitialize();

            var initializedCash = algorithm.Portfolio.CashBook["USD"].Amount;

            // init algorithm manager
            var manager = new AlgorithmManager(true);
            var job     = new LiveNodePacket
            {
                UserId    = 1,
                ProjectId = 2,
                DeployId  = $"{nameof(PaperBrokerageTests)}.{nameof(AppliesDividendsOnce)}"
            };
            var results      = new LiveTradingResultHandler();
            var transactions = new BacktestingTransactionHandler();
            var brokerage    = new PaperBrokerage(algorithm, job);

            // initialize results and transactions
            results.Initialize(job, new EventMessagingHandler(), new Api.Api(), feed, new BrokerageSetupHandler(), transactions);
            results.SetAlgorithm(algorithm);
            transactions.Initialize(algorithm, brokerage, results);

            // run algorithm manager
            manager.Run(job,
                        algorithm,
                        dataManager,
                        transactions,
                        results,
                        new BacktestingRealTimeHandler(),
                        new AlgorithmManagerTests.NullLeanManager(),
                        new AlgorithmManagerTests.NullAlphaHandler(),
                        new CancellationToken()
                        );

            var postDividendCash = algorithm.Portfolio.CashBook["USD"].Amount;

            Assert.AreEqual(initializedCash + dividend.Distribution, postDividendCash);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get an instance of the transaction handler set by the task.
        /// </summary>
        /// <param name="algorithm">Algorithm instance</param>
        /// <param name="job">Algorithm job packet</param>
        /// <param name="brokerage">Brokerage instance to avoid access token duplication</param>
        /// <param name="results">Results array for sending order events.</param>
        /// <returns>Class matching ITransactionHandler interface</returns>
        private static ITransactionHandler GetTransactionHandler(IAlgorithm algorithm, IBrokerage brokerage, IResultHandler results, AlgorithmNodePacket job)
        {
            ITransactionHandler th;

            switch (job.TransactionEndpoint)
            {
            //Operation from local files:
            default:
                th = new BacktestingTransactionHandler(algorithm, brokerage as BacktestingBrokerage);
                Log.Trace("Engine.GetTransactionHandler(): Selected Backtesting Transaction Models.");
                break;
            }
            return(th);
        }
Exemplo n.º 8
0
        public void Initialize()
        {
            _algorithm = new QCAlgorithm();
            _portfolio = _algorithm.Portfolio;
            _portfolio.CashBook.Add("EUR", 0, 1.20m);
            _portfolio.CashBook.Add("BTC", 0, 15000m);
            _portfolio.CashBook.Add("ETH", 0, 1000m);

            _algorithm.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);

            _transactionHandler = new BacktestingTransactionHandler();
            _brokerage          = new BacktestingBrokerage(_algorithm);
            _transactionHandler.Initialize(_algorithm, _brokerage, new TestResultHandler());
            new Thread(_transactionHandler.Run)
            {
                IsBackground = true
            }.Start();

            _algorithm.Transactions.SetOrderProcessor(_transactionHandler);

            var tz = TimeZones.NewYork;

            _btcusd = new Crypto(
                SecurityExchangeHours.AlwaysOpen(tz),
                _portfolio.CashBook[CashBook.AccountCurrency],
                new SubscriptionDataConfig(typeof(TradeBar), Symbols.BTCUSD, Resolution.Minute, tz, tz, true, false, false),
                new SymbolProperties("BTCUSD", "USD", 1, 0.01m, 0.00000001m));

            _ethusd = new Crypto(
                SecurityExchangeHours.AlwaysOpen(tz),
                _portfolio.CashBook[CashBook.AccountCurrency],
                new SubscriptionDataConfig(typeof(TradeBar), Symbols.ETHUSD, Resolution.Minute, tz, tz, true, false, false),
                new SymbolProperties("ETHUSD", "USD", 1, 0.01m, 0.00000001m));

            _btceur = new Crypto(
                SecurityExchangeHours.AlwaysOpen(tz),
                _portfolio.CashBook["EUR"],
                new SubscriptionDataConfig(typeof(TradeBar), Symbols.BTCEUR, Resolution.Minute, tz, tz, true, false, false),
                new SymbolProperties("BTCEUR", "EUR", 1, 0.01m, 0.00000001m));

            _ethbtc = new Crypto(
                SecurityExchangeHours.AlwaysOpen(tz),
                _portfolio.CashBook["BTC"],
                new SubscriptionDataConfig(typeof(TradeBar), Symbols.ETHBTC, Resolution.Minute, tz, tz, true, false, false),
                new SymbolProperties("ETHBTC", "BTC", 1, 0.00001m, 0.00000001m));

            _buyingPowerModel = new CashBuyingPowerModel();
        }
Exemplo n.º 9
0
        private void SetupImpl(IDataQueueHandler dataQueueHandler, Synchronizer synchronizer, IDataAggregator dataAggregator)
        {
            _dataFeed     = new TestableLiveTradingDataFeed(dataQueueHandler ?? new FakeDataQueue(dataAggregator ?? new AggregationManager()));
            _algorithm    = new AlgorithmStub(createDataManager: false);
            _synchronizer = synchronizer ?? new LiveSynchronizer();


            var registeredTypesProvider = new RegisteredSecurityDataTypesProvider();
            var securityService         = new SecurityService(_algorithm.Portfolio.CashBook,
                                                              MarketHoursDatabase.FromDataFolder(),
                                                              SymbolPropertiesDatabase.FromDataFolder(),
                                                              _algorithm,
                                                              registeredTypesProvider,
                                                              new SecurityCacheProvider(_algorithm.Portfolio));
            var universeSelection = new UniverseSelection(
                _algorithm,
                securityService,
                new DataPermissionManager(),
                TestGlobals.DataProvider,
                Resolution.Second);

            _dataManager = new DataManager(_dataFeed, universeSelection, _algorithm, new TimeKeeper(DateTime.UtcNow, TimeZones.NewYork),
                                           MarketHoursDatabase.FromDataFolder(),
                                           true,
                                           new RegisteredSecurityDataTypesProvider(),
                                           new DataPermissionManager());
            _resultHandler = new TestResultHandler();
            _synchronizer.Initialize(_algorithm, _dataManager);
            _dataFeed.Initialize(_algorithm,
                                 new LiveNodePacket(),
                                 _resultHandler,
                                 TestGlobals.MapFileProvider,
                                 TestGlobals.FactorFileProvider,
                                 TestGlobals.DataProvider,
                                 _dataManager,
                                 _synchronizer,
                                 new DataChannelProvider());
            _algorithm.SubscriptionManager.SetDataManager(_dataManager);
            _algorithm.Securities.SetSecurityService(securityService);
            _algorithm.SetFinishedWarmingUp();
            var backtestingTransactionHandler = new BacktestingTransactionHandler();

            backtestingTransactionHandler.Initialize(_algorithm, new PaperBrokerage(_algorithm, new LiveNodePacket()), _resultHandler);
            _algorithm.Transactions.SetOrderProcessor(backtestingTransactionHandler);
            _algorithm.PostInitialize();
        }
        private static DateTime InitializeTest(out BasicTemplateAlgorithm algorithm, out Security security, out PartialMarketFillModel model, out MarketOrder order, out OrderTicket ticket)
        {
            var referenceTimeNY  = new DateTime(2015, 12, 21, 13, 0, 0);
            var referenceTimeUtc = referenceTimeNY.ConvertToUtc(TimeZones.NewYork);

            algorithm = new BasicTemplateAlgorithm();
            algorithm.SetDateTime(referenceTimeUtc);

            var transactionHandler = new BacktestingTransactionHandler();

            transactionHandler.Initialize(algorithm, new BacktestingBrokerage(algorithm), new TestResultHandler(Console.WriteLine));

            algorithm.Transactions.SetOrderProcessor(transactionHandler);

            var config = new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Second, TimeZones.NewYork, TimeZones.NewYork, false, false, false);

            security = new Security(
                SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork),
                config,
                new Cash(Currencies.USD, 0, 1m),
                SymbolProperties.GetDefault(Currencies.USD),
                ErrorCurrencyConverter.Instance,
                RegisteredSecurityDataTypesProvider.Null,
                new SecurityCache()
                );

            model = new PartialMarketFillModel(algorithm.Transactions, 2);

            algorithm.Securities.Add(security);
            algorithm.Securities[Symbols.SPY].FillModel = model;
            security.SetMarketPrice(new Tick {
                Symbol = Symbols.SPY, Value = 100
            });
            algorithm.SetFinishedWarmingUp();

            order = new MarketOrder(Symbols.SPY, 100, referenceTimeUtc)
            {
                Id = 1
            };

            var request = new SubmitOrderRequest(OrderType.Market, security.Type, security.Symbol, order.Quantity, 0, 0, algorithm.UtcTime, null);

            ticket = algorithm.Transactions.ProcessRequest(request);
            return(referenceTimeUtc);
        }
Exemplo n.º 11
0
        private PortfolioLooperAlgorithm CreateAlgorithm(IEnumerable <Order> orders)
        {
            var algorithm = new PortfolioLooperAlgorithm(100000m, orders);

            // Create MHDB and Symbol properties DB instances for the DataManager
            var marketHoursDatabase      = MarketHoursDatabase.FromDataFolder();
            var symbolPropertiesDataBase = SymbolPropertiesDatabase.FromDataFolder();
            var dataPermissionManager    = new DataPermissionManager();
            var dataManager = new DataManager(new QuantConnect.Report.MockDataFeed(),
                                              new UniverseSelection(
                                                  algorithm,
                                                  new SecurityService(algorithm.Portfolio.CashBook,
                                                                      marketHoursDatabase,
                                                                      symbolPropertiesDataBase,
                                                                      algorithm,
                                                                      RegisteredSecurityDataTypesProvider.Null,
                                                                      new SecurityCacheProvider(algorithm.Portfolio)),
                                                  dataPermissionManager),
                                              algorithm,
                                              algorithm.TimeKeeper,
                                              marketHoursDatabase,
                                              false,
                                              RegisteredSecurityDataTypesProvider.Null,
                                              dataPermissionManager);

            var securityService = new SecurityService(algorithm.Portfolio.CashBook,
                                                      marketHoursDatabase,
                                                      symbolPropertiesDataBase,
                                                      algorithm,
                                                      RegisteredSecurityDataTypesProvider.Null,
                                                      new SecurityCacheProvider(algorithm.Portfolio));

            var transactions = new BacktestingTransactionHandler();
            var results      = new BacktestingResultHandler();

            // Initialize security services and other properties so that we
            // don't get null reference exceptions during our re-calculation
            algorithm.Securities.SetSecurityService(securityService);
            algorithm.SubscriptionManager.SetDataManager(dataManager);

            return(algorithm);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Get an instance of the transaction handler set by the task.
        /// </summary>
        /// <param name="algorithm">Algorithm instance</param>
        /// <param name="job">Algorithm job packet</param>
        /// <param name="brokerage">Brokerage instance to avoid access token duplication</param>
        /// <param name="results">Results array for sending order events.</param>
        /// <returns>Class matching ITransactionHandler interface</returns>
        private static ITransactionHandler GetTransactionHandler(IAlgorithm algorithm, IBrokerage brokerage, IResultHandler results, AlgorithmNodePacket job)
        {
            ITransactionHandler th;

            switch (job.TransactionEndpoint)
            {
            //Operation from local files:
            default:
                th = new BacktestingTransactionHandler(algorithm);
                Log.Trace("Engine.GetTransactionHandler(): Selected Backtesting Transaction Models.");
                break;

            case TransactionHandlerEndpoint.Tradier:
                var live = job as LiveNodePacket;
                Log.Trace("Engine.GetTransactionHandler(): Selected Live Transaction Fills.");
                th = new TradierTransactionHandler(algorithm, brokerage, results, live.AccountId);
                break;
            }
            return(th);
        }
Exemplo n.º 13
0
        public void AppliesDividendsOnce()
        {
            // init algorithm
            var algorithm = new AlgorithmStub(new MockDataFeed());

            algorithm.SetLiveMode(true);
            var dividend = new Dividend(Symbols.SPY, DateTime.UtcNow, 10m, 100m);

            var feed = new MockDataFeed();

            var marketHoursDatabase      = MarketHoursDatabase.FromDataFolder();
            var symbolPropertiesDataBase = SymbolPropertiesDatabase.FromDataFolder();
            var dataPermissionManager    = new DataPermissionManager();
            var dataManager = new DataManager(feed,
                                              new UniverseSelection(
                                                  algorithm,
                                                  new SecurityService(algorithm.Portfolio.CashBook, marketHoursDatabase, symbolPropertiesDataBase, algorithm, RegisteredSecurityDataTypesProvider.Null, new SecurityCacheProvider(algorithm.Portfolio)),
                                                  dataPermissionManager,
                                                  new DefaultDataProvider()),
                                              algorithm,
                                              algorithm.TimeKeeper,
                                              marketHoursDatabase,
                                              true,
                                              RegisteredSecurityDataTypesProvider.Null,
                                              dataPermissionManager);
            var synchronizer = new NullSynchronizer(algorithm, dividend);

            algorithm.SubscriptionManager.SetDataManager(dataManager);
            algorithm.AddSecurities(equities: new List <string> {
                "SPY"
            });
            algorithm.Securities[Symbols.SPY].Holdings.SetHoldings(100m, 1);
            algorithm.PostInitialize();

            var initializedCash = algorithm.Portfolio.CashBook[Currencies.USD].Amount;

            // init algorithm manager
            var manager = new AlgorithmManager(true);
            var job     = new LiveNodePacket
            {
                UserId    = 1,
                ProjectId = 2,
                DeployId  = $"{nameof(PaperBrokerageTests)}.{nameof(AppliesDividendsOnce)}"
            };
            var results      = new LiveTradingResultHandler();
            var transactions = new BacktestingTransactionHandler();
            var brokerage    = new PaperBrokerage(algorithm, job);

            // initialize results and transactions
            results.Initialize(job, new EventMessagingHandler(), new Api.Api(), transactions);
            results.SetAlgorithm(algorithm, algorithm.Portfolio.TotalPortfolioValue);
            transactions.Initialize(algorithm, brokerage, results);

            var realTime = new BacktestingRealTimeHandler();

            // run algorithm manager
            manager.Run(job,
                        algorithm,
                        synchronizer,
                        transactions,
                        results,
                        realTime,
                        new AlgorithmManagerTests.NullLeanManager(),
                        new AlgorithmManagerTests.NullAlphaHandler(),
                        new CancellationToken()
                        );

            var postDividendCash = algorithm.Portfolio.CashBook[Currencies.USD].Amount;

            realTime.Exit();
            results.Exit();
            Assert.AreEqual(initializedCash + dividend.Distribution, postDividendCash);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Creates an instance of the PortfolioLooper class
        /// </summary>
        /// <param name="startingCash">Equity curve</param>
        /// <param name="orders">Order events</param>
        /// <param name="resolution">Optional parameter to override default resolution (Hourly)</param>
        private PortfolioLooper(double startingCash, List <Order> orders, Resolution resolution = _resolution)
        {
            // Initialize the providers that the HistoryProvider requires
            var factorFileProvider = Composer.Instance.GetExportedValueByTypeName <IFactorFileProvider>("LocalDiskFactorFileProvider");
            var mapFileProvider    = Composer.Instance.GetExportedValueByTypeName <IMapFileProvider>("LocalDiskMapFileProvider");

            _cacheProvider = new ZipDataCacheProvider(new DefaultDataProvider(), false);
            var historyProvider = new SubscriptionDataReaderHistoryProvider();

            var dataPermissionManager = new DataPermissionManager();

            historyProvider.Initialize(new HistoryProviderInitializeParameters(null, null, null, _cacheProvider, mapFileProvider, factorFileProvider, (_) => { }, false, dataPermissionManager));
            Algorithm = new PortfolioLooperAlgorithm((decimal)startingCash, orders);
            Algorithm.SetHistoryProvider(historyProvider);

            // Dummy LEAN datafeed classes and initializations that essentially do nothing
            var job  = new BacktestNodePacket(1, 2, "3", null, 9m, $"");
            var feed = new MockDataFeed();

            // Create MHDB and Symbol properties DB instances for the DataManager
            var marketHoursDatabase      = MarketHoursDatabase.FromDataFolder();
            var symbolPropertiesDataBase = SymbolPropertiesDatabase.FromDataFolder();

            _dataManager = new DataManager(feed,
                                           new UniverseSelection(
                                               Algorithm,
                                               new SecurityService(Algorithm.Portfolio.CashBook,
                                                                   marketHoursDatabase,
                                                                   symbolPropertiesDataBase,
                                                                   Algorithm,
                                                                   RegisteredSecurityDataTypesProvider.Null,
                                                                   new SecurityCacheProvider(Algorithm.Portfolio)),
                                               dataPermissionManager,
                                               new DefaultDataProvider()),
                                           Algorithm,
                                           Algorithm.TimeKeeper,
                                           marketHoursDatabase,
                                           false,
                                           RegisteredSecurityDataTypesProvider.Null,
                                           dataPermissionManager);

            _securityService = new SecurityService(Algorithm.Portfolio.CashBook,
                                                   marketHoursDatabase,
                                                   symbolPropertiesDataBase,
                                                   Algorithm,
                                                   RegisteredSecurityDataTypesProvider.Null,
                                                   new SecurityCacheProvider(Algorithm.Portfolio));

            var transactions = new BacktestingTransactionHandler();

            _resultHandler = new BacktestingResultHandler();

            // Initialize security services and other properties so that we
            // don't get null reference exceptions during our re-calculation
            Algorithm.Securities.SetSecurityService(_securityService);
            Algorithm.SubscriptionManager.SetDataManager(_dataManager);

            // Initializes all the proper Securities from the orders provided by the user
            Algorithm.FromOrders(orders);

            // Initialize the algorithm
            Algorithm.Initialize();
            Algorithm.PostInitialize();

            // More initialization, this time with Algorithm and other misc. classes
            _resultHandler.Initialize(job, new Messaging.Messaging(), new Api.Api(), transactions);
            _resultHandler.SetAlgorithm(Algorithm, Algorithm.Portfolio.TotalPortfolioValue);

            Algorithm.Transactions.SetOrderProcessor(transactions);

            transactions.Initialize(Algorithm, new BacktestingBrokerage(Algorithm), _resultHandler);
            feed.Initialize(Algorithm, job, _resultHandler, null, null, null, _dataManager, null, null);

            // Begin setting up the currency conversion feed if needed
            var coreSecurities = Algorithm.Securities.Values.ToList();

            if (coreSecurities.Any(x => x.Symbol.SecurityType == SecurityType.Forex || x.Symbol.SecurityType == SecurityType.Crypto))
            {
                BaseSetupHandler.SetupCurrencyConversions(Algorithm, _dataManager.UniverseSelection);
                var conversionSecurities = Algorithm.Securities.Values.Where(s => !coreSecurities.Contains(s)).ToList();

                // Skip the history request if we don't need to convert anything
                if (conversionSecurities.Any())
                {
                    // Point-in-time Slices to convert FX and Crypto currencies to the portfolio currency
                    _conversionSlices = GetHistory(Algorithm, conversionSecurities, resolution);
                }
            }
        }
Exemplo n.º 15
0
        public void SendingNewOrderFromOnOrderEvent()
        {
            //Initializes the transaction handler
            var transactionHandler = new BacktestingTransactionHandler();
            var brokerage          = new BacktestingBrokerage(_algorithm);

            transactionHandler.Initialize(_algorithm, brokerage, new BacktestingResultHandler());

            // Creates a market order
            var security = _algorithm.Securities[Ticker];
            var price    = 1.12m;

            security.SetMarketPrice(new Tick(DateTime.UtcNow.AddDays(-1), security.Symbol, price, price, price));
            var orderRequest  = new SubmitOrderRequest(OrderType.Market, security.Type, security.Symbol, 1000, 0, 0, 0, DateTime.UtcNow, "");
            var orderRequest2 = new SubmitOrderRequest(OrderType.Market, security.Type, security.Symbol, -1000, 0, 0, 0, DateTime.UtcNow, "");

            orderRequest.SetOrderId(1);
            orderRequest2.SetOrderId(2);

            // Mock the the order processor
            var orderProcessorMock = new Mock <IOrderProcessor>();

            orderProcessorMock.Setup(m => m.GetOrderTicket(It.Is <int>(i => i == 1))).Returns(new OrderTicket(_algorithm.Transactions, orderRequest));
            orderProcessorMock.Setup(m => m.GetOrderTicket(It.Is <int>(i => i == 2))).Returns(new OrderTicket(_algorithm.Transactions, orderRequest2));
            _algorithm.Transactions.SetOrderProcessor(orderProcessorMock.Object);

            var orderEventCalls = 0;

            brokerage.OrderStatusChanged += (sender, orderEvent) =>
            {
                orderEventCalls++;
                switch (orderEventCalls)
                {
                case 1:
                    Assert.AreEqual(1, orderEvent.OrderId);
                    Assert.AreEqual(OrderStatus.Submitted, orderEvent.Status);

                    // we send a new order request
                    var ticket2 = transactionHandler.Process(orderRequest2);
                    break;

                case 2:
                    Assert.AreEqual(2, orderEvent.OrderId);
                    Assert.AreEqual(OrderStatus.Submitted, orderEvent.Status);
                    break;

                case 3:
                    Assert.AreEqual(1, orderEvent.OrderId);
                    Assert.AreEqual(OrderStatus.Filled, orderEvent.Status);
                    break;

                case 4:
                    Assert.AreEqual(2, orderEvent.OrderId);
                    Assert.AreEqual(OrderStatus.Filled, orderEvent.Status);
                    break;
                }
                Log.Trace($"{orderEvent}");
            };

            var ticket = transactionHandler.Process(orderRequest);

            Assert.IsTrue(orderRequest.Response.IsProcessed);
            Assert.IsTrue(orderRequest.Response.IsSuccess);
            Assert.AreEqual(OrderRequestStatus.Processed, orderRequest.Status);
            Assert.IsTrue(orderRequest2.Response.IsProcessed);
            Assert.IsTrue(orderRequest2.Response.IsSuccess);
            Assert.AreEqual(OrderRequestStatus.Processed, orderRequest2.Status);

            var order1 = transactionHandler.GetOrderById(1);

            Assert.AreEqual(OrderStatus.Filled, order1.Status);
            var order2 = transactionHandler.GetOrderById(2);

            Assert.AreEqual(OrderStatus.Filled, order2.Status);

            // 2 submitted and 2 filled
            Assert.AreEqual(4, orderEventCalls);
        }
Exemplo n.º 16
0
        public void OptionExercise_NonAccountCurrency()
        {
            var algorithm          = new QCAlgorithm();
            var securities         = new SecurityManager(new TimeKeeper(DateTime.Now, TimeZones.NewYork));
            var transactions       = new SecurityTransactionManager(null, securities);
            var transactionHandler = new BacktestingTransactionHandler();
            var portfolio          = new SecurityPortfolioManager(securities, transactions);

            var EUR = new Cash("EUR", 100 * 192, 10);

            portfolio.CashBook.Add("EUR", EUR);
            portfolio.SetCash("USD", 0, 1);
            algorithm.Securities = securities;
            transactionHandler.Initialize(algorithm, new BacktestingBrokerage(algorithm), _resultHandler);
            transactions.SetOrderProcessor(transactionHandler);

            securities.Add(
                Symbols.SPY,
                new Security(
                    SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork),
                    CreateTradeBarConfig(Symbols.SPY),
                    EUR,
                    SymbolProperties.GetDefault(EUR.Symbol),
                    ErrorCurrencyConverter.Instance,
                    RegisteredSecurityDataTypesProvider.Null,
                    new SecurityCache()
                    )
                );
            securities.Add(
                Symbols.SPY_C_192_Feb19_2016,
                new Option(
                    SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork),
                    CreateTradeBarConfig(Symbols.SPY_C_192_Feb19_2016),
                    EUR,
                    new OptionSymbolProperties(new SymbolProperties("EUR", "EUR", 100, 0.01m, 1)),
                    ErrorCurrencyConverter.Instance,
                    RegisteredSecurityDataTypesProvider.Null
                    )
                );
            securities[Symbols.SPY_C_192_Feb19_2016].Holdings.SetHoldings(1, 1);
            securities[Symbols.SPY].SetMarketPrice(new Tick {
                Value = 200
            });

            transactions.AddOrder(new SubmitOrderRequest(OrderType.OptionExercise, SecurityType.Option, Symbols.SPY_C_192_Feb19_2016, -1, 0, 0, securities.UtcTime, ""));
            var option = (Option)securities[Symbols.SPY_C_192_Feb19_2016];
            var order  = (OptionExerciseOrder)transactions.GetOrders(x => true).First();

            option.Underlying = securities[Symbols.SPY];

            var fills = option.OptionExerciseModel.OptionExercise(option, order).ToList();

            Assert.AreEqual(2, fills.Count);
            Assert.IsFalse(fills[0].IsAssignment);
            Assert.AreEqual("Automatic Exercise", fills[0].Message);
            Assert.AreEqual("Option Exercise", fills[1].Message);

            foreach (var fill in fills)
            {
                portfolio.ProcessFill(fill);
            }

            // now we have long position in SPY with average price equal to strike
            var newUnderlyingHoldings = securities[Symbols.SPY].Holdings;

            // we added 100*192 EUR (strike price) at beginning, all consumed by exercise
            Assert.AreEqual(0, EUR.Amount);
            Assert.AreEqual(0, portfolio.CashBook["USD"].Amount);
            Assert.AreEqual(100, newUnderlyingHoldings.Quantity);
            Assert.AreEqual(192.0, newUnderlyingHoldings.AveragePrice);

            // and long call option position has disappeared
            Assert.AreEqual(0, securities[Symbols.SPY_C_192_Feb19_2016].Holdings.Quantity);
        }
Exemplo n.º 17
0
        private void LaunchLean(string id)
        {
            Config.Set("environment", "backtesting");

            if (!string.IsNullOrEmpty(_config.AlgorithmTypeName))
            {
                Config.Set("algorithm-type-name", _config.AlgorithmTypeName);
            }

            if (!string.IsNullOrEmpty(_config.AlgorithmLocation))
            {
                Config.Set("algorithm-location", Path.GetFileName(_config.AlgorithmLocation));
            }

            if (!string.IsNullOrEmpty(_config.DataFolder))
            {
                Config.Set("data-folder", _config.DataFolder);
            }

            if (!string.IsNullOrEmpty(_config.TransactionLog))
            {
                var filename = _config.TransactionLog;
                filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileNameWithoutExtension(filename) + id + Path.GetExtension(filename));

                Config.Set("transaction-log", filename);
            }

            Config.Set("api-handler", nameof(EmptyApiHandler));
            Config.Set("backtesting.result-handler", nameof(OptimizerResultHandler));

            //Composer.Instance.Reset();

            //todo: instance logging
            //var logFileName = "log" + DateTime.Now.ToString("yyyyMMddssfffffff") + "_" + id + ".txt";
            Log.LogHandler = LogSingleton.Instance;

            var jobQueue = new JobQueue();
            var manager  = new LocalLeanManager();

            var systemHandlers = new LeanEngineSystemHandlers(
                jobQueue,
                new EmptyApiHandler(),
                new QuantConnect.Messaging.Messaging(),
                manager);

            systemHandlers.Initialize();

            var map          = new LocalDiskMapFileProvider();
            var results      = new OptimizerResultHandler();
            var transactions = new BacktestingTransactionHandler();
            var dataFeed     = new FileSystemDataFeed();
            var realTime     = new BacktestingRealTimeHandler();
            var data         = new DefaultDataProvider();

            var leanEngineAlgorithmHandlers = new LeanEngineAlgorithmHandlers(
                results,
                new ConsoleSetupHandler(),
                dataFeed,
                transactions,
                realTime,
                map,
                new LocalDiskFactorFileProvider(map),
                data,
                new OptimizerAlphaHandler(),
                new EmptyObjectStore());

            _resultsHandler = (OptimizerResultHandler)leanEngineAlgorithmHandlers.Results;

            var job = (BacktestNodePacket)systemHandlers.JobQueue.NextJob(out var algorithmPath);

            //mark job with id. Is set on algorithm in OptimizerAlphaHandler
            job.BacktestId = id;
            //todo: pass period through job
            //job.PeriodStart = _config.StartDate;
            //job.PeriodFinish = _config.EndDate;

            Engine           engine;
            AlgorithmManager algorithmManager;

            try
            {
                algorithmManager = new AlgorithmManager(false);
                systemHandlers.LeanManager.Initialize(systemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager);
                engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, false);
                using (var workerThread = new MultipleWorkerThread())
                {
                    engine.Run(job, algorithmManager, algorithmPath, workerThread);
                }
            }
            finally
            {
                // clean up resources
                Composer.Instance.Reset();
                results.Charts.Clear();
                results.Messages.Clear();

                results.Algorithm.Transactions.TransactionRecord.Clear();
                var closedTrades = (List <Trade>) typeof(TradeBuilder).GetField("_closedTrades", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(results.Algorithm.TradeBuilder);
                closedTrades.Clear();
                results.Algorithm.HistoryProvider = null;
                results.Algorithm = null;
                transactions.Orders.Clear();
                transactions.OrderTickets.Clear();
                manager.Dispose();
                systemHandlers.Dispose();
                leanEngineAlgorithmHandlers.Dispose();
                results        = null;
                dataFeed       = null;
                transactions   = null;
                realTime       = null;
                data           = null;
                map            = null;
                systemHandlers = null;
                leanEngineAlgorithmHandlers = null;
                algorithmManager            = null;
                engine   = null;
                job      = null;
                jobQueue = null;
                manager  = null;
            }
        }
Exemplo n.º 18
0
        private void LaunchLean(string id)
        {
            ConfigMerger.Merge(_config, id);

            Config.Set("api-handler", nameof(EmptyApiHandler));

            //todo: instance logging
            //var logFileName = "log" + DateTime.Now.ToString("yyyyMMddssfffffff") + "_" + id + ".txt";
            Log.LogHandler = LogSingleton.Instance;

            var jobQueue = new JobQueue();
            var manager  = new LocalLeanManager();

            var systemHandlers = new LeanEngineSystemHandlers(
                jobQueue,
                new EmptyApiHandler(),
                new QuantConnect.Messaging.Messaging(),
                manager);

            systemHandlers.Initialize();

            var map          = new LocalDiskMapFileProvider();
            var results      = new OptimizerResultHandler();
            var transactions = new BacktestingTransactionHandler();
            var dataFeed     = new FileSystemDataFeed();
            var realTime     = new BacktestingRealTimeHandler();
            var data         = new DefaultDataProvider();

            var leanEngineAlgorithmHandlers = new LeanEngineAlgorithmHandlers(
                results,
                new ConsoleSetupHandler(),
                dataFeed,
                transactions,
                realTime,
                map,
                new LocalDiskFactorFileProvider(map),
                data,
                new OptimizerAlphaHandler(),
                new EmptyObjectStore());

            _resultsHandler = (OptimizerResultHandler)leanEngineAlgorithmHandlers.Results;

            var job = (BacktestNodePacket)systemHandlers.JobQueue.NextJob(out var algorithmPath);

            //mark job with id. Is set on algorithm in OptimizerAlphaHandler
            job.BacktestId = id;
            //todo: pass period through job
            //job.PeriodStart = _config.StartDate;
            //job.PeriodFinish = _config.EndDate;

            Engine           engine;
            AlgorithmManager algorithmManager;

            try
            {
                algorithmManager = new AlgorithmManager(false);
                systemHandlers.LeanManager.Initialize(systemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager);
                engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, false);
                using (var workerThread = new MultipleWorkerThread())
                {
                    engine.Run(job, algorithmManager, algorithmPath, workerThread);
                }
            }
            finally
            {
                // clean up resources
                Composer.Instance.Reset();
                results.Charts.Clear();
                results.Messages.Clear();
                if (results.Algorithm != null)
                {
                    results.Algorithm.Transactions.TransactionRecord.Clear();
                    results.Algorithm.SubscriptionManager.Subscriptions.SelectMany(s => s.Consolidators)?.ToList().ForEach(f =>
                    {
                        results.Algorithm.SubscriptionManager.RemoveConsolidator(f.WorkingData?.Symbol, f);
                        UnregisterAllEvents(f);
                    });
                    if (results.Algorithm is QCAlgorithm)
                    {
                        ((QCAlgorithm)results.Algorithm).SubscriptionManager.Subscriptions.ToList().Clear();
                    }
                    if (_config.AlgorithmLanguage != "Python")
                    {
                        results.Algorithm.HistoryProvider = null;
                    }
                    var closedTrades = (List <Trade>) typeof(TradeBuilder).GetField("_closedTrades", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(results.Algorithm.TradeBuilder);
                    closedTrades.Clear();
                    results.Algorithm = null;
                }
                transactions.Orders.Clear();
                transactions.OrderTickets.Clear();
                manager.Dispose();
                systemHandlers.Dispose();
                leanEngineAlgorithmHandlers.Dispose();
                results        = null;
                dataFeed       = null;
                transactions   = null;
                realTime       = null;
                data           = null;
                map            = null;
                systemHandlers = null;
                leanEngineAlgorithmHandlers = null;
                algorithmManager            = null;
                engine   = null;
                job      = null;
                jobQueue = null;
                manager  = null;
            }
        }