Lean  $LEAN_TAG$
SymbolProperties.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 
19 {
20  /// <summary>
21  /// Represents common properties for a specific security, uniquely identified by market, symbol and security type
22  /// </summary>
23  public class SymbolProperties
24  {
25  /// <summary>
26  /// DTO used to hold the properties of the symbol
27  /// </summary>
28  /// <remarks>
29  /// A DTO is used to handle updates to the symbol properties. Since some properties are decimals,
30  /// which is not thread-safe, we get around it by creating a new instance of the DTO and assigning to this property
31  /// </remarks>
32  private SymbolPropertiesHolder _properties;
33 
34  /// <summary>
35  /// The description of the security
36  /// </summary>
37  public string Description => _properties.Description;
38 
39  /// <summary>
40  /// The quote currency of the security
41  /// </summary>
42  public string QuoteCurrency => _properties.QuoteCurrency;
43 
44  /// <summary>
45  /// The contract multiplier for the security
46  /// </summary>
47  public virtual decimal ContractMultiplier
48  {
49  get => _properties.ContractMultiplier;
50  internal set => _properties.ContractMultiplier = value;
51  }
52 
53  /// <summary>
54  /// The minimum price variation (tick size) for the security
55  /// </summary>
56  public virtual decimal MinimumPriceVariation => _properties.MinimumPriceVariation;
57 
58  /// <summary>
59  /// The lot size (lot size of the order) for the security
60  /// </summary>
61  public decimal LotSize => _properties.LotSize;
62 
63  /// <summary>
64  /// The market ticker
65  /// </summary>
66  public string MarketTicker => _properties.MarketTicker;
67 
68  /// <summary>
69  /// The minimum order size allowed
70  /// For crypto/forex pairs it's expected to be expressed in base or quote currency
71  /// i.e For BTC/USD the minimum order size allowed with Coinbase is 0.0001 BTC
72  /// while on Binance the minimum order size allowed is 10 USD
73  /// </summary>
74  public decimal? MinimumOrderSize => _properties.MinimumOrderSize;
75 
76  /// <summary>
77  /// Allows normalizing live asset prices to US Dollars for Lean consumption. In some exchanges,
78  /// for some securities, data is expressed in cents like for example for corn futures ('ZC').
79  /// </summary>
80  /// <remarks>Default value is 1 but for some futures in cents it's 100</remarks>
81  public decimal PriceMagnifier => _properties.PriceMagnifier;
82 
83  /// <summary>
84  /// Scale factor for option's strike price. For some options, such as NQX, the strike price
85  /// is based on a fraction of the underlying, thus this paramater scales the strike price so
86  /// that it can be used in comparation with the underlying such as
87  /// in <see cref="OptionFilterUniverse.Strikes(int, int)"/>
88  /// </summary>
89  public decimal StrikeMultiplier => _properties.StrikeMultiplier;
90 
91  /// <summary>
92  /// Creates an instance of the <see cref="SymbolProperties"/> class
93  /// </summary>
94  protected SymbolProperties(SymbolProperties properties)
95  {
96  _properties = properties._properties;
97  }
98 
99  /// <summary>
100  /// Creates an instance of the <see cref="SymbolProperties"/> class
101  /// </summary>
102  public SymbolProperties(string description, string quoteCurrency, decimal contractMultiplier,
103  decimal minimumPriceVariation, decimal lotSize, string marketTicker,
104  decimal? minimumOrderSize = null, decimal priceMagnifier = 1, decimal strikeMultiplier = 1)
105  {
106  _properties = new SymbolPropertiesHolder(description, quoteCurrency, contractMultiplier,
107  minimumPriceVariation, lotSize, marketTicker, minimumOrderSize, priceMagnifier, strikeMultiplier);
108  }
109 
110  /// <summary>
111  /// The string representation of these symbol properties
112  /// </summary>
113  public override string ToString()
114  {
115  return Messages.SymbolProperties.ToString(this);
116  }
117 
118  /// <summary>
119  /// Gets a default instance of the <see cref="SymbolProperties"/> class for the specified <paramref name="quoteCurrency"/>
120  /// </summary>
121  /// <param name="quoteCurrency">The quote currency of the symbol</param>
122  /// <returns>A default instance of the<see cref="SymbolProperties"/> class</returns>
123  public static SymbolProperties GetDefault(string quoteCurrency)
124  {
125  return new SymbolProperties(string.Empty, quoteCurrency.LazyToUpper(), 1, 0.01m, 1, string.Empty);
126  }
127 
128  /// <summary>
129  /// Updates the symbol properties with the values from the specified <paramref name="other"/>
130  /// </summary>
131  /// <param name="other">The symbol properties to take values from</param>
132  internal virtual void Update(SymbolProperties other)
133  {
134  _properties = other._properties;
135  }
136 
137  /// <summary>
138  /// DTO used to hold the properties of the symbol
139  /// </summary>
140  private class SymbolPropertiesHolder
141  {
142  public string Description { get; }
143 
144  public string QuoteCurrency { get; }
145 
146  public decimal ContractMultiplier { get; set; }
147 
148  public decimal MinimumPriceVariation { get; }
149 
150  public decimal LotSize { get; }
151 
152  public string MarketTicker { get; }
153 
154  public decimal? MinimumOrderSize { get; }
155 
156  public decimal PriceMagnifier { get; }
157 
158  public decimal StrikeMultiplier { get; }
159 
160 
161  /// <summary>
162  /// Creates an instance of the <see cref="SymbolPropertiesHolder"/> class
163  /// </summary>
164  public SymbolPropertiesHolder(string description, string quoteCurrency, decimal contractMultiplier, decimal minimumPriceVariation, decimal lotSize, string marketTicker, decimal? minimumOrderSize, decimal priceMagnifier, decimal strikeMultiplier)
165  {
166  Description = description;
167  QuoteCurrency = quoteCurrency;
168  ContractMultiplier = contractMultiplier;
169  MinimumPriceVariation = minimumPriceVariation;
170  LotSize = lotSize;
171 
172  if (LotSize <= 0)
173  {
174  throw new ArgumentException(Messages.SymbolProperties.InvalidLotSize);
175  }
176 
177  MarketTicker = marketTicker;
178  MinimumOrderSize = minimumOrderSize;
179 
180  PriceMagnifier = priceMagnifier;
181  if (PriceMagnifier <= 0)
182  {
183  throw new ArgumentException(Messages.SymbolProperties.InvalidPriceMagnifier);
184  }
185 
186  StrikeMultiplier = strikeMultiplier;
187  if (strikeMultiplier <= 0)
188  {
189  throw new ArgumentException(Messages.SymbolProperties.InvalidStrikeMultiplier);
190  }
191  }
192  }
193  }
194 }