Lean  $LEAN_TAG$
Messaging.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 
17 using QuantConnect.Logging;
19 using QuantConnect.Packets;
20 using QuantConnect.Util;
21 using System;
22 using System.Linq;
23 
24 namespace QuantConnect.Messaging
25 {
26  /// <summary>
27  /// Local/desktop implementation of messaging system for Lean Engine.
28  /// </summary>
30  {
31  /// <summary>
32  /// This implementation ignores the <seealso cref="HasSubscribers"/> flag and
33  /// instead will always write to the log.
34  /// </summary>
35  public bool HasSubscribers
36  {
37  get;
38  set;
39  }
40 
41  /// <summary>
42  /// Initialize the messaging system
43  /// </summary>
44  /// <param name="initializeParameters">The parameters required for initialization</param>
45  public void Initialize(MessagingHandlerInitializeParameters initializeParameters)
46  {
47  //
48  }
49 
50  /// <summary>
51  /// Set the messaging channel
52  /// </summary>
53  public virtual void SetAuthentication(AlgorithmNodePacket job)
54  {
55  }
56 
57  /// <summary>
58  /// Send a generic base packet without processing
59  /// </summary>
60  public virtual void Send(Packet packet)
61  {
62  switch (packet.Type)
63  {
64  case PacketType.Debug:
65  var debug = (DebugPacket)packet;
66  Log.Trace("Debug: " + debug.Message);
67  break;
68 
69  case PacketType.SystemDebug:
70  var systemDebug = (SystemDebugPacket)packet;
71  Log.Trace("Debug: " + systemDebug.Message);
72  break;
73 
74  case PacketType.Log:
75  var log = (LogPacket)packet;
76  Log.Trace("Log: " + log.Message);
77  break;
78 
79  case PacketType.RuntimeError:
80  var runtime = (RuntimeErrorPacket)packet;
81  var rstack = (!string.IsNullOrEmpty(runtime.StackTrace) ? (Environment.NewLine + " " + runtime.StackTrace) : string.Empty);
82  Log.Error(runtime.Message + rstack);
83  break;
84 
85  case PacketType.HandledError:
86  var handled = (HandledErrorPacket)packet;
87  var hstack = (!string.IsNullOrEmpty(handled.StackTrace) ? (Environment.NewLine + " " + handled.StackTrace) : string.Empty);
88  Log.Error(handled.Message + hstack);
89  break;
90 
91  case PacketType.AlphaResult:
92  break;
93 
94  case PacketType.BacktestResult:
95  var result = (BacktestResultPacket)packet;
96 
97  if (result.Progress == 1)
98  {
99  var orderHash = result.Results.Orders.GetHash();
100  result.Results.Statistics.Add("OrderListHash", orderHash);
101 
102  var statisticsStr = $"{Environment.NewLine}" +
103  $"{string.Join(Environment.NewLine, result.Results.Statistics.Select(x => $"STATISTICS:: {x.Key} {x.Value}"))}";
104  Log.Trace(statisticsStr);
105  }
106  break;
107  }
108  }
109 
110  /// <summary>
111  /// Send any notification with a base type of Notification.
112  /// </summary>
113  public void SendNotification(Notification notification)
114  {
115  if (!notification.CanSend())
116  {
117  Log.Error("Messaging.SendNotification(): Send not implemented for notification of type: " + notification.GetType().Name);
118  return;
119  }
120  notification.Send();
121  }
122 
123  /// <summary>
124  /// Dispose of any resources
125  /// </summary>
126  public void Dispose()
127  {
128  }
129  }
130 }