Skip to content

Commit

Permalink
Added ILeanManagement.Update() method
Browse files Browse the repository at this point in the history
  • Loading branch information
quietjoy committed Jul 14, 2017
1 parent f7bd4f5 commit 6d67dbd
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 240 deletions.
27 changes: 6 additions & 21 deletions Engine/AlgorithmManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using QuantConnect.Lean.Engine.DataFeeds;
using QuantConnect.Lean.Engine.RealTime;
using QuantConnect.Lean.Engine.Results;
using QuantConnect.Lean.Engine.Server;
using QuantConnect.Lean.Engine.TransactionHandlers;
using QuantConnect.Logging;
using QuantConnect.Orders;
Expand Down Expand Up @@ -128,9 +129,11 @@ public AlgorithmManager(bool liveMode)
/// <param name="results">Result handler object</param>
/// <param name="realtime">Realtime processing object</param>
/// <param name="commands">The command queue for relaying extenal commands to the algorithm</param>
/// <param name="systemHandlersServer"></param>
/// <param name="server">IServer implementation that is updated periodically with the IAlgorithm instance</param>
/// <param name="token">Cancellation token</param>
/// <remarks>Modify with caution</remarks>
public void Run(AlgorithmNodePacket job, IAlgorithm algorithm, IDataFeed feed, ITransactionHandler transactions, IResultHandler results, IRealTimeHandler realtime, ICommandQueueHandler commands, CancellationToken token)
public void Run(AlgorithmNodePacket job, IAlgorithm algorithm, IDataFeed feed, ITransactionHandler transactions, IResultHandler results, IRealTimeHandler realtime, IServer server, CancellationToken token)
{
//Initialize:
_dataPointCount = 0;
Expand Down Expand Up @@ -216,26 +219,8 @@ public void Run(AlgorithmNodePacket job, IAlgorithm algorithm, IDataFeed feed, I
return;
}

// before doing anything, check our command queue
foreach (var command in commands.GetCommands())
{
if (command == null) continue;
Log.Trace("AlgorithmManager.Run(): Executing {0}", command);
CommandResultPacket result;
try
{
result = command.Run(algorithm);
}
catch (Exception err)
{
Log.Error(err);
algorithm.Error(string.Format("{0} Error: {1}", command.GetType().Name, err.Message));
result = new CommandResultPacket(command, false);
}

// send the result of the command off to the result handler
results.Messages.Enqueue(result);
}
// Update the IServer
server.Update();

var time = timeSlice.Time;
_dataPointCount += timeSlice.DataPointCount;
Expand Down
9 changes: 4 additions & 5 deletions Engine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ public void Run(AlgorithmNodePacket job, AlgorithmManager manager, string assemb
// Save algorithm to cache, load algorithm instance:
algorithm = _algorithmHandlers.Setup.CreateAlgorithmInstance(job, assemblyPath);

// Set algorithm in IServer
_systemHandlers.Server.SetAlgorithm(algorithm);

// Initialize the brokerage
IBrokerageFactory factory;
brokerage = _algorithmHandlers.Setup.CreateBrokerage(job, algorithm, out factory);

// Initialize the data feed before we initialize so he can intercept added securities/universes via events
_algorithmHandlers.DataFeed.Initialize(algorithm, job, _algorithmHandlers.Results, _algorithmHandlers.MapFileProvider, _algorithmHandlers.FactorFileProvider, _algorithmHandlers.DataProvider);

// initialize command queue system
_algorithmHandlers.CommandQueue.Initialize(job, algorithm);

// set the order processor on the transaction manager (needs to be done before initializing BrokerageHistoryProvider)
algorithm.Transactions.SetOrderProcessor(_algorithmHandlers.Transactions);

Expand Down Expand Up @@ -180,7 +180,6 @@ public void Run(AlgorithmNodePacket job, AlgorithmManager manager, string assemb
Log.Trace(" RealTime: " + _algorithmHandlers.RealTime.GetType().FullName);
Log.Trace(" Results: " + _algorithmHandlers.Results.GetType().FullName);
Log.Trace(" Transactions: " + _algorithmHandlers.Transactions.GetType().FullName);
Log.Trace(" Commands: " + _algorithmHandlers.CommandQueue.GetType().FullName);
if (algorithm != null && algorithm.HistoryProvider != null)
{
Log.Trace(" History Provider: " + algorithm.HistoryProvider.GetType().FullName);
Expand Down Expand Up @@ -249,7 +248,7 @@ public void Run(AlgorithmNodePacket job, AlgorithmManager manager, string assemb
// -> Using this Data Feed,
// -> Send Orders to this TransactionHandler,
// -> Send Results to ResultHandler.
algorithmManager.Run(job, algorithm, _algorithmHandlers.DataFeed, _algorithmHandlers.Transactions, _algorithmHandlers.Results, _algorithmHandlers.RealTime, _algorithmHandlers.CommandQueue, isolator.CancellationToken);
algorithmManager.Run(job, algorithm, _algorithmHandlers.DataFeed, _algorithmHandlers.Transactions, _algorithmHandlers.Results, _algorithmHandlers.RealTime, _systemHandlers.Server, isolator.CancellationToken);
}
catch (Exception err)
{
Expand Down
18 changes: 0 additions & 18 deletions Engine/LeanEngineAlgorithmHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class LeanEngineAlgorithmHandlers : IDisposable
private readonly IResultHandler _results;
private readonly IRealTimeHandler _realTime;
private readonly ITransactionHandler _transactions;
private readonly ICommandQueueHandler _commandQueue;
private readonly IMapFileProvider _mapFileProvider;
private readonly IFactorFileProvider _factorFileProvider;
private readonly IDataProvider _dataProvider;
Expand Down Expand Up @@ -82,14 +81,6 @@ public IRealTimeHandler RealTime
get { return _realTime; }
}

/// <summary>
/// Gets the command queue responsible for receiving external commands for the algorithm
/// </summary>
public ICommandQueueHandler CommandQueue
{
get { return _commandQueue; }
}

/// <summary>
/// Gets the map file provider used as a map file source for the data feed
/// </summary>
Expand Down Expand Up @@ -131,7 +122,6 @@ public LeanEngineAlgorithmHandlers(IResultHandler results,
IDataFeed dataFeed,
ITransactionHandler transactions,
IRealTimeHandler realTime,
ICommandQueueHandler commandQueue,
IMapFileProvider mapFileProvider,
IFactorFileProvider factorFileProvider,
IDataProvider dataProvider
Expand All @@ -157,10 +147,6 @@ IDataProvider dataProvider
{
throw new ArgumentNullException("realTime");
}
if (commandQueue == null)
{
throw new ArgumentNullException("commandQueue");
}
if (mapFileProvider == null)
{
throw new ArgumentNullException("mapFileProvider");
Expand All @@ -178,7 +164,6 @@ IDataProvider dataProvider
_dataFeed = dataFeed;
_transactions = transactions;
_realTime = realTime;
_commandQueue = commandQueue;
_mapFileProvider = mapFileProvider;
_factorFileProvider = factorFileProvider;
_dataProvider = dataProvider;
Expand All @@ -197,7 +182,6 @@ public static LeanEngineAlgorithmHandlers FromConfiguration(Composer composer)
var realTimeHandlerTypeName = Config.Get("real-time-handler", "BacktestingRealTimeHandler");
var dataFeedHandlerTypeName = Config.Get("data-feed-handler", "FileSystemDataFeed");
var resultHandlerTypeName = Config.Get("result-handler", "BacktestingResultHandler");
var commandQueueHandlerTypeName = Config.Get("command-queue-handler", "EmptyCommandQueueHandler");
var mapFileProviderTypeName = Config.Get("map-file-provider", "LocalDiskMapFileProvider");
var factorFileProviderTypeName = Config.Get("factor-file-provider", "LocalDiskFactorFileProvider");
var dataProviderTypeName = Config.Get("data-provider", "DefaultDataProvider");
Expand All @@ -208,7 +192,6 @@ public static LeanEngineAlgorithmHandlers FromConfiguration(Composer composer)
composer.GetExportedValueByTypeName<IDataFeed>(dataFeedHandlerTypeName),
composer.GetExportedValueByTypeName<ITransactionHandler>(transactionHandlerTypeName),
composer.GetExportedValueByTypeName<IRealTimeHandler>(realTimeHandlerTypeName),
composer.GetExportedValueByTypeName<ICommandQueueHandler>(commandQueueHandlerTypeName),
composer.GetExportedValueByTypeName<IMapFileProvider>(mapFileProviderTypeName),
composer.GetExportedValueByTypeName<IFactorFileProvider>(factorFileProviderTypeName),
composer.GetExportedValueByTypeName<IDataProvider>(dataProviderTypeName)
Expand All @@ -222,7 +205,6 @@ public static LeanEngineAlgorithmHandlers FromConfiguration(Composer composer)
public void Dispose()
{
Setup.Dispose();
CommandQueue.Dispose();
}
}
}
12 changes: 12 additions & 0 deletions Engine/Server/IServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using System.ComponentModel.Composition;
using QuantConnect.Interfaces;
using QuantConnect.Packets;

namespace QuantConnect.Lean.Engine.Server
Expand All @@ -34,5 +35,16 @@ public interface IServer : IDisposable
/// <param name="job">The job packet representing either a live or backtest Lean instance</param>
/// <param name="algorithmManager">The Algorithm manager</param>
void Initialize(LeanEngineSystemHandlers systemHandlers, LeanEngineAlgorithmHandlers algorithmHandlers, AlgorithmNodePacket job, AlgorithmManager algorithmManager);

/// <summary>
/// Sets the IAlgorithm instance in the IServer
/// </summary>
/// <param name="algorithm">The IAlgorithm instance being run</param>
void SetAlgorithm(IAlgorithm algorithm);

/// <summary>
/// Update IServer with the IAlgorithm instance
/// </summary>
void Update();
}
}
22 changes: 21 additions & 1 deletion Engine/Server/LocalServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using QuantConnect.Packets;
using QuantConnect.Interfaces;
using QuantConnect.Packets;

namespace QuantConnect.Lean.Engine.Server
{
Expand All @@ -19,8 +20,27 @@ public void Initialize(LeanEngineSystemHandlers systemHandlers, LeanEngineAlgori
// NOP
}

/// <summary>
/// Sets the IAlgorithm instance in the IServer
/// </summary>
/// <param name="algorithm">The IAlgorithm instance being run</param>
public void SetAlgorithm(IAlgorithm algorithm)
{
// NOP
}

/// <summary>
/// Update IServer with the IAlgorithm instance
/// </summary>
public void Update()
{
// NOP
}

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
// NOP
}
}
}
5 changes: 0 additions & 5 deletions Launcher/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
"messaging-handler": "QuantConnect.Messaging.Messaging",
"job-queue-handler": "QuantConnect.Queues.JobQueue",
"api-handler": "QuantConnect.Api.Api",
"command-queue-handler": "QuantConnect.Queues.EmptyCommandQueueHandler",
// this command queue will check a file to read json serialized commands (use TypeNameHandling.All)
//"command-queue-handler": "QuantConnect.Queues.FileCommandQueueHandler",
// used by the FileComandQueueHandler, serialize a ICommand and it will be run against algorithm
"command-json-file": "command.json",
"map-file-provider": "QuantConnect.Data.Auxiliary.LocalDiskMapFileProvider",
"factor-file-provider": "QuantConnect.Data.Auxiliary.LocalDiskFactorFileProvider",
"data-provider": "QuantConnect.Lean.Engine.DataFeeds.DefaultDataProvider",
Expand Down
56 changes: 0 additions & 56 deletions Queues/EmptyCommandQueueHandler.cs

This file was deleted.

Loading

0 comments on commit 6d67dbd

Please sign in to comment.