Lean  $LEAN_TAG$
PythonSlice.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 Python.Runtime;
17 using QuantConnect.Data;
20 using System.Collections.Generic;
21 
22 namespace QuantConnect.Python
23 {
24  /// <summary>
25  /// Provides a data structure for all of an algorithm's data at a single time step
26  /// </summary>
27  public class PythonSlice : Slice
28  {
29  private readonly Slice _slice;
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="PythonSlice"/> class
33  /// </summary>
34  /// <param name="slice">slice object to wrap</param>
35  public PythonSlice(Slice slice)
36  : base(slice)
37  {
38  _slice = slice;
39  }
40 
41  /// <summary>
42  /// Gets the data of the specified symbol and type.
43  /// </summary>
44  /// <param name="type">The type of data we seek</param>
45  /// <param name="symbol">The specific symbol was seek</param>
46  /// <returns>The data for the requested symbol</returns>
47  public dynamic Get(PyObject type, Symbol symbol)
48  {
49  return GetImpl(type.CreateType(), _slice)[symbol];
50  }
51 
52  /// <summary>
53  /// Gets the data of the specified symbol and type.
54  /// </summary>
55  /// <param name="type">The type of data we seek</param>
56  /// <returns>The data for the requested symbol</returns>
57  public PyObject Get(PyObject type)
58  {
59  var result = GetImpl(type.CreateType(), _slice) as object;
60  using (Py.GIL())
61  {
62  return result.ToPython();
63  }
64  }
65 
66  /// <summary>
67  /// Gets the number of symbols held in this slice
68  /// </summary>
69  public override int Count
70  {
71  get { return _slice.Count; }
72  }
73 
74  /// <summary>
75  /// Gets all the symbols in this slice
76  /// </summary>
77  public override IReadOnlyList<Symbol> Keys
78  {
79  get { return _slice.Keys; }
80  }
81 
82  /// <summary>
83  /// Gets a list of all the data in this slice
84  /// </summary>
85  public override IReadOnlyList<BaseData> Values
86  {
87  get { return _slice.Values; }
88  }
89 
90  /// <summary>
91  /// Gets the data corresponding to the specified symbol. If the requested data
92  /// is of <see cref="MarketDataType.Tick"/>, then a <see cref="List{Tick}"/> will
93  /// be returned, otherwise, it will be the subscribed type, for example, <see cref="TradeBar"/>
94  /// or event <see cref="UnlinkedData"/> for custom data.
95  /// </summary>
96  /// <param name="symbol">The data's symbols</param>
97  /// <returns>The data for the specified symbol</returns>
98  public override dynamic this[Symbol symbol]
99  {
100  get
101  {
102  return _slice[symbol];
103  }
104  }
105 
106  /// <summary>
107  /// Determines whether this instance contains data for the specified symbol
108  /// </summary>
109  /// <param name="symbol">The symbol we seek data for</param>
110  /// <returns>True if this instance contains data for the symbol, false otherwise</returns>
111  public override bool ContainsKey(Symbol symbol)
112  {
113  return _slice.ContainsKey(symbol);
114  }
115 
116  /// <summary>
117  /// Gets the data associated with the specified symbol
118  /// </summary>
119  /// <param name="symbol">The symbol we want data for</param>
120  /// <param name="data">The data for the specifed symbol, or null if no data was found</param>
121  /// <returns>True if data was found, false otherwise</returns>
122  public override bool TryGetValue(Symbol symbol, out dynamic data)
123  {
124  return _slice.TryGetValue(symbol, out data);
125  }
126  }
127 }