Lean  $LEAN_TAG$
LocalLeanManager.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 QuantConnect.Util;
17 using QuantConnect.Packets;
22 
24 {
25  /// <summary>
26  /// NOP implementation of the ILeanManager interface
27  /// </summary>
29  {
30  /// <summary>
31  /// The current algorithm
32  /// </summary>
33  protected IAlgorithm Algorithm { get; set; }
34 
35  private AlgorithmNodePacket _job;
36  private ICommandHandler _commandHandler;
37 
38  /// <summary>
39  /// The system handlers
40  /// </summary>
41  protected LeanEngineSystemHandlers SystemHandlers { get; set; }
42 
43  /// <summary>
44  /// The algorithm handlers
45  /// </summary>
47 
48  /// <summary>
49  /// Empty implementation of the ILeanManager interface
50  /// </summary>
51  /// <param name="systemHandlers">Exposes lean engine system handlers running LEAN</param>
52  /// <param name="algorithmHandlers">Exposes the lean algorithm handlers running lean</param>
53  /// <param name="job">The job packet representing either a live or backtest Lean instance</param>
54  /// <param name="algorithmManager">The Algorithm manager</param>
55  public virtual void Initialize(LeanEngineSystemHandlers systemHandlers, LeanEngineAlgorithmHandlers algorithmHandlers, AlgorithmNodePacket job, AlgorithmManager algorithmManager)
56  {
57  AlgorithmHandlers = algorithmHandlers;
58  SystemHandlers = systemHandlers;
59  _job = job;
60  }
61 
62  /// <summary>
63  /// Sets the IAlgorithm instance in the ILeanManager
64  /// </summary>
65  /// <param name="algorithm">The IAlgorithm instance being run</param>
66  public virtual void SetAlgorithm(IAlgorithm algorithm)
67  {
68  Algorithm = algorithm;
69  algorithm.SetApi(SystemHandlers.Api);
71  }
72 
73  /// <summary>
74  /// Execute the commands using the IAlgorithm instance
75  /// </summary>
76  public virtual void Update()
77  {
78  if(_commandHandler != null)
79  {
80  foreach (var commandResultPacket in _commandHandler.ProcessCommands())
81  {
82  AlgorithmHandlers.Results.Messages.Enqueue(commandResultPacket);
83  }
84  }
85  }
86 
87  /// <summary>
88  /// This method is called after algorithm initialization
89  /// </summary>
90  public virtual void OnAlgorithmStart()
91  {
92  if (Algorithm.LiveMode)
93  {
95  }
96  }
97 
98  /// <summary>
99  /// This method is called before algorithm termination
100  /// </summary>
101  public virtual void OnAlgorithmEnd()
102  {
103  // NOP
104  }
105 
106  /// <summary>
107  /// Callback fired each time that we add/remove securities from the data feed
108  /// </summary>
109  public virtual void OnSecuritiesChanged(SecurityChanges changes)
110  {
111  // NOP
112  }
113 
114  /// <summary>
115  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
116  /// </summary>
117  public virtual void Dispose()
118  {
119  _commandHandler.DisposeSafely();
120  }
121 
122  /// <summary>
123  /// Set the command handler to use, protected for testing purposes
124  /// </summary>
125  protected virtual void SetCommandHandler()
126  {
127  _commandHandler = new FileCommandHandler();
128  _commandHandler.Initialize(_job, Algorithm);
129  }
130  }
131 }