Skip to content

Commit

Permalink
Options algorithm performance enhancement (QuantConnect#7555)
Browse files Browse the repository at this point in the history
* Ignore non-market universe data for security cache storage

This is a performance enhancement

* Minor refactor
  • Loading branch information
jhonabreul authored Nov 2, 2023
1 parent 2f75c68 commit a9d2608
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
11 changes: 10 additions & 1 deletion Common/Data/Auxiliary/ZipEntryName.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 Down Expand Up @@ -31,6 +31,15 @@ public ZipEntryName()
DataType = MarketDataType.Auxiliary;
}

/// <summary>
/// Indicates whether this contains data that should be stored in the security cache
/// </summary>
/// <returns>Whether this contains data that should be stored in the security cache</returns>
public override bool ShouldCacheToSecurity()
{
return false;
}

/// <summary>
/// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
/// each time it is called. The returned object is assumed to be time stamped in the config.ExchangeTimeZone.
Expand Down
9 changes: 9 additions & 0 deletions Common/Data/BaseData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ public virtual bool IsSparseData()
return Symbol.SecurityType == SecurityType.Base;
}

/// <summary>
/// Indicates whether this contains data that should be stored in the security cache
/// </summary>
/// <returns>Whether this contains data that should be stored in the security cache</returns>
public virtual bool ShouldCacheToSecurity()
{
return true;
}

/// <summary>
/// Gets the default resolution for this data and security type
/// </summary>
Expand Down
15 changes: 12 additions & 3 deletions Common/Data/UniverseSelection/BaseDataCollection.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* 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");
*
* 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.
Expand Down Expand Up @@ -112,6 +112,15 @@ public BaseDataCollection(DateTime time, DateTime endTime, Symbol symbol, List<B
}
}

/// <summary>
/// Indicates whether this contains data that should be stored in the security cache
/// </summary>
/// <returns>Whether this contains data that should be stored in the security cache</returns>
public override bool ShouldCacheToSecurity()
{
return Data.Count > 0 && Data[0].ShouldCacheToSecurity();
}

/// <summary>
/// Adds a new data point to this collection
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions Engine/AlgorithmManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,11 @@ public void Run(AlgorithmNodePacket job, IAlgorithm algorithm, ISynchronizer syn
{
foreach (var dataCollection in timeSlice.UniverseData.Values)
{
if (!dataCollection.ShouldCacheToSecurity()) continue;

foreach (var data in dataCollection.Data)
{
Security security;
if (algorithm.Securities.TryGetValue(data.Symbol, out security))
if (algorithm.Securities.TryGetValue(data.Symbol, out var security))
{
security.Cache.StoreData(new[] { data }, data.GetType());
}
Expand Down

0 comments on commit a9d2608

Please sign in to comment.