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