Lean  $LEAN_TAG$
FutureSymbolGenerator.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 System.Collections.Generic;
18 
20 {
21  /// <summary>
22  /// Generates a new random future <see cref="Symbol"/>. The generates future contract Symbol will have an
23  /// expiry between the specified time range.
24  /// </summary>
26  {
27  private readonly DateTime _minExpiry;
28  private readonly DateTime _maxExpiry;
29  private readonly string _market;
30 
32  : base(settings, random)
33  {
34  _minExpiry = settings.Start;
35  _maxExpiry = settings.End;
36  _market = settings.Market;
37  }
38 
39  /// <summary>
40  /// Generates a new random future <see cref="Symbol"/>. The generates future contract Symbol will have an
41  /// expiry between the specified minExpiry and maxExpiry.
42  /// </summary>
43  /// <param name="ticker">Optionally can provide a ticker that should be used</param>
44  /// <returns>A new future contract Symbol with the specified expiration parameters</returns>
45  protected override IEnumerable<Symbol> GenerateAsset(string ticker = null)
46  {
47  if (ticker == null)
48  {
49  // get a valid ticker from the Symbol properties database
50  ticker = NextTickerFromSymbolPropertiesDatabase(SecurityType.Future, _market);
51  }
52 
53  var marketHours = MarketHoursDatabase.GetExchangeHours(_market, ticker, SecurityType.Future);
54  var expiry = GetRandomExpiration(marketHours, _minExpiry, _maxExpiry);
55 
56  yield return Symbol.CreateFuture(ticker, _market, expiry);
57  }
58 
59  /// <summary>
60  /// There is no limit for the future symbols.
61  /// </summary>
62  /// <returns>Returns int.MaxValue</returns>
63  public override int GetAvailableSymbolCount() => int.MaxValue;
64  }
65 }