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