Lean  $LEAN_TAG$
DowngradeErrorCodeToWarningBrokerageMessageHandler.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.Linq;
17 using System.Collections.Generic;
18 
20 {
21  /// <summary>
22  /// Provides an implementation of <see cref="IBrokerageMessageHandler"/> that converts specified error codes into warnings
23  /// </summary>
25  {
26  private readonly HashSet<string> _errorCodesToIgnore;
27  private readonly IBrokerageMessageHandler _brokerageMessageHandler;
28 
29  /// <summary>
30  /// Initializes a new instance of the <see cref="DowngradeErrorCodeToWarningBrokerageMessageHandler"/> class
31  /// </summary>
32  /// <param name="brokerageMessageHandler">The brokerage message handler to be wrapped</param>
33  /// <param name="errorCodesToIgnore">The error codes to convert to warning messages</param>
34  public DowngradeErrorCodeToWarningBrokerageMessageHandler(IBrokerageMessageHandler brokerageMessageHandler, string[] errorCodesToIgnore)
35  {
36  _brokerageMessageHandler = brokerageMessageHandler;
37  _errorCodesToIgnore = errorCodesToIgnore.ToHashSet();
38  }
39 
40  /// <summary>
41  /// Handles the message
42  /// </summary>
43  /// <param name="message">The message to be handled</param>
44  public void HandleMessage(BrokerageMessageEvent message)
45  {
46  if (message.Type == BrokerageMessageType.Error && _errorCodesToIgnore.Contains(message.Code))
47  {
48  // rewrite the ignored message as a warning message
49  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, message.Code, message.Message);
50  }
51 
52  _brokerageMessageHandler.HandleMessage(message);
53  }
54 
55  /// <summary>
56  /// Handles a new order placed manually in the brokerage side
57  /// </summary>
58  /// <param name="eventArgs">The new order event</param>
59  /// <returns>Whether the order should be added to the transaction handler</returns>
61  {
62  return _brokerageMessageHandler.HandleOrder(eventArgs);
63  }
64  }
65 }