forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbol.cs
458 lines (396 loc) · 18.5 KB
/
Symbol.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* 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 Newtonsoft.Json;
namespace QuantConnect
{
/// <summary>
/// Represents a unique security identifier. This is made of two components,
/// the unique SID and the Value. The value is the current ticker symbol while
/// the SID is constant over the life of a security
/// </summary>
[JsonConverter(typeof(SymbolJsonConverter))]
public sealed class Symbol : IEquatable<Symbol>, IComparable
{
/// <summary>
/// Represents an unassigned symbol. This is intended to be used as an
/// uninitialized, default value
/// </summary>
public static readonly Symbol Empty = new Symbol(SecurityIdentifier.Empty, string.Empty);
/// <summary>
/// Provides a convience method for creating a Symbol for most security types.
/// This method currently does not support Option, Commodity, and Future
/// </summary>
/// <param name="ticker">The string ticker symbol</param>
/// <param name="securityType">The security type of the ticker. If securityType == Option, then a canonical symbol is created</param>
/// <param name="market">The market the ticker resides in</param>
/// <param name="alias">An alias to be used for the symbol cache. Required when
/// adding the same security from different markets</param>
/// <returns>A new Symbol object for the specified ticker</returns>
public static Symbol Create(string ticker, SecurityType securityType, string market, string alias = null)
{
SecurityIdentifier sid;
switch (securityType)
{
case SecurityType.Base:
sid = SecurityIdentifier.GenerateBase(ticker, market);
break;
case SecurityType.Equity:
sid = SecurityIdentifier.GenerateEquity(ticker, market);
break;
case SecurityType.Forex:
sid = SecurityIdentifier.GenerateForex(ticker, market);
break;
case SecurityType.Cfd:
sid = SecurityIdentifier.GenerateCfd(ticker, market);
break;
case SecurityType.Option:
return CreateOption(ticker, market, default(OptionStyle), default(OptionRight), 0, SecurityIdentifier.DefaultDate);
case SecurityType.Future:
sid = SecurityIdentifier.GenerateFuture(SecurityIdentifier.DefaultDate, ticker, market);
break;
case SecurityType.Commodity:
default:
throw new NotImplementedException("The security type has not been implemented yet: " + securityType);
}
return new Symbol(sid, alias ?? ticker);
}
/// <summary>
/// Provides a convenience method for creating an option Symbol.
/// </summary>
/// <param name="underlying">The underlying ticker</param>
/// <param name="market">The market the underlying resides in</param>
/// <param name="style">The option style (American, European, ect..)</param>
/// <param name="right">The option right (Put/Call)</param>
/// <param name="strike">The option strike price</param>
/// <param name="expiry">The option expiry date</param>
/// <param name="alias">An alias to be used for the symbol cache. Required when
/// adding the same security from diferent markets</param>
/// <param name="mapSymbol">Specifies if symbol should be mapped using map file provider</param>
/// <returns>A new Symbol object for the specified option contract</returns>
public static Symbol CreateOption(string underlying, string market, OptionStyle style, OptionRight right, decimal strike, DateTime expiry, string alias = null, bool mapSymbol = true)
{
var underlyingSid = SecurityIdentifier.GenerateEquity(underlying, market, mapSymbol);
var underlyingSymbol = new Symbol(underlyingSid, underlying);
return CreateOption(underlyingSymbol, market, style, right, strike, expiry, alias);
}
/// <summary>
/// Provides a convenience method for creating an option Symbol using SecurityIdentifier.
/// </summary>
/// <param name="underlyingSymbol">The underlying security symbol</param>
/// <param name="market">The market the underlying resides in</param>
/// <param name="style">The option style (American, European, ect..)</param>
/// <param name="right">The option right (Put/Call)</param>
/// <param name="strike">The option strike price</param>
/// <param name="expiry">The option expiry date</param>
/// <param name="alias">An alias to be used for the symbol cache. Required when
/// adding the same security from diferent markets</param>
/// <returns>A new Symbol object for the specified option contract</returns>
public static Symbol CreateOption(Symbol underlyingSymbol, string market, OptionStyle style, OptionRight right, decimal strike, DateTime expiry, string alias = null)
{
var sid = SecurityIdentifier.GenerateOption(expiry, underlyingSymbol.ID, market, strike, right, style);
if (expiry == SecurityIdentifier.DefaultDate)
{
alias = alias ?? "?" + underlyingSymbol.Value.ToUpper();
}
else
{
var sym = underlyingSymbol.Value;
if (sym.Length > 5) sym += " ";
alias = alias ?? SymbolRepresentation.GenerateOptionTickerOSI(sym, sid.OptionRight, sid.StrikePrice, sid.Date);
}
return new Symbol(sid, alias, underlyingSymbol);
}
/// <summary>
/// Provides a convenience method for creating a future Symbol.
/// </summary>
/// <param name="ticker">The ticker</param>
/// <param name="market">The market the future resides in</param>
/// <param name="expiry">The future expiry date</param>
/// <param name="alias">An alias to be used for the symbol cache. Required when
/// adding the same security from different markets</param>
/// <returns>A new Symbol object for the specified future contract</returns>
public static Symbol CreateFuture(string ticker, string market, DateTime expiry, string alias = null)
{
var sid = SecurityIdentifier.GenerateFuture(expiry, ticker, market);
if (expiry == SecurityIdentifier.DefaultDate)
{
alias = alias ?? "/" + ticker.ToUpper();
}
else
{
var sym = sid.Symbol;
alias = alias ?? SymbolRepresentation.GenerateFutureTicker(sym, sid.Date);
}
return new Symbol(sid, alias);
}
/// <summary>
/// Method returns true, if symbol is a derivative canonical symbol
/// </summary>
/// <returns>true, if symbol is a derivative canonical symbol</returns>
public bool IsCanonical()
{
return
(ID.SecurityType == SecurityType.Future ||
(ID.SecurityType == SecurityType.Option && HasUnderlying)) &&
ID.Date == SecurityIdentifier.DefaultDate;
}
#region Properties
/// <summary>
/// Gets the current symbol for this ticker
/// </summary>
public string Value { get; private set; }
/// <summary>
/// Gets the security identifier for this symbol
/// </summary>
public SecurityIdentifier ID { get; private set; }
/// <summary>
/// Gets whether or not this <see cref="Symbol"/> is a derivative,
/// that is, it has a valid <see cref="Underlying"/> property
/// </summary>
public bool HasUnderlying
{
get { return !ReferenceEquals(Underlying, null); }
}
/// <summary>
/// Gets the security underlying symbol, if any
/// </summary>
public Symbol Underlying { get; private set; }
/// <summary>
/// Gets the security type of the symbol
/// </summary>
public SecurityType SecurityType
{
get { return ID.SecurityType; }
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Symbol"/> class
/// </summary>
/// <param name="sid">The security identifier for this symbol</param>
/// <param name="value">The current ticker symbol value</param>
public Symbol(SecurityIdentifier sid, string value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
ID = sid;
Value = value.ToUpper();
}
/// <summary>
/// Creates new symbol with updated mapped symbol. Symbol Mapping: When symbols change over time (e.g. CHASE-> JPM) need to update the symbol requested.
/// Method returns newly created symbol
/// </summary>
public Symbol UpdateMappedSymbol(string mappedSymbol)
{
if (ID.SecurityType == SecurityType.Option)
{
var underlyingSymbol = new Symbol(Underlying.ID, mappedSymbol, null);
var alias = Value;
if (ID.Date != SecurityIdentifier.DefaultDate)
{
var sym = mappedSymbol;
alias = SymbolRepresentation.GenerateOptionTickerOSI(sym, ID.OptionRight, ID.StrikePrice, ID.Date);
}
return new Symbol(ID, alias, underlyingSymbol);
}
else
{
return new Symbol(ID, mappedSymbol, Underlying);
}
}
/// <summary>
/// Private constructor initializes a new instance of the <see cref="Symbol"/> class with underlying
/// </summary>
/// <param name="sid">The security identifier for this symbol</param>
/// <param name="value">The current ticker symbol value</param>
/// <param name="underlying">The underlying symbol</param>
internal Symbol(SecurityIdentifier sid, string value, Symbol underlying)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
ID = sid;
Value = value.ToUpper();
Underlying = underlying;
}
#endregion
#region Overrides of Object
/// <summary>
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// true if the specified object is equal to the current object; otherwise, false.
/// </returns>
/// <param name="obj">The object to compare with the current object. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
// compare strings just as you would a symbol object
var sidString = obj as string;
if (sidString != null)
{
SecurityIdentifier sid;
if (SecurityIdentifier.TryParse(sidString, out sid))
{
return ID.Equals(sid);
}
}
// compare a sid just as you would a symbol object
if (obj is SecurityIdentifier)
{
return ID.Equals((SecurityIdentifier) obj);
}
if (obj.GetType() != GetType()) return false;
return Equals((Symbol)obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override int GetHashCode()
{
// only SID is used for comparisons
unchecked { return ID.GetHashCode(); }
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <returns>
/// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj"/> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj"/>. Greater than zero This instance follows <paramref name="obj"/> in the sort order.
/// </returns>
/// <param name="obj">An object to compare with this instance. </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. </exception><filterpriority>2</filterpriority>
public int CompareTo(object obj)
{
var str = obj as string;
if (str != null)
{
return string.Compare(Value, str, StringComparison.OrdinalIgnoreCase);
}
var sym = obj as Symbol;
if (sym != null)
{
return string.Compare(Value, sym.Value, StringComparison.OrdinalIgnoreCase);
}
throw new ArgumentException("Object must be of type Symbol or string.");
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
return SymbolCache.GetTicker(this);
}
#endregion
#region Equality members
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(Symbol other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
// only SID is used for comparisons
return ID.Equals(other.ID);
}
/// <summary>
/// Equals operator
/// </summary>
/// <param name="left">The left operand</param>
/// <param name="right">The right operand</param>
/// <returns>True if both symbols are equal, otherwise false</returns>
public static bool operator ==(Symbol left, Symbol right)
{
if (ReferenceEquals(left, null)) return ReferenceEquals(right, null);
return left.Equals(right);
}
/// <summary>
/// Not equals operator
/// </summary>
/// <param name="left">The left operand</param>
/// <param name="right">The right operand</param>
/// <returns>True if both symbols are not equal, otherwise false</returns>
public static bool operator !=(Symbol left, Symbol right)
{
if (ReferenceEquals(left, null)) return ReferenceEquals(right, null);
return !left.Equals(right);
}
#endregion
#region Implicit operators
/// <summary>
/// Returns the symbol's string ticker
/// </summary>
/// <param name="symbol">The symbol</param>
/// <returns>The string ticker</returns>
[Obsolete("Symbol implicit operator to string is provided for algorithm use only.")]
public static implicit operator string(Symbol symbol)
{
return symbol.ToString();
}
/// <summary>
/// Creates symbol using string as sid
/// </summary>
/// <param name="ticker">The string</param>
/// <returns>The symbol</returns>
[Obsolete("Symbol implicit operator from string is provided for algorithm use only.")]
public static implicit operator Symbol(string ticker)
{
Symbol symbol;
if (SymbolCache.TryGetSymbol(ticker, out symbol))
{
return symbol;
}
SecurityIdentifier sid;
if (SecurityIdentifier.TryParse(ticker, out sid))
{
return new Symbol(sid, sid.Symbol);
}
return Empty;
}
#endregion
#region String methods
// in order to maintain better compile time backwards compatibility,
// we'll redirect a few common string methods to Value, but mark obsolete
#pragma warning disable 1591
[Obsolete("Symbol.Contains is a pass-through for Symbol.Value.Contains")]
public bool Contains(string value) { return Value.Contains(value); }
[Obsolete("Symbol.EndsWith is a pass-through for Symbol.Value.EndsWith")]
public bool EndsWith(string value) { return Value.EndsWith(value); }
[Obsolete("Symbol.StartsWith is a pass-through for Symbol.Value.StartsWith")]
public bool StartsWith(string value) { return Value.StartsWith(value); }
[Obsolete("Symbol.ToLower is a pass-through for Symbol.Value.ToLower")]
public string ToLower() { return Value.ToLower(); }
[Obsolete("Symbol.ToUpper is a pass-through for Symbol.Value.ToUpper")]
public string ToUpper() { return Value.ToUpper(); }
#pragma warning restore 1591
#endregion
}
}