Lean  $LEAN_TAG$
BrokerageFactory.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.Collections.Generic;
19 using QuantConnect.Packets;
21 
23 {
24  /// <summary>
25  /// Provides a base implementation of IBrokerageFactory that provides a helper for reading data from a job's brokerage data dictionary
26  /// </summary>
27  public abstract class BrokerageFactory : IBrokerageFactory
28  {
29  private readonly Type _brokerageType;
30 
31  /// <summary>
32  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
33  /// </summary>
34  /// <filterpriority>2</filterpriority>
35  public abstract void Dispose();
36 
37  /// <summary>
38  /// Gets the type of brokerage produced by this factory
39  /// </summary>
40  public Type BrokerageType
41  {
42  get { return _brokerageType; }
43  }
44 
45  /// <summary>
46  /// Gets the brokerage data required to run the brokerage from configuration/disk
47  /// </summary>
48  /// <remarks>
49  /// The implementation of this property will create the brokerage data dictionary required for
50  /// running live jobs. See <see cref="IJobQueueHandler.NextJob"/>
51  /// </remarks>
52  public abstract Dictionary<string, string> BrokerageData { get; }
53 
54  /// <summary>
55  /// Gets a brokerage model that can be used to model this brokerage's unique behaviors
56  /// </summary>
57  /// <param name="orderProvider">The order provider</param>
58  public abstract IBrokerageModel GetBrokerageModel(IOrderProvider orderProvider);
59 
60  /// <summary>
61  /// Creates a new IBrokerage instance
62  /// </summary>
63  /// <param name="job">The job packet to create the brokerage for</param>
64  /// <param name="algorithm">The algorithm instance</param>
65  /// <returns>A new brokerage instance</returns>
66  public abstract IBrokerage CreateBrokerage(LiveNodePacket job, IAlgorithm algorithm);
67 
68  /// <summary>
69  /// Gets a brokerage message handler
70  /// </summary>
72  {
73  return new DefaultBrokerageMessageHandler(algorithm, job, api);
74  }
75 
76  /// <summary>
77  /// Initializes a new instance of the <see cref="BrokerageFactory"/> class for the specified <paramref name="brokerageType"/>
78  /// </summary>
79  /// <param name="brokerageType">The type of brokerage created by this factory</param>
80  protected BrokerageFactory(Type brokerageType)
81  {
82  _brokerageType = brokerageType;
83  }
84 
85  /// <summary>
86  /// Reads a value from the brokerage data, adding an error if the key is not found
87  /// </summary>
88  protected static T Read<T>(IReadOnlyDictionary<string, string> brokerageData, string key, ICollection<string> errors)
89  where T : IConvertible
90  {
91  string value;
92  if (!brokerageData.TryGetValue(key, out value))
93  {
94  errors.Add("BrokerageFactory.CreateBrokerage(): Missing key: " + key);
95  return default(T);
96  }
97 
98  try
99  {
100  return value.ConvertTo<T>();
101  }
102  catch (Exception err)
103  {
104  errors.Add($"BrokerageFactory.CreateBrokerage(): Error converting key '{key}' with value '{value}'. {err.Message}");
105  return default(T);
106  }
107  }
108  }
109 }