Lean  $LEAN_TAG$
FuncSecuritySeeder.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 
17 using System;
18 using Python.Runtime;
19 using QuantConnect.Data;
20 using QuantConnect.Logging;
21 using System.Collections.Generic;
22 
24 {
25  /// <summary>
26  /// Seed a security price from a history function
27  /// </summary>
29  {
30  private readonly Func<Security, IEnumerable<BaseData>> _seedFunction;
31 
32 
33  /// <summary>
34  /// Constructor that takes as a parameter the security used to seed the price
35  /// </summary>
36  /// <param name="seedFunction">The seed function to use</param>
37  public FuncSecuritySeeder(PyObject seedFunction)
38  {
39  var result = seedFunction.ConvertToDelegate<Func<Security, object>>();
40  _seedFunction = security =>
41  {
42  var dataObject = result(security);
43  var dataPoint = dataObject as BaseData;
44  if (dataPoint != null)
45  {
46  return new[] { dataPoint };
47  }
48 
49  return (IEnumerable<BaseData>)dataObject;
50  };
51  }
52 
53  /// <summary>
54  /// Constructor that takes as a parameter the security used to seed the price
55  /// </summary>
56  /// <param name="seedFunction">The seed function to use</param>
57  public FuncSecuritySeeder(Func<Security, BaseData> seedFunction)
58  : this(security => { return new []{ seedFunction(security) }; })
59  {
60  }
61 
62  /// <summary>
63  /// Constructor that takes as a parameter the security used to seed the price
64  /// </summary>
65  /// <param name="seedFunction">The seed function to use</param>
66  public FuncSecuritySeeder(Func<Security, IEnumerable<BaseData>> seedFunction)
67  {
68  _seedFunction = seedFunction;
69  }
70 
71  /// <summary>
72  /// Seed the security
73  /// </summary>
74  /// <param name="security"><see cref="Security"/> being seeded</param>
75  /// <returns>true if the security was seeded, false otherwise</returns>
76  public bool SeedSecurity(Security security)
77  {
78  try
79  {
80  // Do not seed canonical symbols
81  if (!security.Symbol.IsCanonical())
82  {
83  var gotData = false;
84  foreach (var seedData in _seedFunction(security))
85  {
86  gotData = true;
87  security.SetMarketPrice(seedData);
88  Log.Debug("FuncSecuritySeeder.SeedSecurity(): " + Messages.FuncSecuritySeeder.SeededSecurityInfo(seedData));
89  }
90 
91  if (!gotData)
92  {
93  Log.Trace("FuncSecuritySeeder.SeedSecurity(): " + Messages.FuncSecuritySeeder.UnableToSeedSecurity(security));
94  return false;
95  }
96  }
97  }
98  catch (Exception exception)
99  {
100  Log.Trace("FuncSecuritySeeder.SeedSecurity(): " + Messages.FuncSecuritySeeder.UnableToSecurityPrice(security) + $": {exception}");
101  return false;
102  }
103 
104  return true;
105  }
106  }
107 }