/// <summary> /// Setup the algorithm cash, dates and portfolio as desired. /// </summary> /// <param name="algorithm">Existing algorithm instance</param> /// <param name="brokerage">New brokerage instance</param> /// <param name="baseJob">Backtesting job</param> /// <returns>Boolean true on successfully setting up the console.</returns> public bool Setup(IAlgorithm algorithm, out IBrokerage brokerage, AlgorithmNodePacket baseJob) { var initializeComplete = false; brokerage = new BacktestingBrokerage(algorithm); try { //Set common variables for console programs: if (baseJob.Type == PacketType.BacktestNode) { var backtestJob = baseJob as BacktestNodePacket; //Set the limits on the algorithm assets (for local no limits) algorithm.SetAssetLimits(999, 999, 999); algorithm.SetMaximumOrders(int.MaxValue); //Setup Base Algorithm: algorithm.Initialize(); //Add currency data feeds that weren't explicity added in Initialize algorithm.Portfolio.CashBook.EnsureCurrencyDataFeeds(algorithm.Securities, algorithm.SubscriptionManager); //Construct the backtest job packet: backtestJob.PeriodStart = algorithm.StartDate; backtestJob.PeriodFinish = algorithm.EndDate; backtestJob.BacktestId = "LOCALHOST"; backtestJob.UserId = 1001; backtestJob.Type = PacketType.BacktestNode; //Backtest Specific Parameters: StartingDate = backtestJob.PeriodStart; StartingPortfolioValue = algorithm.Portfolio.Cash; } else { var liveJob = baseJob as LiveNodePacket; //Live Job Parameters: liveJob.DeployId = "LOCALHOST"; liveJob.Type = PacketType.LiveNode; //Call in the brokerage setup: var setup = new BrokerageSetupHandler(); setup.Setup(algorithm, out brokerage, baseJob); //Live Specific Parameters: StartingDate = DateTime.Now; StartingPortfolioValue = algorithm.Portfolio.Cash; } } catch (Exception err) { Log.Error("ConsoleSetupHandler().Setup(): " + err.Message); Errors.Add("Failed to initialize algorithm: Initialize(): " + err.Message); } if (Errors.Count == 0) { initializeComplete = true; } return(initializeComplete); }
/// <summary> /// Get the setup handler for this algorithm, depending on its use case. /// </summary> /// <param name="setupMethod">Setup handler</param> /// <returns>Instance of a setup handler:</returns> private static ISetupHandler GetSetupHandler(SetupHandlerEndpoint setupMethod) { var sh = default(ISetupHandler); switch (setupMethod) { //Setup console handler: case SetupHandlerEndpoint.Console: sh = new ConsoleSetupHandler(); Log.Trace("Engine.GetSetupHandler(): Selected Console Algorithm Setup Handler."); break; //Default, backtesting result handler: case SetupHandlerEndpoint.Backtesting: sh = new BacktestingSetupHandler(); Log.Trace("Engine.GetSetupHandler(): Selected Backtesting Algorithm Setup Handler."); break; case SetupHandlerEndpoint.Brokerage: sh = new BrokerageSetupHandler(); Log.Trace("Engine.GetSetupHandler(): Selected Brokerage Algorithm Setup Handler."); break; } return sh; }
/// <summary> /// Setup the algorithm cash, dates and portfolio as desired. /// </summary> /// <param name="algorithm">Existing algorithm instance</param> /// <param name="brokerage">New brokerage instance</param> /// <param name="baseJob">Backtesting job</param> /// <returns>Boolean true on successfully setting up the console.</returns> public bool Setup(IAlgorithm algorithm, out IBrokerage brokerage, AlgorithmNodePacket baseJob) { var initializeComplete = false; try { //Set common variables for console programs: if (baseJob.Type == PacketType.BacktestNode) { var backtestJob = baseJob as BacktestNodePacket; //Set the limits on the algorithm assets (for local no limits) algorithm.SetAssetLimits(999, 999, 999); algorithm.SetMaximumOrders(int.MaxValue); //Setup Base Algorithm: algorithm.Initialize(); //Add currency data feeds that weren't explicity added in Initialize algorithm.Portfolio.CashBook.EnsureCurrencyDataFeeds(algorithm.Securities, algorithm.SubscriptionManager); //Construct the backtest job packet: backtestJob.PeriodStart = algorithm.StartDate; backtestJob.PeriodFinish = algorithm.EndDate; backtestJob.BacktestId = "LOCALHOST"; backtestJob.UserId = 1001; backtestJob.Type = PacketType.BacktestNode; //Backtest Specific Parameters: StartingDate = backtestJob.PeriodStart; StartingPortfolioValue = algorithm.Portfolio.Cash; } else { var liveJob = baseJob as LiveNodePacket; //Live Job Parameters: liveJob.DeployId = "LOCALHOST"; liveJob.Type = PacketType.LiveNode; //Call in the brokerage setup: var setup = new BrokerageSetupHandler(); setup.Setup(algorithm, out brokerage, baseJob); //Live Specific Parameters: StartingDate = DateTime.Now; StartingPortfolioValue = algorithm.Portfolio.Cash; } } catch (Exception err) { Log.Error("ConsoleSetupHandler().Setup(): " + err.Message); Errors.Add("Failed to initialize algorithm: Initialize(): " + err.Message); } if (Errors.Count == 0) { initializeComplete = true; } // we need to do this after algorithm initialization brokerage = new BacktestingBrokerage(algorithm); // set the transaction models base on the requested brokerage properties SetupHandler.UpdateTransactionModels(algorithm, algorithm.BrokerageModel); return initializeComplete; }