Lean  $LEAN_TAG$
Market.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.Linq;
18 using System.Collections.Generic;
19 
20 namespace QuantConnect
21 {
22  /// <summary>
23  /// Markets Collection: Soon to be expanded to a collection of items specifying the market hour, timezones and country codes.
24  /// </summary>
25  public static class Market
26  {
27  // the upper bound (non-inclusive) for market identifiers
28  private const int MaxMarketIdentifier = 1000;
29 
30  private static Dictionary<string, int> Markets = new Dictionary<string, int>();
31  private static Dictionary<int, string> ReverseMarkets = new Dictionary<int, string>();
32  private static readonly IEnumerable<Tuple<string, int>> HardcodedMarkets = new List<Tuple<string, int>>
33  {
34  Tuple.Create("empty", 0),
35  Tuple.Create(USA, 1),
36  Tuple.Create(FXCM, 2),
37  Tuple.Create(Oanda, 3),
38  Tuple.Create(Dukascopy, 4),
39  Tuple.Create(Bitfinex, 5),
40 
41  Tuple.Create(Globex, 6),
42  Tuple.Create(NYMEX, 7),
43  Tuple.Create(CBOT, 8),
44  Tuple.Create(ICE, 9),
45  Tuple.Create(CBOE, 10),
46  Tuple.Create(India, 11),
47 
48  Tuple.Create(GDAX, 12),
49  Tuple.Create(Kraken, 13),
50  Tuple.Create(Bittrex, 14),
51  Tuple.Create(Bithumb, 15),
52  Tuple.Create(Binance, 16),
53  Tuple.Create(Poloniex, 17),
54  Tuple.Create(Coinone, 18),
55  Tuple.Create(HitBTC, 19),
56  Tuple.Create(OkCoin, 20),
57  Tuple.Create(Bitstamp, 21),
58 
59  Tuple.Create(COMEX, 22),
60  Tuple.Create(CME, 23),
61  Tuple.Create(SGX, 24),
62  Tuple.Create(HKFE, 25),
63  Tuple.Create(NYSELIFFE, 26),
64 
65  Tuple.Create(CFE, 33),
66  Tuple.Create(FTX, 34),
67  Tuple.Create(FTXUS, 35),
68  Tuple.Create(BinanceUS, 36),
69  Tuple.Create(Bybit, 37)
70  };
71 
72  static Market()
73  {
74  // initialize our maps
75  foreach (var market in HardcodedMarkets)
76  {
77  Markets[market.Item1] = market.Item2;
78  ReverseMarkets[market.Item2] = market.Item1;
79  }
80  }
81 
82  /// <summary>
83  /// USA Market
84  /// </summary>
85  public const string USA = "usa";
86 
87  /// <summary>
88  /// Oanda Market
89  /// </summary>
90  public const string Oanda = "oanda";
91 
92  /// <summary>
93  /// FXCM Market Hours
94  /// </summary>
95  public const string FXCM = "fxcm";
96 
97  /// <summary>
98  /// Dukascopy Market
99  /// </summary>
100  public const string Dukascopy = "dukascopy";
101 
102  /// <summary>
103  /// Bitfinex market
104  /// </summary>
105  public const string Bitfinex = "bitfinex";
106 
107  // Futures exchanges
108 
109  /// <summary>
110  /// CME Globex
111  /// </summary>
112  public const string Globex = "cmeglobex";
113 
114  /// <summary>
115  /// NYMEX
116  /// </summary>
117  public const string NYMEX = "nymex";
118 
119  /// <summary>
120  /// CBOT
121  /// </summary>
122  public const string CBOT = "cbot";
123 
124  /// <summary>
125  /// ICE
126  /// </summary>
127  public const string ICE = "ice";
128 
129  /// <summary>
130  /// CBOE
131  /// </summary>
132  public const string CBOE = "cboe";
133 
134  /// <summary>
135  /// CFE
136  /// </summary>
137  public const string CFE = "cfe";
138 
139  /// <summary>
140  /// NSE - National Stock Exchange
141  /// </summary>
142  public const string India = "india";
143 
144  /// <summary>
145  /// Comex
146  /// </summary>
147  public const string COMEX = "comex";
148 
149  /// <summary>
150  /// CME
151  /// </summary>
152  public const string CME = "cme";
153 
154  /// <summary>
155  /// Singapore Exchange
156  /// </summary>
157  public const string SGX = "sgx";
158 
159  /// <summary>
160  /// Hong Kong Exchange
161  /// </summary>
162  public const string HKFE = "hkfe";
163 
164  /// <summary>
165  /// London International Financial Futures and Options Exchange
166  /// </summary>
167  public const string NYSELIFFE = "nyseliffe";
168 
169  /// <summary>
170  /// GDAX
171  /// </summary>
172  public const string GDAX = "gdax";
173 
174  /// <summary>
175  /// Kraken
176  /// </summary>
177  public const string Kraken = "kraken";
178 
179  /// <summary>
180  /// Bitstamp
181  /// </summary>
182  public const string Bitstamp = "bitstamp";
183 
184  /// <summary>
185  /// OkCoin
186  /// </summary>
187  public const string OkCoin = "okcoin";
188 
189  /// <summary>
190  /// Bithumb
191  /// </summary>
192  public const string Bithumb = "bithumb";
193 
194  /// <summary>
195  /// Binance
196  /// </summary>
197  public const string Binance = "binance";
198 
199  /// <summary>
200  /// Poloniex
201  /// </summary>
202  public const string Poloniex = "poloniex";
203 
204  /// <summary>
205  /// Coinone
206  /// </summary>
207  public const string Coinone = "coinone";
208 
209  /// <summary>
210  /// HitBTC
211  /// </summary>
212  public const string HitBTC = "hitbtc";
213 
214  /// <summary>
215  /// Bittrex
216  /// </summary>
217  public const string Bittrex = "bittrex";
218 
219  /// <summary>
220  /// FTX
221  /// </summary>
222  public const string FTX = "ftx";
223 
224  /// <summary>
225  /// FTX.US
226  /// </summary>
227  public const string FTXUS = "ftxus";
228 
229  /// <summary>
230  /// Binance.US
231  /// </summary>
232  public const string BinanceUS = "binanceus";
233 
234  /// <summary>
235  /// Bybit
236  /// </summary>
237  public const string Bybit = "bybit";
238 
239  /// <summary>
240  /// Adds the specified market to the map of available markets with the specified identifier.
241  /// </summary>
242  /// <param name="market">The market string to add</param>
243  /// <param name="identifier">The identifier for the market, this value must be positive and less than 1000</param>
244  public static void Add(string market, int identifier)
245  {
246  if (identifier >= MaxMarketIdentifier)
247  {
248  throw new ArgumentOutOfRangeException(nameof(identifier), Messages.Market.InvalidMarketIdentifier(MaxMarketIdentifier));
249  }
250 
251  market = market.ToLowerInvariant();
252 
253  int marketIdentifier;
254  if (Markets.TryGetValue(market, out marketIdentifier) && identifier != marketIdentifier)
255  {
256  throw new ArgumentException(Messages.Market.TriedToAddExistingMarketWithDifferentIdentifier(market));
257  }
258 
259  string existingMarket;
260  if (ReverseMarkets.TryGetValue(identifier, out existingMarket))
261  {
262  throw new ArgumentException(Messages.Market.TriedToAddExistingMarketIdentifier(market, existingMarket));
263  }
264 
265  // update our maps.
266  // We make a copy and update the copy, later swap the references so it's thread safe with no lock
267  var newMarketDictionary = Markets.ToDictionary(entry => entry.Key,
268  entry => entry.Value);
269  newMarketDictionary[market] = identifier;
270 
271  var newReverseMarketDictionary = ReverseMarkets.ToDictionary(entry => entry.Key,
272  entry => entry.Value);
273  newReverseMarketDictionary[identifier] = market;
274 
275  Markets = newMarketDictionary;
276  ReverseMarkets = newReverseMarketDictionary;
277  }
278 
279  /// <summary>
280  /// Gets the market code for the specified market. Returns <c>null</c> if the market is not found
281  /// </summary>
282  /// <param name="market">The market to check for (case sensitive)</param>
283  /// <returns>The internal code used for the market. Corresponds to the value used when calling <see cref="Add"/></returns>
284  public static int? Encode(string market)
285  {
286  return !Markets.TryGetValue(market, out var code) ? null : code;
287  }
288 
289  /// <summary>
290  /// Gets the market string for the specified market code.
291  /// </summary>
292  /// <param name="code">The market code to be decoded</param>
293  /// <returns>The string representation of the market, or null if not found</returns>
294  public static string Decode(int code)
295  {
296  return !ReverseMarkets.TryGetValue(code, out var market) ? null : market;
297  }
298 
299  /// <summary>
300  /// Returns a list of the supported markets
301  /// </summary>
302  public static List<string> SupportedMarkets()
303  {
304  return Markets.Keys.ToList();
305  }
306  }
307 }