|
36 | 36 | using QuantConnect.Tests.Common.Securities;
|
37 | 37 | using QuantConnect.Tests.Engine.DataFeeds;
|
38 | 38 | using QuantConnect.Util;
|
| 39 | +using QuantConnect.Algorithm.CSharp; |
39 | 40 |
|
40 | 41 | namespace QuantConnect.Tests.Engine.Setup
|
41 | 42 | {
|
@@ -272,6 +273,38 @@ public void LoadsExistingHoldingsAndOrders(Func<List<Holding>> getHoldings, Func
|
272 | 273 | }
|
273 | 274 | }
|
274 | 275 |
|
| 276 | + [TestCaseSource(nameof(GetExistingHoldingsAndOrdersWithCustomDataTestCase))] |
| 277 | + public void LoadsExistingHoldingsAndOrdersWithCustomData(Func<List<Holding>> getHoldings, Func<List<Order>> getOrders, bool expected) |
| 278 | + { |
| 279 | + var algorithm = new TestAlgorithm(); |
| 280 | + algorithm.AddData<CustomDataBitcoinAlgorithm.Bitcoin>("BTC"); |
| 281 | + algorithm.SetHistoryProvider(new BrokerageTransactionHandlerTests.BrokerageTransactionHandlerTests.EmptyHistoryProvider()); |
| 282 | + var job = GetJob(); |
| 283 | + |
| 284 | + var resultHandler = new Mock<IResultHandler>(); |
| 285 | + var transactionHandler = new Mock<ITransactionHandler>(); |
| 286 | + var realTimeHandler = new Mock<IRealTimeHandler>(); |
| 287 | + var objectStore = new Mock<IObjectStore>(); |
| 288 | + var brokerage = new Mock<IBrokerage>(); |
| 289 | + |
| 290 | + brokerage.Setup(x => x.IsConnected).Returns(true); |
| 291 | + brokerage.Setup(x => x.AccountBaseCurrency).Returns(Currencies.USD); |
| 292 | + brokerage.Setup(x => x.GetCashBalance()).Returns(new List<CashAmount>()); |
| 293 | + brokerage.Setup(x => x.GetAccountHoldings()).Returns(getHoldings); |
| 294 | + brokerage.Setup(x => x.GetOpenOrders()).Returns(getOrders); |
| 295 | + |
| 296 | + var setupHandler = new TestBrokerageSetupHandler(); |
| 297 | + |
| 298 | + IBrokerageFactory factory; |
| 299 | + setupHandler.CreateBrokerage(job, algorithm, out factory); |
| 300 | + |
| 301 | + var parameters = new SetupHandlerParameters(_dataManager.UniverseSelection, algorithm, brokerage.Object, job, resultHandler.Object, |
| 302 | + transactionHandler.Object, realTimeHandler.Object, TestGlobals.DataCacheProvider, TestGlobals.MapFileProvider); |
| 303 | + var result = setupHandler.TestLoadExistingHoldingsAndOrders(brokerage.Object, algorithm, parameters); |
| 304 | + |
| 305 | + Assert.AreEqual(expected, result); |
| 306 | + } |
| 307 | + |
275 | 308 | [TestCase(true)]
|
276 | 309 | [TestCase(false)]
|
277 | 310 | public void EnforcesTotalPortfolioValue(bool fails)
|
@@ -593,6 +626,16 @@ public void HasErrorWithZeroTotalPortfolioValue(bool hasCashBalance, bool hasHol
|
593 | 626 | }
|
594 | 627 | }
|
595 | 628 |
|
| 629 | + private static object[] GetExistingHoldingsAndOrdersWithCustomDataTestCase = |
| 630 | + { |
| 631 | + new object[] { |
| 632 | + new Func<List<Holding>>(() => new List<Holding> { new Holding { Symbol = new Symbol( |
| 633 | + SecurityIdentifier.GenerateBase(typeof(CustomDataBitcoinAlgorithm.Bitcoin), "BTC", Market.USA, false), "BTC"), Quantity = 1 }}), |
| 634 | + new Func<List<Order>>(() => new List<Order> { new LimitOrder(new Symbol( |
| 635 | + SecurityIdentifier.GenerateBase(typeof(CustomDataBitcoinAlgorithm.Bitcoin), "BTC", Market.USA, false), "BTC"), 1, 1, DateTime.UtcNow)}), |
| 636 | + true } |
| 637 | + }; |
| 638 | + |
596 | 639 | private static TestCaseData[] GetExistingHoldingsAndOrdersTestCaseData()
|
597 | 640 | {
|
598 | 641 | return new[]
|
@@ -884,4 +927,12 @@ public override void Disconnect()
|
884 | 927 |
|
885 | 928 | #endregion
|
886 | 929 | }
|
| 930 | + |
| 931 | + public class TestBrokerageSetupHandler: BrokerageSetupHandler |
| 932 | + { |
| 933 | + public bool TestLoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algorithm, SetupHandlerParameters parameters) |
| 934 | + { |
| 935 | + return LoadExistingHoldingsAndOrders(brokerage, algorithm, parameters); |
| 936 | + } |
| 937 | + } |
887 | 938 | }
|
0 commit comments