Lean  $LEAN_TAG$
IResultHandler.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 
17 using System;
18 using System.Collections.Concurrent;
19 using System.Collections.Generic;
20 using System.ComponentModel.Composition;
25 using QuantConnect.Orders;
26 using QuantConnect.Packets;
28 
30 {
31  /// <summary>
32  /// Handle the results of the backtest: where should we send the profit, portfolio updates:
33  /// Backtester or the Live trading platform:
34  /// </summary>
35  [InheritedExport(typeof(IResultHandler))]
37  {
38  /// <summary>
39  /// Put messages to process into the queue so they are processed by this thread.
40  /// </summary>
41  ConcurrentQueue<Packet> Messages
42  {
43  get;
44  set;
45  }
46 
47  /// <summary>
48  /// Boolean flag indicating the result hander thread is busy.
49  /// False means it has completely finished and ready to dispose.
50  /// </summary>
51  bool IsActive
52  {
53  get;
54  }
55 
56  /// <summary>
57  /// Event fired each time that we add/remove securities from the data feed
58  /// </summary>
60 
61  /// <summary>
62  /// Initialize the result handler with this result packet.
63  /// </summary>
64  /// <param name="parameters">DTO parameters class to initialize a result handler</param>
66 
67  /// <summary>
68  /// Process debug messages with the preconfigured settings.
69  /// </summary>
70  /// <param name="message">String debug message</param>
71  void DebugMessage(string message);
72 
73  /// <summary>
74  /// Process system debug messages with the preconfigured settings.
75  /// </summary>
76  /// <param name="message">String debug message</param>
77  void SystemDebugMessage(string message);
78 
79  /// <summary>
80  /// Send a list of security types to the browser
81  /// </summary>
82  /// <param name="types">Security types list inside algorithm</param>
83  void SecurityType(List<SecurityType> types);
84 
85  /// <summary>
86  /// Send a logging message to the log list for storage.
87  /// </summary>
88  /// <param name="message">Message we'd in the log.</param>
89  void LogMessage(string message);
90 
91  /// <summary>
92  /// Send an error message back to the browser highlighted in red with a stacktrace.
93  /// </summary>
94  /// <param name="error">Error message we'd like shown in console.</param>
95  /// <param name="stacktrace">Stacktrace information string</param>
96  void ErrorMessage(string error, string stacktrace = "");
97 
98  /// <summary>
99  /// Send a runtime error message back to the browser highlighted with in red
100  /// </summary>
101  /// <param name="message">Error message.</param>
102  /// <param name="stacktrace">Stacktrace information string</param>
103  void RuntimeError(string message, string stacktrace = "");
104 
105  /// <summary>
106  /// Process brokerage message events
107  /// </summary>
108  /// <param name="brokerageMessageEvent">The brokerage message event</param>
109  void BrokerageMessage(BrokerageMessageEvent brokerageMessageEvent);
110 
111  /// <summary>
112  /// Method to update the <see cref="IResultHandler"/> with various performance metrics.
113  /// Called once a day by scheduled event in AlgorithmManager
114  /// </summary>
115  /// <param name="time">Current time</param>
116  void Sample(DateTime time);
117 
118  /// <summary>
119  /// Set the algorithm of the result handler after its been initialized.
120  /// </summary>
121  /// <param name="algorithm">Algorithm object matching IAlgorithm interface</param>
122  /// <param name="startingPortfolioValue">Algorithm starting capital for statistics calculations</param>
123  void SetAlgorithm(IAlgorithm algorithm, decimal startingPortfolioValue);
124 
125  /// <summary>
126  /// Send a algorithm status update to the user of the algorithms running state.
127  /// </summary>
128  /// <param name="status">Status enum of the algorithm.</param>
129  /// <param name="message">Optional string message describing reason for status change.</param>
130  void SendStatusUpdate(AlgorithmStatus status, string message = "");
131 
132  /// <summary>
133  /// Set a dynamic runtime statistic to show in the (live) algorithm header
134  /// </summary>
135  /// <param name="key">Runtime headline statistic name</param>
136  /// <param name="value">Runtime headline statistic value</param>
137  void RuntimeStatistic(string key, string value);
138 
139  /// <summary>
140  /// Send a new order event.
141  /// </summary>
142  /// <param name="newEvent">Update, processing or cancellation of an order, update the IDE in live mode or ignore in backtesting.</param>
143  void OrderEvent(OrderEvent newEvent);
144 
145  /// <summary>
146  /// Terminate the result thread and apply any required exit procedures like sending final results.
147  /// </summary>
148  void Exit();
149 
150  /// <summary>
151  /// Process any synchronous events in here that are primarily triggered from the algorithm loop
152  /// </summary>
153  void ProcessSynchronousEvents(bool forceProcess = false);
154 
155  /// <summary>
156  /// Save the results
157  /// </summary>
158  /// <param name="name">The name of the results</param>
159  /// <param name="result">The results to save</param>
160  void SaveResults(string name, Result result);
161 
162  /// <summary>
163  /// Handles updates to the algorithm's name
164  /// </summary>
165  /// <param name="name">The new name</param>
166  void AlgorithmNameUpdated(string name);
167 
168  /// <summary>
169  /// Handles updates to the algorithm's tags
170  /// </summary>
171  /// <param name="tags">The new tags</param>
172  void AlgorithmTagsUpdated(HashSet<string> tags);
173  }
174 }