Lean  $LEAN_TAG$
SecurityDefinition.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.IO;
19 using System.Globalization;
20 using System.Collections.Generic;
21 
23 
25 {
26  /// <summary>
27  /// Helper class containing various unique identifiers for a given
28  /// <see cref="SecurityIdentifier"/>, such as FIGI, ISIN, CUSIP, SEDOL.
29  /// </summary>
30  public class SecurityDefinition
31  {
32  /// <summary>
33  /// The unique <see cref="SecurityIdentifier"/> identified by
34  /// the industry-standard security identifiers contained within this class.
35  /// </summary>
37 
38  /// <summary>
39  /// The Committee on Uniform Securities Identification Procedures (CUSIP) number of a security
40  /// </summary>
41  /// <remarks>For more information on CUSIP numbers: https://en.wikipedia.org/wiki/CUSIP</remarks>
42  public string CUSIP { get; set; }
43 
44  /// <summary>
45  /// The composite Financial Instrument Global Identifier (FIGI) of a security
46  /// </summary>
47  /// <remarks>
48  /// The composite FIGI differs from an exchange-level FIGI, in that it identifies
49  /// an asset across all exchanges in a single country that the asset trades in.
50  /// For more information about the FIGI standard: https://en.wikipedia.org/wiki/Financial_Instrument_Global_Identifier
51  /// </remarks>
52  public string CompositeFIGI { get; set; }
53 
54  /// <summary>
55  /// The Stock Exchange Daily Official List (SEDOL) security identifier of a security
56  /// </summary>
57  /// <remarks>For more information about SEDOL security identifiers: https://en.wikipedia.org/wiki/SEDOL</remarks>
58  public string SEDOL { get; set; }
59 
60  /// <summary>
61  /// The International Securities Identification Number (ISIN) of a security
62  /// </summary>
63  /// <remarks>For more information about the ISIN standard: https://en.wikipedia.org/wiki/International_Securities_Identification_Number</remarks>
64  public string ISIN { get; set; }
65 
66  /// <summary>
67  /// A Central Index Key or CIK number is a unique number assigned to an individual, company, filing agent or foreign government by the United States
68  /// Securities and Exchange Commission (SEC). The number is used to identify its filings in several online databases, including EDGAR.
69  /// </summary>
70  /// <remarks>For more information about CIK: https://en.wikipedia.org/wiki/Central_Index_Key</remarks>
71  public int? CIK { get; set; }
72 
73  /// <summary>
74  /// Reads data from the specified file and converts it to a list of SecurityDefinition
75  /// </summary>
76  /// <param name="dataProvider">Data provider used to obtain symbol mappings data</param>
77  /// <param name="securitiesDefinitionKey">Location to read the securities definition data from</param>
78  /// <returns>List of security definitions</returns>
79  public static List<SecurityDefinition> Read(IDataProvider dataProvider, string securitiesDefinitionKey)
80  {
81  using var stream = dataProvider.Fetch(securitiesDefinitionKey);
82  using var reader = new StreamReader(stream);
83 
84  var securityDefinitions = new List<SecurityDefinition>();
85 
86  string line;
87  while ((line = reader.ReadLine()) != null)
88  {
89  if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#", StringComparison.InvariantCulture))
90  {
91  continue;
92  }
93 
94  securityDefinitions.Add(FromCsvLine(line));
95  }
96 
97  return securityDefinitions;
98  }
99 
100  /// <summary>
101  /// Attempts to read data from the specified file and convert it into a list of SecurityDefinition
102  /// </summary>
103  /// <param name="dataProvider">Data provider used to obtain symbol mappings data</param>
104  /// <param name="securitiesDatabaseKey">Location of the file to read from</param>
105  /// <param name="securityDefinitions">Security definitions read</param>
106  /// <returns>true if data was read successfully, false otherwise</returns>
107  public static bool TryRead(IDataProvider dataProvider, string securitiesDatabaseKey, out List<SecurityDefinition> securityDefinitions)
108  {
109  try
110  {
111  securityDefinitions = Read(dataProvider, securitiesDatabaseKey);
112  return true;
113  }
114  catch
115  {
116  securityDefinitions = null;
117  return false;
118  }
119  }
120 
121  /// <summary>
122  /// Parses a single line of CSV and converts it into an instance
123  /// </summary>
124  /// <param name="line">Line of CSV</param>
125  /// <returns>SecurityDefinition instance</returns>
126  public static SecurityDefinition FromCsvLine(string line)
127  {
128  var csv = line.Split(',');
129  return new SecurityDefinition
130  {
132  CUSIP = string.IsNullOrWhiteSpace(csv[1]) ? null : csv[1],
133  CompositeFIGI = string.IsNullOrWhiteSpace(csv[2]) ? null : csv[2],
134  SEDOL = string.IsNullOrWhiteSpace(csv[3]) ? null : csv[3],
135  ISIN = string.IsNullOrWhiteSpace(csv[4]) ? null : csv[4],
136  CIK = (csv.Length <= 5 || string.IsNullOrWhiteSpace(csv[5])) ? null : int.Parse(csv[5], CultureInfo.InvariantCulture)
137  };
138  }
139  }
140 }