/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using Python.Runtime; using QuantConnect.Data; using QuantConnect.Data.Custom.IconicTypes; using QuantConnect.Data.Market; using System.Collections.Generic; namespace QuantConnect.Python { /// /// Provides a data structure for all of an algorithm's data at a single time step /// public class PythonSlice : Slice { private readonly Slice _slice; /// /// Initializes a new instance of the class /// /// slice object to wrap public PythonSlice(Slice slice) : base(slice) { _slice = slice; } /// /// Gets the data of the specified symbol and type. /// /// The type of data we seek /// The specific symbol was seek /// The data for the requested symbol public dynamic Get(PyObject type, Symbol symbol) { return GetImpl(type.CreateType(), _slice)[symbol]; } /// /// Gets the data of the specified symbol and type. /// /// The type of data we seek /// The data for the requested symbol public PyObject Get(PyObject type) { var result = GetImpl(type.CreateType(), _slice) as object; using (Py.GIL()) { return result.ToPython(); } } /// /// Gets the number of symbols held in this slice /// public override int Count { get { return _slice.Count; } } /// /// Gets all the symbols in this slice /// public override IReadOnlyList Keys { get { return _slice.Keys; } } /// /// Gets a list of all the data in this slice /// public override IReadOnlyList Values { get { return _slice.Values; } } /// /// Gets the data corresponding to the specified symbol. If the requested data /// is of , then a will /// be returned, otherwise, it will be the subscribed type, for example, /// or event for custom data. /// /// The data's symbols /// The data for the specified symbol public override dynamic this[Symbol symbol] { get { return _slice[symbol]; } } /// /// Determines whether this instance contains data for the specified symbol /// /// The symbol we seek data for /// True if this instance contains data for the symbol, false otherwise public override bool ContainsKey(Symbol symbol) { return _slice.ContainsKey(symbol); } /// /// Gets the data associated with the specified symbol /// /// The symbol we want data for /// The data for the specifed symbol, or null if no data was found /// True if data was found, false otherwise public override bool TryGetValue(Symbol symbol, out dynamic data) { return _slice.TryGetValue(symbol, out data); } } }