/// <summary> /// Live trading result handler thread. /// </summary> public void Run() { // -> 1. Run Primary Sender Loop: Continually process messages from queue as soon as they arrive. while (!(_exitTriggered && Messages.Count == 0)) { try { //1. Process Simple Messages in Queue Packet packet; if (Messages.TryDequeue(out packet)) { _messagingHandler.Send(packet); } //2. Update the packet scanner: Update(); if (Messages.Count == 0) { // prevent thread lock/tight loop when there's no work to be done Thread.Sleep(10); } } catch (Exception err) { Log.Error(err); } } // While !End. Log.Trace("LiveTradingResultHandler.Run(): Ending Thread..."); _isActive = false; } // End Run();
/// <summary> /// The main processing method steps through the messaging queue and processes the messages one by one. /// </summary> public void Run() { //Initialize: var lastMessage = ""; _lastDaysProcessed = 5; //Setup minimum result arrays: //SampleEquity(job.periodStart, job.startingCapital); //SamplePerformance(job.periodStart, 0); try { while (!(_exitTriggered && Messages.Count == 0)) { //While there's no work to do, go back to the algorithm: if (Messages.Count == 0) { Thread.Sleep(50); } else { //1. Process Simple Messages in Queue Packet packet; if (Messages.TryDequeue(out packet)) { _messagingHandler.Send(packet); } } //2. Update the packet scanner: Update(); } // While !End. } catch (Exception err) { // unexpected error, we need to close down shop Log.Error(err); // quit the algorithm due to error _algorithm.RunTimeError = err; } Log.Trace("BacktestingResultHandler.Run(): Ending Thread..."); _isActive = false; // reset standard out/error Console.SetOut(StandardOut); Console.SetError(StandardError); } // End Run();
private void MessagingUpdateIntervalElapsed(object state) { try { _timer.Change(Timeout.Infinite, Timeout.Infinite); try { Insight insight; var insights = new List <Insight>(); while (insights.Count < _maximumNumberOfInsightsPerPacket && _insights.TryDequeue(out insight)) { insights.Add(insight); } if (insights.Count > 0) { _messagingHandler.Send(new AlphaResultPacket(_job.AlgorithmId, _job.UserId, insights)); } } catch (Exception err) { Log.Error(err); } _timer.Change(_interval, _interval); } catch (ObjectDisposedException) { // pass. The timer callback can be called even after disposed } }
private void MessagingUpdateIntervalElapsed(object state) { try { Insight insight; var insights = new List <Insight>(); while (insights.Count < _maximumNumberOfInsightsPerPacket && _insights.TryDequeue(out insight)) { insights.Add(insight); } if (insights.Count > 0) { _messagingHandler.Send(new AlphaResultPacket(_job.AlgorithmId, _job.UserId, insights)); } } catch (Exception err) { Log.Error(err); } finally { try { var nextDueTime = Time.GetSecondUnevenWait((int)Math.Ceiling(_interval.TotalMilliseconds)); _timer.Change(nextDueTime, Timeout.Infinite); } catch (ObjectDisposedException) { // ignored disposed } } }
/// <summary> /// Performs asynchronous processing, including broadcasting of alphas to messaging handler /// </summary> protected void ProcessAsynchronousEvents() { Packet packet; while (_messages.TryDequeue(out packet)) { _messagingHandler.Send(packet); } // persist generated alphas to storage if (DateTime.UtcNow > _nextPersistenceUpdate) { StoreAlphas(); _nextPersistenceUpdate = DateTime.UtcNow + PersistenceUpdateInterval; } // push updated alphas through messaging handler if (DateTime.UtcNow > _nextMessagingUpdate) { var alphas = AlphaManager.GetUpdatedContexts().Select(context => context.Alpha).ToList(); if (alphas.Count > 0) { _messages.Enqueue(new AlphaResultPacket { AlgorithmId = AlgorithmId, Alphas = alphas }); } _nextMessagingUpdate = DateTime.UtcNow + MessagingUpdateInterval; } }
/// <summary> /// The main processing method steps through the messaging queue and processes the messages one by one. /// </summary> public void Run() { //Initialize: var lastMessage = ""; _lastDaysProcessed = 5; //Setup minimum result arrays: //SampleEquity(job.periodStart, job.startingCapital); //SamplePerformance(job.periodStart, 0); try { while (!(_exitTriggered && Messages.Count == 0)) { //While there's no work to do, go back to the algorithm: if (Messages.Count == 0) { Thread.Sleep(50); } else { //1. Process Simple Messages in Queue Packet packet; if (Messages.TryDequeue(out packet)) { switch (packet.Type) { //New Debug Message: case PacketType.Debug: var debug = packet as DebugPacket; if (lastMessage != debug.Message) { //Log.Trace("BacktestingResultHandlerRun(): Debug Packet: " + debug.Message); _messagingHandler.DebugMessage(debug.Message, debug.ProjectId, _backtestId, _compileId); lastMessage = debug.Message; } break; //Send log messages to the browser as well for live trading: case PacketType.SecurityTypes: var securityPacket = packet as SecurityTypesPacket; _messagingHandler.SecurityTypes(securityPacket); break; case PacketType.RuntimeError: //Log.Error("QC.AlgorithmWorker.Run(): " + packet.Message); var runtime = packet as RuntimeErrorPacket; _messagingHandler.RuntimeError(_backtestId, runtime.Message, runtime.StackTrace); break; case PacketType.HandledError: var handled = packet as HandledErrorPacket; Log.Error("BacktestingResultHandler.Run(): HandledError Packet: " + handled.Message); _messagingHandler.Send(handled); break; default: //Default case.. _messagingHandler.Send(packet); Log.Trace("BacktestingResultHandler.Run(): Default packet type: " + packet.Type); break; } } } //2. Update the packet scanner: Update(); } // While !End. } catch (Exception err) { // unexpected error, we need to close down shop Log.Error(err); // quit the algorithm due to error _algorithm.RunTimeError = err; } Log.Trace("BacktestingResultHandler.Run(): Ending Thread..."); _isActive = false; } // End Run();