Lean  $LEAN_TAG$
Future.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 QuantConnect.Data;
21 using Python.Runtime;
22 using QuantConnect.Util;
23 
25 {
26  /// <summary>
27  /// Futures Security Object Implementation for Futures Assets
28  /// </summary>
29  /// <seealso cref="Security"/>
31  {
32  private bool _isTradable;
33 
34  /// <summary>
35  /// Gets or sets whether or not this security should be considered tradable
36  /// </summary>
37  /// <remarks>Canonical futures are not tradable</remarks>
38  public override bool IsTradable
39  {
40  get
41  {
42  // once a future is removed it is no longer tradable
43  return _isTradable && !Symbol.IsCanonical();
44  }
45  set
46  {
47  _isTradable = value;
48  }
49  }
50 
51  /// <summary>
52  /// The default number of days required to settle a futures sale
53  /// </summary>
54  public const int DefaultSettlementDays = 1;
55 
56  /// <summary>
57  /// The default time of day for settlement
58  /// </summary>
59  public static readonly TimeSpan DefaultSettlementTime = new TimeSpan(8, 0, 0);
60 
61  /// <summary>
62  /// Constructor for the Future security
63  /// </summary>
64  /// <param name="exchangeHours">Defines the hours this exchange is open</param>
65  /// <param name="quoteCurrency">The cash object that represent the quote currency</param>
66  /// <param name="config">The subscription configuration for this security</param>
67  /// <param name="symbolProperties">The symbol properties for this security</param>
68  /// <param name="currencyConverter">Currency converter used to convert <see cref="CashAmount"/>
69  /// instances into units of the account currency</param>
70  /// <param name="registeredTypes">Provides all data types registered in the algorithm</param>
71  public Future(SecurityExchangeHours exchangeHours,
73  Cash quoteCurrency,
74  SymbolProperties symbolProperties,
75  ICurrencyConverter currencyConverter,
77  )
78  : base(config,
79  quoteCurrency,
80  symbolProperties,
81  new FutureExchange(exchangeHours),
82  new FutureCache(),
84  new FutureFillModel(),
86  NullSlippageModel.Instance,
88  Securities.VolatilityModel.Null,
89  null,
90  new SecurityDataFilter(),
92  currencyConverter,
93  registeredTypes,
94  Securities.MarginInterestRateModel.Null
95  )
96  {
97  BuyingPowerModel = new FutureMarginModel(0, this);
98  // for now all futures are cash settled as we don't allow underlying (Live Cattle?) to be posted on the account
100  Holdings = new FutureHolding(this, currencyConverter);
102  }
103 
104  /// <summary>
105  /// Constructor for the Future security
106  /// </summary>
107  /// <param name="symbol">The subscription security symbol</param>
108  /// <param name="exchangeHours">Defines the hours this exchange is open</param>
109  /// <param name="quoteCurrency">The cash object that represent the quote currency</param>
110  /// <param name="symbolProperties">The symbol properties for this security</param>
111  /// <param name="currencyConverter">Currency converter used to convert <see cref="CashAmount"/>
112  /// instances into units of the account currency</param>
113  /// <param name="registeredTypes">Provides all data types registered in the algorithm</param>
114  /// <param name="securityCache">Cache to store security information</param>
115  /// <param name="underlying">Future underlying security</param>
116  public Future(Symbol symbol,
117  SecurityExchangeHours exchangeHours,
118  Cash quoteCurrency,
119  SymbolProperties symbolProperties,
120  ICurrencyConverter currencyConverter,
121  IRegisteredSecurityDataTypesProvider registeredTypes,
122  SecurityCache securityCache,
123  Security underlying = null
124  )
125  : base(symbol,
126  quoteCurrency,
127  symbolProperties,
128  new FutureExchange(exchangeHours),
129  securityCache,
131  new FutureFillModel(),
133  NullSlippageModel.Instance,
134  new FutureSettlementModel(),
135  Securities.VolatilityModel.Null,
136  null,
137  new SecurityDataFilter(),
139  currencyConverter,
140  registeredTypes,
141  Securities.MarginInterestRateModel.Null
142  )
143  {
144  BuyingPowerModel = new FutureMarginModel(0, this);
145  // for now all futures are cash settled as we don't allow underlying (Live Cattle?) to be posted on the account
147  Holdings = new FutureHolding(this, currencyConverter);
149  Underlying = underlying;
150  }
151 
152  /// <summary>
153  /// Returns true if this is the future chain security, false if it is a specific future contract
154  /// </summary>
155  public bool IsFutureChain => Symbol.IsCanonical();
156 
157  /// <summary>
158  /// Returns true if this is a specific future contract security, false if it is the future chain security
159  /// </summary>
161 
162  /// <summary>
163  /// Gets the expiration date
164  /// </summary>
165  public DateTime Expiry
166  {
167  get { return Symbol.ID.Date; }
168  }
169 
170  /// <summary>
171  /// Specifies if futures contract has physical or cash settlement on settlement
172  /// </summary>
174  {
175  get; set;
176  }
177 
178  /// <summary>
179  /// Gets or sets the underlying security object.
180  /// </summary>
181  public Security Underlying
182  {
183  get; set;
184  }
185 
186  /// <summary>
187  /// Gets or sets the currently mapped symbol for the security
188  /// </summary>
189  public Symbol Mapped
190  {
191  get; set;
192  }
193 
194  /// <summary>
195  /// Gets or sets the contract filter
196  /// </summary>
198  {
199  get; set;
200  }
201 
202  /// <summary>
203  /// Sets the <see cref="LocalTimeKeeper"/> to be used for this <see cref="Security"/>.
204  /// This is the source of this instance's time.
205  /// </summary>
206  /// <param name="localTimeKeeper">The source of this <see cref="Security"/>'s time.</param>
207  public override void SetLocalTimeKeeper(LocalTimeKeeper localTimeKeeper)
208  {
209  base.SetLocalTimeKeeper(localTimeKeeper);
210 
211  var model = SettlementModel as FutureSettlementModel;
212  if (model != null)
213  {
215  }
216  }
217 
218  /// <summary>
219  /// Sets the <see cref="ContractFilter"/> to a new instance of the filter
220  /// using the specified expiration range values
221  /// </summary>
222  /// <param name="minExpiry">The minimum time until expiry to include, for example, TimeSpan.FromDays(10)
223  /// would exclude contracts expiring in less than 10 days</param>
224  /// <param name="maxExpiry">The maximum time until expiry to include, for example, TimeSpan.FromDays(10)
225  /// would exclude contracts expiring in more than 10 days</param>
226  public void SetFilter(TimeSpan minExpiry, TimeSpan maxExpiry)
227  {
228  SetFilterImp(universe => universe.Expiration(minExpiry, maxExpiry));
229  }
230 
231  /// <summary>
232  /// Sets the <see cref="ContractFilter"/> to a new instance of the filter
233  /// using the specified expiration range values
234  /// </summary>
235  /// <param name="minExpiryDays">The minimum time, expressed in days, until expiry to include, for example, 10
236  /// would exclude contracts expiring in less than 10 days</param>
237  /// <param name="maxExpiryDays">The maximum time, expressed in days, until expiry to include, for example, 10
238  /// would exclude contracts expiring in more than 10 days</param>
239  public void SetFilter(int minExpiryDays, int maxExpiryDays)
240  {
241  SetFilterImp(universe => universe.Expiration(minExpiryDays, maxExpiryDays));
242  }
243 
244  /// <summary>
245  /// Sets the <see cref="ContractFilter"/> to a new universe selection function
246  /// </summary>
247  /// <param name="universeFunc">new universe selection function</param>
248  public void SetFilter(Func<FutureFilterUniverse, FutureFilterUniverse> universeFunc)
249  {
250  SetFilterImp(universeFunc);
252  }
253 
254  /// <summary>
255  /// Sets the <see cref="ContractFilter"/> to a new universe selection function
256  /// </summary>
257  /// <param name="universeFunc">new universe selection function</param>
258  public void SetFilter(PyObject universeFunc)
259  {
260  var pyUniverseFunc = PythonUtil.ToFunc<FutureFilterUniverse, FutureFilterUniverse>(universeFunc);
261  SetFilter(pyUniverseFunc);
262  }
263 
264  private void SetFilterImp(Func<FutureFilterUniverse, FutureFilterUniverse> universeFunc)
265  {
266  Func<IDerivativeSecurityFilterUniverse, IDerivativeSecurityFilterUniverse> func = universe =>
267  {
268  var futureUniverse = universe as FutureFilterUniverse;
269  var result = universeFunc(futureUniverse);
270  return result.ApplyTypesFilter();
271  };
273  }
274  }
275 }