forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DataFrame property to FuturesChains class
- Loading branch information
1 parent
d9085cb
commit 3d9a451
Showing
69 changed files
with
190 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* 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.Python; | ||
using QuantConnect.Securities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace QuantConnect.Data.Market | ||
{ | ||
/// <summary> | ||
/// Collection of <see cref="BaseChain{T, TContractsCollection}"/> keyed by canonical option symbol | ||
/// </summary> | ||
public class BaseChains<T, TContract, TContractsCollection> : DataDictionary<T> | ||
where T : BaseChain<TContract, TContractsCollection> | ||
where TContract : ISymbol, ISymbolProvider | ||
where TContractsCollection : DataDictionary<TContract>, new() | ||
{ | ||
private static readonly IEnumerable<string> _flattenedDfIndexNames = new[] { "canonical", "symbol" }; | ||
|
||
private readonly Lazy<PyObject> _dataframe; | ||
private readonly bool _flatten; | ||
|
||
/// <summary> | ||
/// Creates a new instance of the <see cref="BaseChains{T, TContract, TContractsCollection}"/> dictionary | ||
/// </summary> | ||
protected BaseChains() | ||
: this(default, true) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of the <see cref="BaseChains{T, TContract, TContractsCollection}"/> dictionary | ||
/// </summary> | ||
protected BaseChains(bool flatten) | ||
: this(default, flatten) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of the <see cref="BaseChains{T, TContract, TContractsCollection}"/> dictionary | ||
/// </summary> | ||
protected BaseChains(DateTime time, bool flatten) | ||
: base(time) | ||
{ | ||
_flatten = flatten; | ||
_dataframe = new Lazy<PyObject>(InitializeDataFrame, isThreadSafe: false); | ||
} | ||
|
||
/// <summary> | ||
/// The data frame representation of the option chains | ||
/// </summary> | ||
public PyObject DataFrame => _dataframe.Value; | ||
|
||
/// <summary> | ||
/// Gets or sets the BaseChain with the specified ticker. | ||
/// </summary> | ||
/// <returns> | ||
/// The BaseChain with the specified ticker. | ||
/// </returns> | ||
/// <param name="ticker">The ticker of the element to get or set.</param> | ||
/// <remarks>Wraps the base implementation to enable indexing in python algorithms due to pythonnet limitations</remarks> | ||
public new T this[string ticker] { get { return base[ticker]; } set { base[ticker] = value; } } | ||
|
||
/// <summary> | ||
/// Gets or sets the BaseChain with the specified Symbol. | ||
/// </summary> | ||
/// <returns> | ||
/// The BaseChain with the specified Symbol. | ||
/// </returns> | ||
/// <param name="symbol">The Symbol of the element to get or set.</param> | ||
/// <remarks>Wraps the base implementation to enable indexing in python algorithms due to pythonnet limitations</remarks> | ||
public new T this[Symbol symbol] { get { return base[symbol]; } set { base[symbol] = value; } } | ||
|
||
private PyObject InitializeDataFrame() | ||
{ | ||
if (!PythonEngine.IsInitialized) | ||
{ | ||
return null; | ||
} | ||
|
||
var dataFrames = this.Select(kvp => kvp.Value.DataFrame).ToList(); | ||
|
||
if (_flatten) | ||
{ | ||
var canonicalSymbols = this.Select(kvp => kvp.Key); | ||
return PandasConverter.ConcatDataFrames(dataFrames, keys: canonicalSymbols, names: _flattenedDfIndexNames, sort: false); | ||
} | ||
|
||
return PandasConverter.ConcatDataFrames(dataFrames, sort: false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.