Skip to content

Commit

Permalink
Set correct exchange time to OptionUniverse instances
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Nov 18, 2024
1 parent c0e3c40 commit 728bddf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
16 changes: 8 additions & 8 deletions Common/Python/PandasConverter.DataFrameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public partial class PandasConverter
private class DataFrameGenerator
{
private static readonly string[] MultiBaseDataCollectionDataFrameNames = new[] { "collection_symbol", "time" };
private static readonly string[] MultiCanonicalOptionDataFrameNames = new[] { "canonical", "time" };
private static readonly string[] MultiCanonicalSymbolsDataFrameNames = new[] { "canonical", "time" };
private static readonly string[] SingleBaseDataCollectionDataFrameNames = new[] { "time" };

private readonly Type _dataType;
Expand Down Expand Up @@ -82,7 +82,7 @@ protected void AddData(IEnumerable<Slice> slices)
{
foreach (var data in slice.AllData)
{
if (_flatten && IsBaseDataCollection(data.GetType()))
if (_flatten && IsCollection(data.GetType()))
{
AddCollection(data.Symbol, data.EndTime, (data as IEnumerable).Cast<ISymbolProvider>());
continue;
Expand Down Expand Up @@ -152,9 +152,9 @@ protected void AddData<T>(IEnumerable<T> data)
where T : ISymbolProvider
{
var type = typeof(T);
var isBaseDataCollection = IsBaseDataCollection(type);
var isCollection = IsCollection(type);

if (_flatten && isBaseDataCollection)
if (_flatten && isCollection)
{
foreach (var collection in data)
{
Expand Down Expand Up @@ -220,8 +220,8 @@ public PyObject GenerateDataFrame(int? levels = null, bool sort = true, bool fil
var keys = collectionsDataFrames
.Select(x => new object[] { x.Item1, x.Item2 })
.Concat(pandasDataDataFrames.Select(x => new object[] { x, DateTime.MinValue }));
var names = _collections.Any(x => x.Symbol.SecurityType.IsOption() && x.Symbol.IsCanonical())
? MultiCanonicalOptionDataFrameNames
var names = _collections.Any(x => x.Symbol.IsCanonical())
? MultiCanonicalSymbolsDataFrameNames
: MultiBaseDataCollectionDataFrameNames;

return ConcatDataFrames(dataFrames, keys, names, sort, dropna: true);
Expand Down Expand Up @@ -306,13 +306,13 @@ private void AddCollection(Symbol symbol, DateTime time, IEnumerable<ISymbolProv
}

/// <summary>
/// Determines whether the type is considered a base data collection for flattening.
/// Determines whether the type is considered a collection for flattening.
/// Any object that is a <see cref="BaseData"/> and implements <see cref="IEnumerable{ISymbolProvider}"/>
/// is considered a base data collection.
/// This allows detecting collections of cases like <see cref="OptionUniverse"/> (which is a direct subclass of
/// <see cref="BaseDataCollection"/>) and <see cref="OptionChain"/>, which is a collection of <see cref="OptionContract"/>
/// </summary>
private static bool IsBaseDataCollection(Type type)
private static bool IsCollection(Type type)
{
return type.IsAssignableTo(typeof(BaseData)) &&
type.GetInterfaces().Any(x => x.IsGenericType &&
Expand Down
2 changes: 1 addition & 1 deletion Engine/DataFeeds/BacktestingChainProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private IEnumerable<Symbol> GetOptionSymbols(Symbol canonicalSymbol, DateTime da
return Enumerable.Empty<Symbol>();
}

return history.TakeLast(1).GetUniverseData().SelectMany(x => x.Values.Single().Where(x => x.Symbol.SecurityType.IsOption())).Select(x => x.Symbol);
return history.GetUniverseData().SelectMany(x => x.Values.Single().Where(x => x.Symbol.SecurityType.IsOption())).Select(x => x.Symbol);
}

/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions Engine/DataFeeds/SubscriptionDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,6 @@ public bool MoveNext()

// Source change, check if we should emit the current instance
if (currentSource != _source
// Don't skip for base data collections (like universe files): those daily files are not read in the
// same way price daily files are read. e.g universe files for date X are loaded in a collection with end time of X+1,
// we don't what to skip them
&& !_config.Type.IsAssignableTo(typeof(BaseDataCollection))
&& (
// After a mapping for every resolution except daily:
// For other resolutions, the instance that triggered the exchange date change should be skipped,
Expand Down

0 comments on commit 728bddf

Please sign in to comment.