Lean  $LEAN_TAG$
BaseCommandHandler.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Linq;
18 using QuantConnect.Logging;
19 using QuantConnect.Packets;
21 using System.Collections.Generic;
22 
23 namespace QuantConnect.Commands
24 {
25  /// <summary>
26  /// Base algorithm command handler
27  /// </summary>
28  public abstract class BaseCommandHandler : ICommandHandler
29  {
30  /// <summary>
31  /// The algorithm instance
32  /// </summary>
33  protected IAlgorithm Algorithm { get; set; }
34 
35  /// <summary>
36  /// Initializes this command queue for the specified job
37  /// </summary>
38  /// <param name="job">The job that defines what queue to bind to</param>
39  /// <param name="algorithm">The algorithm instance</param>
40  public virtual void Initialize(AlgorithmNodePacket job, IAlgorithm algorithm)
41  {
42  Algorithm = algorithm;
43  }
44 
45  /// <summary>
46  /// Get the commands to run
47  /// </summary>
48  protected abstract IEnumerable<ICommand> GetCommands();
49 
50  /// <summary>
51  /// Acknowledge a command that has been executed
52  /// </summary>
53  /// <param name="command">The command that was executed</param>
54  /// <param name="commandResultPacket">The result</param>
55  protected virtual void Acknowledge(ICommand command, CommandResultPacket commandResultPacket)
56  {
57  // nop
58  }
59 
60  /// <summary>
61  /// Will consumer and execute any command in the queue
62  /// </summary>
63  public IEnumerable<CommandResultPacket> ProcessCommands()
64  {
65  List<CommandResultPacket> resultPackets = null;
66  try
67  {
68  foreach (var command in GetCommands().Where(c => c != null))
69  {
70  Log.Trace($"BaseCommandHandler.ProcessCommands(): {Messages.BaseCommandHandler.ExecutingCommand(command)}");
71  CommandResultPacket result;
72  try
73  {
74  result = command.Run(Algorithm);
75  }
76  catch (Exception err)
77  {
78  Log.Error(err);
79  Algorithm.Error($"{command.GetType().Name} Error: {err.Message}");
80  result = new CommandResultPacket(command, false);
81  }
82 
83  Acknowledge(command, result);
84 
85  if(resultPackets == null)
86  {
87  resultPackets = new List<CommandResultPacket>();
88  }
89  resultPackets.Add(result);
90  }
91  }
92  catch (Exception err)
93  {
94  Log.Error(err);
95  }
96 
97  return resultPackets ?? Enumerable.Empty<CommandResultPacket>();
98  }
99 
100  /// <summary>
101  /// Disposes of this instance
102  /// </summary>
103  public virtual void Dispose()
104  {
105  // nop
106  }
107  }
108 }