public void LoadsExistingHoldingsAndOrders(List <Holding> holdings, List <Order> orders) { var algorithm = new TestAlgorithm(); var job = new LiveNodePacket { UserId = 1, ProjectId = 1, DeployId = "1", Brokerage = "PaperBrokerage", DataQueueHandler = "none" }; // Increasing RAM limit, else the tests fail. This is happening in master, when running all the tests together, locally (not travis). job.Controls.RamAllocation = 1024 * 1024 * 1024; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <Cash>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(holdings); brokerage.Setup(x => x.GetOpenOrders()).Returns(orders); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); var result = setupHandler.Setup(algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object); Assert.IsTrue(result); }
public void LoadsExistingHoldingsAndOrders(List <Holding> holdings, List <Order> orders) { var algorithm = new TestAlgorithm(); var job = new LiveNodePacket { UserId = 1, ProjectId = 1, DeployId = "1", Brokerage = "PaperBrokerage", DataQueueHandler = "none" }; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <Cash>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(holdings); brokerage.Setup(x => x.GetOpenOrders()).Returns(orders); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); var result = setupHandler.Setup(algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object); Assert.IsTrue(result); }
public void EnforcesAccountCurrency(bool enforceAccountCurrency) { var algorithm = new TestAlgorithm(); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); if (enforceAccountCurrency) { job.BrokerageData[BrokerageSetupHandler.MaxAllocationLimitConfig] = "200000"; } var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.EUR); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider))); Assert.AreEqual(enforceAccountCurrency ? Currencies.USD : Currencies.EUR, algorithm.AccountCurrency); }
public void HandlesErrorOnInitializeCorrectly() { var algorithm = new BacktestingSetupHandlerTests.TestAlgorithmThrowsOnInitialize(); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsFalse(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider))); setupHandler.DisposeSafely(); Assert.AreEqual(1, setupHandler.Errors.Count); Assert.IsTrue(setupHandler.Errors[0].InnerException.Message.Equals("Some failure")); }
public void EnforcesAccountCurrency(bool enforceAccountCurrency) { var algorithm = new TestAlgorithm(); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); if (enforceAccountCurrency) { job.BrokerageData["max-cash-limit"] = "[{\"amount\":20, \"currency\": \"USD\"}, {\"Amount\":1, \"Currency\": \"EUR\"}]"; } var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.EUR); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object))); Assert.AreEqual(enforceAccountCurrency ? Currencies.USD : Currencies.EUR, algorithm.AccountCurrency); }
public void SeedsSecurityCorrectly() { var symbol = Symbol.Create("AUDUSD", SecurityType.Forex, Market.Oanda); var algorithm = new TestAlgorithm(); algorithm.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = new LiveNodePacket { UserId = 1, ProjectId = 1, DeployId = "1", Brokerage = "PaperBrokerage", DataQueueHandler = "none" }; // Increasing RAM limit, else the tests fail. This is happening in master, when running all the tests together, locally (not travis). job.Controls.RamAllocation = 1024 * 1024 * 1024; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding> { new Holding { Symbol = symbol, Type = symbol.SecurityType, Quantity = 100, MarketPrice = 99 } }); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object))); Security security; Assert.IsTrue(algorithm.Portfolio.Securities.TryGetValue(symbol, out security)); Assert.AreEqual(symbol, security.Symbol); Assert.AreEqual(99, security.Price); var last = security.GetLastData(); Assert.IsTrue((DateTime.UtcNow.ConvertFromUtc(security.Exchange.TimeZone) - last.Time) < TimeSpan.FromSeconds(1)); }
public void HasErrorWithZeroTotalPortfolioValue(bool hasCashBalance, bool hasHoldings) { var algorithm = new TestAlgorithm(); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); job.Brokerage = "TestBrokerage"; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns( hasCashBalance ? new List <CashAmount> { new CashAmount(1000, "USD") } : new List <CashAmount>() ); brokerage.Setup(x => x.GetAccountHoldings()).Returns( hasHoldings ? new List <Holding> { new Holding { Symbol = Symbols.SPY, Quantity = 1, AveragePrice = 100, MarketPrice = 100 } } : new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); var dataManager = new DataManagerStub(algorithm, new MockDataFeed(), true); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider))); if (!hasCashBalance && !hasHoldings) { Assert.That(algorithm.DebugMessages.Count > 0); Assert.That(algorithm.DebugMessages.Any(x => x.Contains("No cash balances or holdings were found in the brokerage account."))); } }
public void LoadsExistingHoldingsAndOrders(Func <List <Holding> > getHoldings, Func <List <Order> > getOrders, bool expected) { var algorithm = new TestAlgorithm(); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = new LiveNodePacket { UserId = 1, ProjectId = 1, DeployId = "1", Brokerage = "PaperBrokerage", DataQueueHandler = "none" }; // Increasing RAM limit, else the tests fail. This is happening in master, when running all the tests together, locally (not travis). job.Controls.RamAllocation = 1024 * 1024 * 1024; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var objectStore = new Mock <IObjectStore>(); var brokerage = new Mock <IBrokerage>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(getHoldings); brokerage.Setup(x => x.GetOpenOrders()).Returns(getOrders); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); var result = setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object)); Assert.AreEqual(expected, result); foreach (var security in algorithm.Securities.Values) { if (security.Symbol.SecurityType == SecurityType.Option) { Assert.AreEqual(DataNormalizationMode.Raw, security.DataNormalizationMode); var underlyingSecurity = algorithm.Securities[security.Symbol.Underlying]; Assert.AreEqual(DataNormalizationMode.Raw, underlyingSecurity.DataNormalizationMode); } } }
public void LoadsHoldingsForExpectedMarket() { var symbol = Symbol.Create("AUDUSD", SecurityType.Forex, Market.Oanda); var algorithm = new TestAlgorithm(); algorithm.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = new LiveNodePacket { UserId = 1, ProjectId = 1, DeployId = "1", Brokerage = "PaperBrokerage", DataQueueHandler = "none" }; // Increasing RAM limit, else the tests fail. This is happening in master, when running all the tests together, locally (not travis). job.Controls.RamAllocation = 1024 * 1024 * 1024; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <Cash>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding> { new Holding { Symbol = symbol, Type = symbol.SecurityType, Quantity = 100 } }); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object)); Security security; Assert.IsTrue(algorithm.Portfolio.Securities.TryGetValue(symbol, out security)); Assert.AreEqual(symbol, security.Symbol); }
public void AlgorithmTimeIsSetToUtcNowBeforePostInitialize() { var time = DateTime.UtcNow; TestAlgorithm algorithm = null; algorithm = new TestAlgorithm(() => { Assert.That(algorithm.UtcTime > time); }); Assert.AreEqual(new DateTime(1998, 1, 1), algorithm.UtcTime); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = new LiveNodePacket { UserId = 1, ProjectId = 1, DeployId = "1", Brokerage = "PaperBrokerage", DataQueueHandler = "none", Controls = new Controls { RamAllocation = 4096 } // no real limit }; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object))); Assert.Greater(algorithm.UtcTime, time); }
public void SeedsSecurityCorrectly() { var symbol = Symbol.Create("AUDUSD", SecurityType.Forex, Market.Oanda); var algorithm = new TestAlgorithm(); algorithm.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding> { new Holding { Symbol = symbol, Quantity = 100, MarketPrice = 99 } }); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider))); Security security; Assert.IsTrue(algorithm.Portfolio.Securities.TryGetValue(symbol, out security)); Assert.AreEqual(symbol, security.Symbol); Assert.AreEqual(99, security.Price); var last = security.GetLastData(); Assert.IsTrue((DateTime.UtcNow.ConvertFromUtc(security.Exchange.TimeZone) - last.Time) < TimeSpan.FromSeconds(1)); }
public void SkipsLoadingHoldingsAndOrders() { var symbol = Symbol.Create("AUDUSD", SecurityType.Forex, Market.Oanda); var algorithm = new TestAlgorithm(); algorithm.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); job.BrokerageData["load-existing-holdings"] = "false"; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding> { new Holding { Symbol = symbol, Type = symbol.SecurityType, Quantity = 100 } }); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order> { new LimitOrder(Symbols.SPY, 1, 1, DateTime.UtcNow) }); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object))); Security security; Assert.IsFalse(algorithm.Portfolio.Securities.TryGetValue(symbol, out security)); Assert.IsFalse(algorithm.Portfolio.Securities.TryGetValue(Symbols.SPY, out security)); }
public void LoadsHoldingsForExpectedMarket() { var symbol = Symbol.Create("AUDUSD", SecurityType.Forex, Market.Oanda); var algorithm = new TestAlgorithm(); algorithm.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding> { new Holding { Symbol = symbol, Quantity = 100 } }); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataProvider))); Security security; Assert.IsTrue(algorithm.Portfolio.Securities.TryGetValue(symbol, out security)); Assert.AreEqual(symbol, security.Symbol); }
public void LoadsExistingHoldingsAndOrders(Func <List <Holding> > getHoldings, Func <List <Order> > getOrders, bool expected) { var algorithm = new TestAlgorithm(); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var objectStore = new Mock <IObjectStore>(); var brokerage = new Mock <IBrokerage>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(getHoldings); brokerage.Setup(x => x.GetOpenOrders()).Returns(getOrders); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); var result = setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider)); Assert.AreEqual(expected, result); foreach (var security in algorithm.Securities.Values) { if (security.Symbol.SecurityType == SecurityType.Option) { Assert.AreEqual(DataNormalizationMode.Raw, security.DataNormalizationMode); var underlyingSecurity = algorithm.Securities[security.Symbol.Underlying]; Assert.AreEqual(DataNormalizationMode.Raw, underlyingSecurity.DataNormalizationMode); } } }
public void AlgorithmTimeIsSetToUtcNowBeforePostInitialize() { var time = DateTime.UtcNow; TestAlgorithm algorithm = null; algorithm = new TestAlgorithm(() => { Assert.That(algorithm.UtcTime > time); }); Assert.AreEqual(new DateTime(1998, 1, 1), algorithm.UtcTime); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider))); Assert.Greater(algorithm.UtcTime, time); }
public void EnforcesCashAmounts() { var algorithm = new TestAlgorithm(); algorithm.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); job.BrokerageData["max-cash-limit"] = "[{\"amount\":20, \"currency\": \"USD\"}, {\"Amount\":1, \"Currency\": \"EUR\"}]"; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount> { new CashAmount(10000, Currencies.USD), new CashAmount(11, Currencies.GBP) }); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object))); Assert.AreEqual(20, algorithm.Portfolio.CashBook[Currencies.USD].Amount); Assert.IsFalse(algorithm.Portfolio.CashBook.ContainsKey(Currencies.GBP)); Assert.IsFalse(algorithm.Portfolio.CashBook.ContainsKey(Currencies.EUR)); }
public void ExistingHoldingsAndOrdersResolution(Func <List <Holding> > getHoldings, Func <List <Order> > getOrders, bool expected, Resolution resolution) { var algorithm = new TestAlgorithm { UniverseSettings = { Resolution = resolution } }; algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var objectStore = new Mock <IObjectStore>(); var brokerage = new Mock <IBrokerage>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(getHoldings); brokerage.Setup(x => x.GetOpenOrders()).Returns(getOrders); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); var result = setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider)); Assert.AreEqual(expected, result); foreach (var symbol in algorithm.Securities.Keys) { var configs = algorithm.SubscriptionManager.SubscriptionDataConfigService.GetSubscriptionDataConfigs(symbol); Assert.AreEqual(algorithm.UniverseSettings.Resolution, configs.First().Resolution); } }
public void EnforcesTotalPortfolioValue(bool fails) { var algorithm = new TestAlgorithm(); algorithm.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage); algorithm.SetHistoryProvider(new TestHistoryProvider()); var job = GetJob(); job.BrokerageData[BrokerageSetupHandler.MaxAllocationLimitConfig] = fails ? "1" : "1000000000"; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount> { new CashAmount(10000, Currencies.USD), new CashAmount(11, Currencies.GBP) }); brokerage.Setup(x => x.GetAccountHoldings()).Returns(new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); Assert.AreEqual(!fails, setupHandler.Setup(new SetupHandlerParameters(algorithm.DataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider))); Assert.AreEqual(10000, algorithm.Portfolio.CashBook[Currencies.USD].Amount); Assert.AreEqual(11, algorithm.Portfolio.CashBook[Currencies.GBP].Amount); }
public void HasErrorWithZeroTotalPortfolioValue(bool hasCashBalance, bool hasHoldings) { var algorithm = new TestAlgorithm(); algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = new LiveNodePacket { UserId = 1, ProjectId = 1, DeployId = "1", Brokerage = "TestBrokerage", DataQueueHandler = "none", Controls = new Controls { RamAllocation = 4096 } // no real limit }; var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var brokerage = new Mock <IBrokerage>(); var objectStore = new Mock <IObjectStore>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.GetCashBalance()).Returns( hasCashBalance ? new List <CashAmount> { new CashAmount(1000, "USD") } : new List <CashAmount>() ); brokerage.Setup(x => x.GetAccountHoldings()).Returns( hasHoldings ? new List <Holding> { new Holding { Type = SecurityType.Equity, Symbol = Symbols.SPY, Quantity = 1, AveragePrice = 100, MarketPrice = 100 } } : new List <Holding>()); brokerage.Setup(x => x.GetOpenOrders()).Returns(new List <Order>()); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); var dataManager = new DataManagerStub(algorithm, new MockDataFeed(), true); Assert.IsTrue(setupHandler.Setup(new SetupHandlerParameters(dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object))); if (hasCashBalance || hasHoldings) { Assert.AreEqual(0, algorithm.DebugMessages.Count); } else { Assert.AreEqual(1, algorithm.DebugMessages.Count); Assert.That(algorithm.DebugMessages.First().Contains("No cash balances or holdings were found in the brokerage account.")); } }
public void ExistingHoldingsAndOrdersUniverseSettings(Func <List <Holding> > getHoldings, Func <List <Order> > getOrders, bool expected) { // Set our universe settings var hasCrypto = false; try { hasCrypto = getHoldings().Any(x => x.Symbol.Value == "BTCUSD"); } catch { } var algorithm = new TestAlgorithm { UniverseSettings = { Resolution = Resolution.Daily, Leverage = (hasCrypto ? 1 : 20), FillForward = false, ExtendedMarketHours = true } }; algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); var job = GetJob(); var resultHandler = new Mock <IResultHandler>(); var transactionHandler = new Mock <ITransactionHandler>(); var realTimeHandler = new Mock <IRealTimeHandler>(); var objectStore = new Mock <IObjectStore>(); var brokerage = new Mock <IBrokerage>(); brokerage.Setup(x => x.IsConnected).Returns(true); brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); brokerage.Setup(x => x.GetCashBalance()).Returns(new List <CashAmount>()); brokerage.Setup(x => x.GetAccountHoldings()).Returns(getHoldings); brokerage.Setup(x => x.GetOpenOrders()).Returns(getOrders); var setupHandler = new BrokerageSetupHandler(); IBrokerageFactory factory; setupHandler.CreateBrokerage(job, algorithm, out factory); var result = setupHandler.Setup(new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, transactionHandler.Object, realTimeHandler.Object, objectStore.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider)); if (result != expected) { Assert.Fail("SetupHandler result did not match expected value"); } foreach (var symbol in algorithm.Securities.Keys) { var config = algorithm.SubscriptionManager.SubscriptionDataConfigService.GetSubscriptionDataConfigs(symbol).First(); // Assert Resolution and FillForward settings persisted Assert.AreEqual(algorithm.UniverseSettings.Resolution, config.Resolution); Assert.AreEqual(algorithm.UniverseSettings.FillForward, config.FillDataForward); // Assert ExtendedHours setting persisted for equities if (config.SecurityType == SecurityType.Equity) { Assert.AreEqual(algorithm.UniverseSettings.ExtendedMarketHours, config.ExtendedMarketHours); } // Assert Leverage setting persisted for non options or futures (Blocked from setting leverage in Security.SetLeverage()) if (!symbol.SecurityType.IsOption() && symbol.SecurityType != SecurityType.Future) { var security = algorithm.Securities[symbol]; Assert.AreEqual(algorithm.UniverseSettings.Leverage, security.Leverage); } } }