Skip to content

Commit

Permalink
Add DataFrame property to FuturesChains class
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Dec 23, 2024
1 parent d9085cb commit 3d9a451
Show file tree
Hide file tree
Showing 69 changed files with 190 additions and 184 deletions.
4 changes: 2 additions & 2 deletions Algorithm/QCAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3439,10 +3439,10 @@ public FuturesChains FuturesChains(IEnumerable<Symbol> symbols, bool flatten = f
.Select(x => (x.Keys.Single(), x.Values.Single().Cast<FutureUniverse>()));

var time = Time.Date;
var chains = new FuturesChains(time);
var chains = new FuturesChains(time, flatten);
foreach (var (symbol, contracts) in futureChainsData)
{
var chain = new FuturesChain(symbol, time, contracts);
var chain = new FuturesChain(symbol, time, contracts, flatten);
chains.Add(symbol, chain);
}

Expand Down
107 changes: 107 additions & 0 deletions Common/Data/Market/BaseChains.cs
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);
}
}
}
32 changes: 10 additions & 22 deletions Common/Data/Market/FuturesChains.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand All @@ -20,7 +20,7 @@ namespace QuantConnect.Data.Market
/// <summary>
/// Collection of <see cref="FuturesChain"/> keyed by canonical futures symbol
/// </summary>
public class FuturesChains : DataDictionary<FuturesChain>
public class FuturesChains : BaseChains<FuturesChain, FuturesContract, FuturesContracts>
{
/// <summary>
/// Creates a new instance of the <see cref="FuturesChains"/> dictionary
Expand All @@ -32,29 +32,17 @@ public FuturesChains()
/// <summary>
/// Creates a new instance of the <see cref="FuturesChains"/> dictionary
/// </summary>
public FuturesChains(DateTime time)
: base(time)
public FuturesChains(bool flatten)
: base(flatten)
{
}

/// <summary>
/// Gets or sets the FuturesChain with the specified ticker.
/// </summary>
/// <returns>
/// The FuturesChain 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 FuturesChain this[string ticker] { get { return base[ticker]; } set { base[ticker] = value; } }

/// <summary>
/// Gets or sets the FuturesChain with the specified Symbol.
/// Creates a new instance of the <see cref="FuturesChains"/> dictionary
/// </summary>
/// <returns>
/// The FuturesChain 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 FuturesChain this[Symbol symbol] { get { return base[symbol]; } set { base[symbol] = value; } }
public FuturesChains(DateTime time, bool flatten = true)
: base(time, flatten)
{
}
}
}
}
63 changes: 4 additions & 59 deletions Common/Data/Market/OptionChains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,91 +13,36 @@
* limitations under the License.
*/

using Python.Runtime;
using QuantConnect.Python;
using System;
using System.Collections.Generic;
using System.Linq;

namespace QuantConnect.Data.Market
{
/// <summary>
/// Collection of <see cref="OptionChain"/> keyed by canonical option symbol
/// </summary>
public class OptionChains : DataDictionary<OptionChain>
public class OptionChains : BaseChains<OptionChain, OptionContract, OptionContracts>
{
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="OptionChains"/> dictionary
/// </summary>
public OptionChains()
: this(default, true)
public OptionChains() : base()
{
}

/// <summary>
/// Creates a new instance of the <see cref="OptionChains"/> dictionary
/// </summary>
public OptionChains(bool flatten)
: this(default, flatten)
: base(flatten)
{
}

/// <summary>
/// Creates a new instance of the <see cref="OptionChains"/> dictionary
/// </summary>
public OptionChains(DateTime time, bool flatten = true)
: base(time)
: base(time, flatten)
{
_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 OptionChain with the specified ticker.
/// </summary>
/// <returns>
/// The OptionChain 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 OptionChain this[string ticker] { get { return base[ticker]; } set { base[ticker] = value; } }

/// <summary>
/// Gets or sets the OptionChain with the specified Symbol.
/// </summary>
/// <returns>
/// The OptionChain 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 OptionChain 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);
}
}
}
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20111229.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.54,17.75,17.41,17.45,310,3824
DC V5E8P9SH0U0X,DC01H12,17.54,17.75,17.41,17.45,310,3824
DC V67S42RJBGU9,DC01J12,17.4,17.43,17.36,17.36,25,2808
DC V70C2SDFJCHT,DC30K12,17.22,17.25,17.19,17.25,35,2650
DC V8PDSL1WA4G1,DC31M12,0,0,0,0,0,
Expand Down
2 changes: 0 additions & 2 deletions Data/future/cme/universes/dc/20111230.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V4JPUDG8NG1T,DC01H12,17.25,17.35,17.13,17.15,231,4726
DC V5E8P9SH0U0X,DC01H12,17.5,17.69,17.45,17.56,449,3926
DC V5E8P9SH0U0X,DC01H12,17.5,17.69,17.45,17.56,449,3926
DC V67S42RJBGU9,DC01J12,17.4,17.44,17.39,17.42,31,2841
DC V70C2SDFJCHT,DC30K12,17.25,17.3,17.25,17.25,11,2687
DC V7YSQ26C7R41,DC04M12,17.16,17.34,17.15,17.28,63,1680
Expand All @@ -14,7 +13,6 @@ DC VCY032R252BL,DC02Z12,17.08,17.08,17.06,17.06,2,1269
DC VDOL5LMM7FNL,DC29F13,16.75,16.78,16.75,16.775,0,36
DC VEG5O7VCCK5D,DC26G13,0,0,0,0,0,25
DC VFEMBHO90YRL,DC02H13,0,0,0,0,0,21
DC VFEMBHO90YRL,DC02H13,0,0,0,0,0,21
DC VH4NHDPVUHVL,DC04K13,0,0,0,0,0,5
DC VHW7ZZYLZMDD,DC02M13,0,0,0,0,0,4
DC VINSIM7C4QV5,DC30N13,0,0,0,0,0,3
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120103.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.59,17.59,17.3,17.45,371,4894
DC V5E8P9SH0U0X,DC01H12,17.59,17.59,17.3,17.45,371,4894
DC V67S42RJBGU9,DC01J12,17.39,17.39,17.28,17.39,63,2884
DC V70C2SDFJCHT,DC30K12,17.24,17.26,17.2,17.24,32,2703
DC V8PDSL1WA4G1,DC31M12,17.2,17.2,17.14,17.2,31,2318
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120104.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.44,17.85,17.4,17.68,1074,5271
DC V5E8P9SH0U0X,DC01H12,17.44,17.85,17.4,17.68,1074,5271
DC V67S42RJBGU9,DC01J12,17.39,17.6,17.39,17.58,107,2934
DC V70C2SDFJCHT,DC30K12,17.28,17.46,17.26,17.4,75,2764
DC V8PDSL1WA4G1,DC31M12,17.22,17.4,17.22,17.35,30,2337
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120105.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.95,17.95,17.57,17.74,531,5271
DC V5E8P9SH0U0X,DC01H12,17.95,17.95,17.57,17.74,531,5271
DC V67S42RJBGU9,DC01J12,17.69,17.78,17.59,17.73,83,2934
DC V70C2SDFJCHT,DC30K12,17.5,17.59,17.44,17.55,50,2764
DC V8PDSL1WA4G1,DC31M12,17.42,17.5,17.39,17.5,24,2337
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120106.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.95,18.04,17.36,17.42,974,4467
DC V5E8P9SH0U0X,DC01H12,17.95,18.04,17.36,17.42,974,4467
DC V67S42RJBGU9,DC01J12,17.8,17.87,17.64,17.66,146,3067
DC V70C2SDFJCHT,DC30K12,17.6,17.62,17.45,17.48,52,2795
DC V8PDSL1WA4G1,DC31M12,17.59,17.6,17.43,17.43,40,2362
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120109.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.65,17.74,17.14,17.3,644,5493
DC V5E8P9SH0U0X,DC01H12,17.65,17.74,17.14,17.3,644,5493
DC V67S42RJBGU9,DC01J12,17.7,17.7,17.6,17.66,42,3084
DC V70C2SDFJCHT,DC30K12,17.51,17.54,17.48,17.5,25,2809
DC V8PDSL1WA4G1,DC31M12,17.52,17.52,17.46,17.46,13,2376
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120110.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.78,17.87,17.35,17.47,327,5604
DC V5E8P9SH0U0X,DC01H12,17.78,17.87,17.35,17.47,327,5604
DC V67S42RJBGU9,DC01J12,17.66,17.76,17.65,17.67,44,3113
DC V70C2SDFJCHT,DC30K12,17.51,17.6,17.5,17.59,34,2835
DC V8PDSL1WA4G1,DC31M12,17.5,17.56,17.46,17.52,23,2391
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120111.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.39,17.83,17.25,17.76,716,5713
DC V5E8P9SH0U0X,DC01H12,17.39,17.83,17.25,17.76,716,5713
DC V67S42RJBGU9,DC01J12,17.65,17.71,17.61,17.61,32,3133
DC V70C2SDFJCHT,DC30K12,17.57,17.62,17.54,17.59,19,2842
DC V8PDSL1WA4G1,DC31M12,17.54,17.6,17.5,17.6,25,2411
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120112.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.71,17.75,17.01,17.08,1429,5713
DC V5E8P9SH0U0X,DC01H12,17.71,17.75,17.01,17.08,1429,5713
DC V67S42RJBGU9,DC01J12,17.61,17.66,17.3,17.31,80,3133
DC V70C2SDFJCHT,DC30K12,17.57,17.61,17.35,17.35,42,2842
DC V8PDSL1WA4G1,DC31M12,17.56,17.58,17.37,17.44,38,2411
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120113.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.06,17.37,16.9,17.25,786,4947
DC V5E8P9SH0U0X,DC01H12,17.06,17.37,16.9,17.25,786,4947
DC V67S42RJBGU9,DC01J12,17.3,17.3,17.2,17.2,84,3180
DC V70C2SDFJCHT,DC30K12,17.29,17.33,17.2,17.2,27,2873
DC V8PDSL1WA4G1,DC31M12,17.35,17.41,17.24,17.24,23,2426
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120117.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,17.21,17.23,16.47,16.62,1426,5531
DC V5E8P9SH0U0X,DC01H12,17.21,17.23,16.47,16.62,1426,5531
DC V67S42RJBGU9,DC01J12,17.12,17.12,16.56,16.61,228,3207
DC V70C2SDFJCHT,DC30K12,17.2,17.2,16.7,16.75,176,2919
DC V8PDSL1WA4G1,DC31M12,17.25,17.25,16.79,16.9,147,2443
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120118.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,16.61,16.82,16.28,16.51,1233,5268
DC V5E8P9SH0U0X,DC01H12,16.61,16.82,16.28,16.51,1233,5268
DC V67S42RJBGU9,DC01J12,16.45,16.74,16.43,16.64,166,3177
DC V70C2SDFJCHT,DC30K12,16.67,16.89,16.56,16.8,71,2926
DC V8PDSL1WA4G1,DC31M12,16.85,17.15,16.76,17.05,83,2467
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120119.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,16.87,17.08,16.31,16.43,685,5268
DC V5E8P9SH0U0X,DC01H12,16.87,17.08,16.31,16.43,685,5268
DC V67S42RJBGU9,DC01J12,16.82,16.9,16.59,16.67,87,3177
DC V70C2SDFJCHT,DC30K12,16.92,17,16.78,16.82,44,2926
DC V8PDSL1WA4G1,DC31M12,17.16,17.19,17.05,17.05,41,2467
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120120.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,16.37,16.67,16.25,16.59,380,5312
DC V5E8P9SH0U0X,DC01H12,16.37,16.67,16.25,16.59,380,5312
DC V67S42RJBGU9,DC01J12,16.61,16.64,16.51,16.6,115,3228
DC V70C2SDFJCHT,DC30K12,16.77,16.77,16.69,16.75,41,2947
DC V8PDSL1WA4G1,DC31M12,17.07,17.07,16.97,17.02,20,2477
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120123.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,16.54,16.95,16.28,16.56,397,5218
DC V5E8P9SH0U0X,DC01H12,16.54,16.95,16.28,16.56,397,5218
DC V67S42RJBGU9,DC01J12,16.6,16.89,16.56,16.8,122,3227
DC V70C2SDFJCHT,DC30K12,16.8,16.92,16.76,16.81,31,2942
DC V8PDSL1WA4G1,DC31M12,17.02,17.17,16.95,17.1,87,2486
Expand Down
1 change: 0 additions & 1 deletion Data/future/cme/universes/dc/20120124.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#symbol_id,symbol_value,open,high,low,close,volume,open_interest
DC V5E8P9SH0U0X,DC01H12,16.83,16.9,16.53,16.62,653,5352
DC V5E8P9SH0U0X,DC01H12,16.83,16.9,16.53,16.62,653,5352
DC V67S42RJBGU9,DC01J12,16.81,16.85,16.74,16.74,56,3233
DC V70C2SDFJCHT,DC30K12,16.9,16.9,16.76,16.8,33,2930
DC V8PDSL1WA4G1,DC31M12,17.14,17.14,17.03,17.03,24,2480
Expand Down
Loading

0 comments on commit 3d9a451

Please sign in to comment.