/// <summary> /// Launch the Lean Engine Primary Form: /// </summary> /// <param name="engine">Accept the engine instance we just launched</param> public LeanEngineWinForm(Engine engine) { _engine = engine; //Setup the State: _resultsHandler = engine.AlgorithmHandlers.Results; //Create Form: Text = "QuantConnect Lean Algorithmic Trading Engine: v" + Constants.Version; Size = new Size(1024,768); MinimumSize = new Size(1024, 768); CenterToScreen(); WindowState = FormWindowState.Maximized; Icon = new Icon("../../../lean.ico"); //Setup Console Log Area: _console = new RichTextBox(); _console.Parent = this; _console.ReadOnly = true; _console.Multiline = true; _console.Location = new Point(0, 0); _console.Dock = DockStyle.Fill; _console.KeyUp += ConsoleOnKeyUp; //Form Events: Closed += OnClosed; //Setup Polling Events: _polling = new Timer(); _polling.Interval = 1000; _polling.Tick += PollingOnTick; _polling.Start(); }
private static void LaunchLean() { /* Config.Set("environment", "backtesting"); * string algorithm = (string)AppDomain.CurrentDomain.GetData("AlgorithmTypeName"); * string path = (string)AppDomain.CurrentDomain.GetData("AlgorithmLocation"); * * Config.Set("algorithm-type-name", algorithm); * if (!string.IsNullOrEmpty(path)) * { * Config.Set("algorithm-location", path); * } */ // Config.Set("result-handler", "BacktestingResultHandler"); Config.Set("messaging-handler", "QuantConnect.Lean.TestProject.Messaging"); var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance); systemHandlers.Initialize(); Log.LogHandler = Composer.Instance.GetExportedValueByTypeName <ILogHandler>(Config.Get("log-handler", "CompositeLogHandler")); //Log.DebuggingEnabled = false; //Log.DebuggingLevel = 1; LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers; try { leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance); _resultsHandler = (BacktestingResultHandler)leanEngineAlgorithmHandlers.Results; } catch (CompositionException compositionException) { Log.Error("Engine.Main(): Failed to load library: " + compositionException); throw; } string algorithmPath; AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath); try { var _engine = new QuantConnect.Lean.Engine.Engine(systemHandlers, leanEngineAlgorithmHandlers, Config.GetBool("live-mode")); _engine.Run(job, algorithmPath); } finally { Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId); // clean up resources systemHandlers.Dispose(); leanEngineAlgorithmHandlers.Dispose(); Log.LogHandler.Dispose(); } }
/// <summary> /// Launch the LEAN Engine in a separate thread. /// </summary> private static Engine LaunchLean() { //Launch the Lean Engine in another thread: this will run the algorithm specified above. // TODO > This should only be launched when clicking a backtest/trade live button provided in the UX. var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance); var algorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance); var engine = new Engine(systemHandlers, algorithmHandlers, Config.GetBool("live-mode")); _leanEngineThread = new Thread(() => { string algorithmPath; var job = systemHandlers.JobQueue.NextJob(out algorithmPath); engine.Run(job, algorithmPath); systemHandlers.JobQueue.AcknowledgeJob(job); }); _leanEngineThread.Start(); return engine; }
/// <summary> /// Primary Analysis Thread: /// </summary> public static void Main(string[] args) { Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "CompositeLogHandler")); //Initialize: string mode = "RELEASE"; var liveMode = Config.GetBool("live-mode"); Log.DebuggingEnabled = Config.GetBool("debug-mode"); #if DEBUG mode = "DEBUG"; #endif //Name thread for the profiler: Thread.CurrentThread.Name = "Algorithm Analysis Thread"; Log.Trace("Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v" + Constants.Version + " Mode: " + mode); Log.Trace("Engine.Main(): Started " + DateTime.Now.ToShortTimeString()); Log.Trace("Engine.Main(): Memory " + OS.ApplicationMemoryUsed + "Mb-App " + +OS.TotalPhysicalMemoryUsed + "Mb-Used " + OS.TotalPhysicalMemory + "Mb-Total"); //Import external libraries specific to physical server location (cloud/local) LeanEngineSystemHandlers leanEngineSystemHandlers; try { leanEngineSystemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance); } catch (CompositionException compositionException) { Log.Error("Engine.Main(): Failed to load library: " + compositionException); throw; } //Setup packeting, queue and controls system: These don't do much locally. leanEngineSystemHandlers.Initialize(); //-> Pull job from QuantConnect job queue, or, pull local build: string assemblyPath; var job = leanEngineSystemHandlers.JobQueue.NextJob(out assemblyPath); if (job == null) { throw new Exception("Engine.Main(): Job was null."); } LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers; try { leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance); } catch (CompositionException compositionException) { Log.Error("Engine.Main(): Failed to load library: " + compositionException); throw; } // log the job endpoints Log.Trace("JOB HANDLERS: "); Log.Trace(" DataFeed: " + leanEngineAlgorithmHandlers.DataFeed.GetType().FullName); Log.Trace(" Setup: " + leanEngineAlgorithmHandlers.Setup.GetType().FullName); Log.Trace(" RealTime: " + leanEngineAlgorithmHandlers.RealTime.GetType().FullName); Log.Trace(" Results: " + leanEngineAlgorithmHandlers.Results.GetType().FullName); Log.Trace(" Transactions: " + leanEngineAlgorithmHandlers.Transactions.GetType().FullName); // if the job version doesn't match this instance version then we can't process it // we also don't want to reprocess redelivered jobs if (job.Version != Constants.Version || job.Redelivered) { Log.Error("Engine.Run(): Job Version: " + job.Version + " Deployed Version: " + Constants.Version + " Redelivered: " + job.Redelivered); //Tiny chance there was an uncontrolled collapse of a server, resulting in an old user task circulating. //In this event kill the old algorithm and leave a message so the user can later review. leanEngineSystemHandlers.JobQueue.AcknowledgeJob(job); leanEngineSystemHandlers.Api.SetAlgorithmStatus(job.AlgorithmId, AlgorithmStatus.RuntimeError, _collapseMessage); leanEngineSystemHandlers.Notify.SetChannel(job.Channel); leanEngineSystemHandlers.Notify.RuntimeError(job.AlgorithmId, _collapseMessage); return; } try { var engine = new Engine(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, liveMode); engine.Run(job, assemblyPath); } finally { //Delete the message from the job queue: leanEngineSystemHandlers.JobQueue.AcknowledgeJob(job); Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId); // clean up resources leanEngineSystemHandlers.Dispose(); leanEngineAlgorithmHandlers.Dispose(); Log.LogHandler.Dispose(); } }
/// <summary> /// Launches a Lean Engine using a parameter /// </summary> /// <param name="val">The paramater to use when launching lean. </param> private void LaunchLean(string val) { Config.Set("environment", "backtesting"); string algorithm = val; // Set the algorithm in Config. Here is where you can customize Config settings Config.Set("algorithm-type-name", algorithm); _jobQueue = new JobQueue(); _notify = new Messaging(); _api = new Api(); /************ Comment one of the two following lines to select which ResultHandler to use ***********/ _resultshandler = new OptimizationResultHandler(); //_resultshandler = new ConsoleResultHandler(); _dataFeed = new FileSystemDataFeed(); _setup = new ConsoleSetupHandler(); _realTime = new BacktestingRealTimeHandler(); _historyProvider = new SubscriptionDataReaderHistoryProvider(); _transactions = new BacktestingTransactionHandler(); // Set the Log.LogHandler to only write to the log.txt file. // This setting avoids writing Log messages to the console. Log.LogHandler = (ILogHandler)new FileLogHandler(); Log.DebuggingEnabled = false; // Set this property to true for lots of messages Log.DebuggingLevel = 1; // A reminder that the default level for Log.Debug message is 1 var systemHandlers = new LeanEngineSystemHandlers(_jobQueue, _api, _notify); systemHandlers.Initialize(); var algorithmHandlers = new LeanEngineAlgorithmHandlers(_resultshandler, _setup, _dataFeed, _transactions, _realTime, _historyProvider); string algorithmPath; AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath); try { var _engine = new Engine(systemHandlers, algorithmHandlers, Config.GetBool("live-mode")); _engine.Run(job, algorithmPath); } finally { /* The JobQueue.AcknowledgeJob only asks for any key to close the window. * We do not want that behavior, so we comment out this line so that multiple Leans will run * * The alternative is to comment out Console.Read(); the line in JobQueue class. */ //systemHandlers.JobQueue.AcknowledgeJob(job); Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId); // clean up resources systemHandlers.Dispose(); algorithmHandlers.Dispose(); Log.LogHandler.Dispose(); } }
private void LaunchLean() { Config.Set("environment", "backtesting"); string algorithm = "EMATest"; Config.Set("algorithm-type-name", algorithm); //string datapath = Config.Get("data-folder"); _jobQueue = new JobQueue(); _notify = new Messaging(); _api = new Api(); _resultshandler = new OptimizationResultHandler(); //_resultshandler = new ConsoleResultHandler(); _dataFeed = new FileSystemDataFeed(); _setup = new ConsoleSetupHandler(); _realTime = new BacktestingRealTimeHandler(); _historyProvider = new SubscriptionDataReaderHistoryProvider(); _transactions = new BacktestingTransactionHandler(); Log.LogHandler = (ILogHandler)new FileLogHandler(); Log.DebuggingEnabled = false; Log.DebuggingLevel = 1; var systemHandlers = new LeanEngineSystemHandlers(_jobQueue, _api, _notify); systemHandlers.Initialize(); var algorithmHandlers = new LeanEngineAlgorithmHandlers(_resultshandler, _setup, _dataFeed, _transactions, _realTime, _historyProvider); string algorithmPath; AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath); try { var _engine = new Engine(systemHandlers, algorithmHandlers, Config.GetBool("live-mode")); _engine.Run(job, algorithmPath); } finally { //Delete the message from the job queue: //systemHandlers.JobQueue.AcknowledgeJob(job); Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId); // clean up resources systemHandlers.Dispose(); algorithmHandlers.Dispose(); Log.LogHandler.Dispose(); } }
/// <summary> /// Launch the LEAN Engine in a separate thread. /// </summary> public static Engine LaunchLean() { //Launch the Lean Engine in another thread: this will run the algorithm specified above. var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance); var algorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance); var engine = new Engine(systemHandlers, algorithmHandlers, Config.GetBool("live-mode")); _leanEngineThread = new Thread(() => { string algorithmPath; var job = systemHandlers.JobQueue.NextJob(out algorithmPath); engine.Run(job, algorithmPath); systemHandlers.JobQueue.AcknowledgeJob(job); }); _leanEngineThread.Start(); return engine; }
private void LaunchLean() { Config.Set ("environment", "desktop"); string algorithm = "EMATest"; Config.Set("algorithm-type-name", algorithm); _jobQueue = new JobQueue (); _notify = new Messaging (); _api = new Api(); _resultshandler = new DesktopResultHandler (); _dataFeed = new FileSystemDataFeed (); _setup = new ConsoleSetupHandler (); _realTime = new BacktestingRealTimeHandler (); _historyProvider = new SubscriptionDataReaderHistoryProvider (); _transactions = new BacktestingTransactionHandler (); var systemHandlers = new LeanEngineSystemHandlers (_jobQueue, _api, _notify); systemHandlers.Initialize (); var algorithmHandlers = new LeanEngineAlgorithmHandlers (_resultshandler, _setup, _dataFeed, _transactions, _realTime, _historyProvider); var _engine = new Engine (systemHandlers, algorithmHandlers, Config.GetBool ("live-mode")); string algorithmPath; var job = systemHandlers.JobQueue.NextJob(out algorithmPath); _engine.Run(job, algorithmPath); }
private void LaunchLean() { Config.Set ("environment", "backtesting"); string algorithm = "EMATest"; Config.Set("algorithm-type-name", algorithm); _jobQueue = new JobQueue (); _notify = new Messaging (); _api = new Api(); _resultshandler = new DesktopResultHandler (); _dataFeed = new FileSystemDataFeed (); _setup = new ConsoleSetupHandler (); _realTime = new BacktestingRealTimeHandler (); _historyProvider = new SubscriptionDataReaderHistoryProvider (); _transactions = new BacktestingTransactionHandler (); var systemHandlers = new LeanEngineSystemHandlers (_jobQueue, _api, _notify); systemHandlers.Initialize (); // var algorithmHandlers = new LeanEngineAlgorithmHandlers (_resultshandler, _setup, _dataFeed, _transactions, _realTime, _historyProvider); Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "CompositeLogHandler")); LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers; try { leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance); _resultshandler = leanEngineAlgorithmHandlers.Results; } catch (CompositionException compositionException) { Log.Error("Engine.Main(): Failed to load library: " + compositionException); throw; } string algorithmPath; AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath); try { var _engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, Config.GetBool("live-mode")); _engine.Run(job, algorithmPath); } finally { //Delete the message from the job queue: //systemHandlers.JobQueue.AcknowledgeJob(job); Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId); // clean up resources systemHandlers.Dispose(); leanEngineAlgorithmHandlers.Dispose(); Log.LogHandler.Dispose(); } }