Lean  $LEAN_TAG$
BaseSignalExport.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.Util;
18 using System;
19 using System.Collections.Generic;
20 using System.Net.Http;
21 
23 {
24  /// <summary>
25  /// Base class to send signals to different 3rd party API's
26  /// </summary>
27  public abstract class BaseSignalExport : ISignalExportTarget
28  {
29  /// <summary>
30  /// Lazy initialization of HttpClient to be used to sent signals to different 3rd party API's
31  /// </summary>
32  private Lazy<HttpClient> _lazyClient = new Lazy<HttpClient>();
33 
34  /// <summary>
35  /// List of all SecurityTypes present in LEAN
36  /// </summary>
37  private HashSet<SecurityType> _defaultAllowedSecurityTypes = new HashSet<SecurityType>
38  {
39  SecurityType.Equity,
40  SecurityType.Forex,
41  SecurityType.Option,
42  SecurityType.Future,
43  SecurityType.FutureOption,
44  SecurityType.Crypto,
45  SecurityType.CryptoFuture,
46  SecurityType.Cfd,
47  SecurityType.IndexOption,
48  };
49 
50  /// <summary>
51  /// The name of this signal export
52  /// </summary>
53  protected abstract string Name { get; }
54 
55  /// <summary>
56  /// Property to access a HttpClient
57  /// </summary>
58 
59  protected HttpClient HttpClient => _lazyClient.Value;
60 
61  /// <summary>
62  /// Default hashset of allowed Security types
63  /// </summary>
64  protected virtual HashSet<SecurityType> AllowedSecurityTypes
65  {
66  get => _defaultAllowedSecurityTypes;
67  }
68 
69  /// <summary>
70  /// Sends positions to different 3rd party API's
71  /// </summary>
72  /// <param name="parameters">Holdings the user have defined to be sent to certain 3rd party API and the algorithm being ran</param>
73  /// <returns>True if the positions were sent correctly and the 3rd party API sent no errors. False, otherwise</returns>
74  public virtual bool Send(SignalExportTargetParameters parameters)
75  {
76  if (parameters.Targets.Count == 0)
77  {
78  parameters.Algorithm.Debug("Portfolio target is empty");
79  return false;
80  }
81 
82  return VerifyTargets(parameters);
83  }
84 
85  /// <summary>
86  /// Verifies the security type of every holding in the given list is allowed
87  /// </summary>
88  /// <param name="parameters">Holdings the user have defined to be sent to certain 3rd party API and the algorithm being ran</param>
89  /// <returns>True if all the targets were allowed, false otherwise</returns>
90  private bool VerifyTargets(SignalExportTargetParameters parameters)
91  {
92  foreach (var signal in parameters.Targets)
93  {
94  if (!AllowedSecurityTypes.Contains(signal.Symbol.SecurityType))
95  {
96  parameters.Algorithm.Debug($"{signal.Symbol.SecurityType} security type is not supported by {Name}. Allowed security types: [{string.Join(",", AllowedSecurityTypes)}]");
97  return false;
98  }
99  }
100 
101  return true;
102  }
103 
104  /// <summary>
105  /// If created, dispose of HttpClient we used for the requests to the different 3rd party API's
106  /// </summary>
107  public void Dispose()
108  {
109  if (_lazyClient.IsValueCreated)
110  {
111  _lazyClient.Value.DisposeSafely();
112  }
113  }
114  }
115 }