public void CurrencyConversionRateResolved() { // Unit test to prove that in the event that default resolution (minute) history request returns // no data for our currency conversion that BaseSetupHandler will use a daily history request // to determine the the conversion rate if possible. // Setup history provider and algorithm var historyProvider = new SubscriptionDataReaderHistoryProvider(); var zipCache = new ZipDataCacheProvider(new DefaultDataProvider()); historyProvider.Initialize(new HistoryProviderInitializeParameters( null, null, TestGlobals.DataProvider, zipCache, TestGlobals.MapFileProvider, TestGlobals.FactorFileProvider, null, false, new DataPermissionManager())); var algorithm = new BrokerageSetupHandlerTests.TestAlgorithm { UniverseSettings = { Resolution = Resolution.Minute } }; algorithm.SetHistoryProvider(historyProvider); // Pick a date range where we do NOT have BTCUSD minute data algorithm.SetStartDate(2015, 1, 24); algorithm.SetCash("USD", 0); algorithm.SetCash("BTC", 10); // Have BaseSetupHandler resolve the currency conversion BaseSetupHandler.SetupCurrencyConversions(algorithm, algorithm.DataManager.UniverseSelection); // Assert that our portfolio has some value and that value is bitcoin Assert.IsTrue(algorithm.Portfolio.Cash > 0); Assert.IsTrue(algorithm.Portfolio.CashBook["BTC"].ValueInAccountCurrency > 0); zipCache.DisposeSafely(); }
/// <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); } } }