Skip to content

Commit

Permalink
Rework GetResultHandler to use type loader; first desktop UX implemen…
Browse files Browse the repository at this point in the history
…tation; functional logger
  • Loading branch information
jaredbroad committed Jun 7, 2015
1 parent a2f630e commit ca4a49e
Show file tree
Hide file tree
Showing 23 changed files with 1,094 additions and 90 deletions.
13 changes: 0 additions & 13 deletions Common/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,6 @@ public enum RealTimeEndpoint
LiveTrading
}

/// <summary>
/// Result events handler options for processing algorithm messages
/// </summary>
public enum ResultHandlerEndpoint
{
/// Send Results to the Backtesting Web Application
Backtesting,
/// Send the Results to the Local Console
Console,
/// Send Results to the Live Web Application
LiveTrading,
}

/// <summary>
/// Setup handler options for setting up algorithm state and the livetrading/backtest requirements.
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions Common/Packets/AlgorithmNodePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ public string AlgorithmId
/// <summary>
/// Result endpoint plugin to select for task
/// </summary>
[JsonProperty(PropertyName = "eResultEndpoint")]
public ResultHandlerEndpoint ResultEndpoint = ResultHandlerEndpoint.Backtesting;
/// <remarks>
/// DEPRECATED: Maintained here for temporary consistency. Eventually all the endpoint enums will be replaced with MEF / Type import loading by config.
/// </remarks>
//[JsonProperty(PropertyName = "eResultEndpoint")]
//public ResultHandlerEndpoint ResultEndpoint = ResultHandlerEndpoint.Backtesting;

/// <summary>
/// Setup handler endpoint for this task
Expand Down
2 changes: 1 addition & 1 deletion Common/Properties/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyCompany("QuantConnect")]
[assembly: AssemblyTrademark("QuantConnect")]
[assembly: AssemblyVersion("2.1.1.33")]
[assembly: AssemblyVersion("2.1.1.35")]
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
Expand Down
28 changes: 7 additions & 21 deletions Engine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,28 +577,14 @@ private static ITransactionHandler GetTransactionHandler(IAlgorithm algorithm, I
/// <returns>Class Matching IResultHandler Inteface</returns>
private static IResultHandler GetResultHandler(AlgorithmNodePacket job)
{
var rh = default(IResultHandler);
var resultHandler = Config.Get("result-handler"); //, "BacktestingResultHandler"
var rhoh = Activator.CreateInstance(null, resultHandler);
var rh = (IResultHandler)rhoh.Unwrap();

switch (job.ResultEndpoint)
{
//Local backtesting and live trading result handler route messages to the local console.
case ResultHandlerEndpoint.Console:
Log.Trace("Engine.GetResultHandler(): Selected Console Output.");
rh = new ConsoleResultHandler(job);
break;

// Backtesting route messages to user browser.
case ResultHandlerEndpoint.Backtesting:
Log.Trace("Engine.GetResultHandler(): Selected Backtesting API Result Endpoint.");
rh = new BacktestingResultHandler((BacktestNodePacket)job);
break;

// Live trading route messages to user's browser.
case ResultHandlerEndpoint.LiveTrading:
Log.Trace("Engine.GetResultHandler(): Selected Live Trading API Result Endpoint.");
rh = new LiveTradingResultHandler((LiveNodePacket)job);
break;
}
// Composer.Instance.GetExportedValueByTypeName<IResultHandler>(resultHandler);

rh.Initialize(job);
Log.Trace("Engine.GetResultHandler(): Loaded and Initialized Result Handler: " + resultHandler + " v" + Constants.Version);
return rh;
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/QuantConnect.Lean.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<Compile Include="RealTime\LiveTradingRealTimeHandler.cs" />
<Compile Include="RealTime\BacktestingRealTimeHandler.cs" />
<Compile Include="RealTime\IRealTimeHandler.cs" />
<Compile Include="Results\DesktopResultHandler.cs" />
<Compile Include="Setup\BrokerageSetupHandler.cs" />
<Compile Include="Setup\SetupHandler.cs" />
<Compile Include="StateCheck.cs" />
Expand All @@ -159,7 +160,6 @@
<Compile Include="Results\BacktestingResultHandler.cs" />
<Compile Include="DataFeeds\FileSystemDataFeed.cs" />
<Compile Include="Results\ConsoleResultHandler.cs" />
<Compile Include="Results\IResultHandler.cs" />
<Compile Include="TransactionHandlers\BacktestingTransactionHandler.cs" />
<Compile Include="TransactionHandlers\BrokerageTransactionHandler.cs" />
<Compile Include="TransactionHandlers\ITransactionHandler.cs" />
Expand Down
48 changes: 27 additions & 21 deletions Engine/Results/BacktestingResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class BacktestingResultHandler : IResultHandler
private DateTime _nextUpdate = new DateTime();
private DateTime _nextS3Update = new DateTime();
DateTime _lastUpdate = new DateTime();
DateTime _timeRequested = new DateTime();
private string _debugMessage = "";
private List<string> _log = new List<string>();
private string _errorMessage = "";
Expand Down Expand Up @@ -72,6 +71,9 @@ public class BacktestingResultHandler : IResultHandler
private DateTime _startTime;
private DateTime _nextSample;

private const double _samples = 4000;
private const double _minimumSamplePeriod = 4;

/********************************************************
* CLASS PROPERTIES
*********************************************************/
Expand Down Expand Up @@ -163,34 +165,20 @@ public TimeSpan NotificationPeriod
* CONSTRUCTOR
*********************************************************/
/// <summary>
/// Backtesting result handler constructor.
/// Default initializer for
/// </summary>
/// <remarks>Setup the default sampling and notification periods based on the backtest length.</remarks>
public BacktestingResultHandler(BacktestNodePacket job)
public BacktestingResultHandler()
{
_job = job;
_exitTriggered = false;
_compileId = job.CompileId;
_backtestId = job.BacktestId;
_timeRequested = DateTime.Now;

//Get the resample period:
double samples = 4000;
double minimumSamplePeriod = 4;
double totalMinutes = (job.PeriodFinish - job.PeriodStart).TotalMinutes;
var resampleMinutes = (totalMinutes < (minimumSamplePeriod * samples)) ? minimumSamplePeriod : (totalMinutes / samples); // Space out the sampling every
_resamplePeriod = TimeSpan.FromMinutes(resampleMinutes);
Log.Trace("BacktestingResultHandler(): Sample Period Set: " + resampleMinutes.ToString("00.00"));

//Notification Period for Browser Pushes:
_notificationPeriod = TimeSpan.FromSeconds(2);

//Initialize Properties:
_messages = new ConcurrentQueue<Packet>();
_charts = new ConcurrentDictionary<string, Chart>();
_chartLock = new Object();
_isActive = true;

//Notification Period for Browser Pushes:
_notificationPeriod = TimeSpan.FromSeconds(2);
_exitTriggered = false;

//Set the start time for the algorithm
_startTime = DateTime.Now;

Expand All @@ -203,6 +191,24 @@ public BacktestingResultHandler(BacktestNodePacket job)
/********************************************************
* CLASS METHODS
*********************************************************/
/// <summary>
/// Initialize the result handler with this result packet.
/// </summary>
/// <param name="job">Algorithm job packet for this result handler</param>
public void Initialize(AlgorithmNodePacket job)
{
_job = (BacktestNodePacket)job;
if (_job == null) throw new Exception("BacktestingResultHandler.Constructor(): Submitted Job type invalid.");
_compileId = _job.CompileId;
_backtestId = _job.BacktestId;

//Get the resample period:
var totalMinutes = (_job.PeriodFinish - _job.PeriodStart).TotalMinutes;
var resampleMinutes = (totalMinutes < (_minimumSamplePeriod * _samples)) ? _minimumSamplePeriod : (totalMinutes / _samples); // Space out the sampling every
_resamplePeriod = TimeSpan.FromMinutes(resampleMinutes);
Log.Trace("BacktestingResultHandler(): Sample Period Set: " + resampleMinutes.ToString("00.00"));
}

/// <summary>
/// The main processing method steps through the messaging queue and processes the messages one by one.
/// </summary>
Expand Down
38 changes: 20 additions & 18 deletions Engine/Results/ConsoleResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public class ConsoleResultHandler : IResultHandler
private DateTime _lastSampledTimed;
private IAlgorithm _algorithm;
private readonly object _chartLock;
private readonly IConsoleStatusHandler _algorithmNode;
private IConsoleStatusHandler _algorithmNode;

//Sampling Periods:
private DateTime _nextSample;
private readonly TimeSpan _resamplePeriod;
private readonly TimeSpan _notificationPeriod;
private TimeSpan _resamplePeriod;
private TimeSpan _notificationPeriod;

public Dictionary<string, string> FinalStatistics { get; private set; }

Expand Down Expand Up @@ -125,21 +125,30 @@ public TimeSpan NotificationPeriod
/// <summary>
/// Console result handler constructor.
/// </summary>
/// <remarks>Setup the default sampling and notification periods based on the backtest length.</remarks>
public ConsoleResultHandler(AlgorithmNodePacket packet)
public ConsoleResultHandler()
{
FinalStatistics = new Dictionary<string, string>();
Log.Trace("Launching Console Result Handler: QuantConnect v2.0");
Messages = new ConcurrentQueue<Packet>();
Charts = new ConcurrentDictionary<string, Chart>();
FinalStatistics = new Dictionary<string, string>();
_chartLock = new Object();
_isActive = true;
_notificationPeriod = TimeSpan.FromSeconds(5);
}

/********************************************************
* PUBLIC METHODS
*********************************************************/
/// <summary>
/// Initialize the result handler with this result packet.
/// </summary>
/// <param name="packet">Algorithm job packet for this result handler</param>
public void Initialize(AlgorithmNodePacket packet)
{
// we expect one of two types here, the backtest node packet or the live node packet
if (packet is BacktestNodePacket)
var job = packet as BacktestNodePacket;
if (job != null)
{
var backtest = packet as BacktestNodePacket;
_algorithmNode = new BacktestConsoleStatusHandler(backtest);
_algorithmNode = new BacktestConsoleStatusHandler(job);
}
else
{
Expand All @@ -150,16 +159,9 @@ public ConsoleResultHandler(AlgorithmNodePacket packet)
}
_algorithmNode = new LiveConsoleStatusHandler(live);
}

_resamplePeriod = _algorithmNode.ComputeSampleEquityPeriod();

//Notification Period for pushes:
_notificationPeriod = TimeSpan.FromSeconds(5);
}

/********************************************************
* PUBLIC METHODS
*********************************************************/

/// <summary>
/// Entry point for console result handler thread.
/// </summary>
Expand Down
Loading

0 comments on commit ca4a49e

Please sign in to comment.