Skip to content

Commit

Permalink
Addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JosueNina committed Jan 16, 2025
1 parent 7b2d0e0 commit 56c19f1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Algorithm.Python/HistoryAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def initialize(self):
# get the historical data from last current day to this current day in minute resolution
# with Extended Market option
interval_bar_history = self.history(["SPY"], self.time - timedelta(1), self.time, Resolution.MINUTE, False, True)
self.assert_history_count("History([\"SPY\"], self.time - timedelta(1), self.time, Resolution.MINUTE, False, True)", interval_bar_history, 828)
self.assert_history_count("History([\"SPY\"], self.time - timedelta(1), self.time, Resolution.MINUTE, False, True)", interval_bar_history, 919)

# get the historical data from last current day to this current day in minute resolution
# with Fill Forward option
Expand Down
10 changes: 6 additions & 4 deletions Algorithm/QCAlgorithm.History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,10 +1097,12 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb


// If no existing configuration for the Quote tick type, add the new config
if (!configs.Any(config => config.TickType == TickType.Quote) && type == null)
if (type == null && !configs.Any(config => config.TickType == TickType.Quote))
{
type = LeanData.GetDataType(resolution.Value, TickType.Quote);
var entry = MarketHoursDatabase.GetEntry(symbol, new[] { type });
var baseFillForward = configs[0].FillDataForward;
var baseExtendedMarketHours = configs[0].ExtendedMarketHours;

// Create a new SubscriptionDataConfig
var newConfig = new SubscriptionDataConfig(
Expand All @@ -1109,14 +1111,14 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
resolution.Value,
entry.DataTimeZone,
entry.ExchangeHours.TimeZone,
UniverseSettings.FillForward,
UniverseSettings.ExtendedMarketHours,
baseFillForward,
baseExtendedMarketHours,
false, tickType: TickType.Quote);

configs.Add(newConfig);

// Sort the configs in descending order based on tick type
configs = configs.OrderByDescending(config => GetTickTypeOrder(config.SecurityType, config.TickType)).ToList();
return configs.OrderByDescending(config => GetTickTypeOrder(config.SecurityType, config.TickType));
}
}

Expand Down
31 changes: 14 additions & 17 deletions Tests/Algorithm/AlgorithmHistoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,38 +331,35 @@ public void ExplicitTickResolutionHistoryRequestTradeBarApiThrowsException()
public void VerifyReceivedDataBasedOnHistoryResolutionOnly(Resolution historyResolution, Resolution equityResolution, bool expected)
{
var algorithm = GetAlgorithm(new DateTime(2013, 10, 1));
algorithm.SetStartDate(2013, 10, 10);
algorithm.SetStartDate(2013, 10, 8);
var spy = algorithm.AddEquity("SPY", Resolution.Minute).Symbol;
var ibm = algorithm.AddEquity("IBM", equityResolution).Symbol;

// Retrieving history for both symbols based on the given resolution
var history = algorithm.History(new[] { spy, ibm }, TimeSpan.FromDays(1), historyResolution);
var allHistory = history.SelectMany(slice => slice.AllData).ToList();

// Flags to check if there's Quote data for SPY and IBM
var spyFlag = false;
var ibmFlag = false;

// If the history resolution is Tick, check for Quote-type ticks
if (historyResolution == Resolution.Tick)
{
var start = new DateTime(2013, 10, 7, 15, 0, 0);
var allHistory = algorithm.History(new[] { spy, ibm }, start, start.AddSeconds(5), historyResolution).SelectMany(slice => slice.AllData);
// Filter the data to get only the Quote-type ticks
var ticks = allHistory.OfType<Tick>().Where(e => e.TickType == TickType.Quote).ToList();
spyFlag = ticks.Any(e => e.Symbol == spy);
ibmFlag = ticks.Any(e => e.Symbol == ibm);
} else
{
var allHistory = algorithm.History(new[] { spy, ibm }, TimeSpan.FromDays(1), historyResolution).SelectMany(slice => slice.AllData).ToList();
// Checking for QuoteBar data for SPY and IBM
var quoteBars = allHistory.Where(e => e.DataType == MarketDataType.QuoteBar).ToList();
spyFlag |= quoteBars.Any(e => e.Symbol == spy);
ibmFlag |= quoteBars.Any(e => e.Symbol == ibm);
}

// Checking for QuoteBar data for SPY and IBM
var quoteBars = allHistory.Where(e => e.DataType == MarketDataType.QuoteBar).ToList();
spyFlag |= quoteBars.Any(e => e.Symbol == spy);
ibmFlag |= quoteBars.Any(e => e.Symbol == ibm);
// Verifying that both symbols have Quote data based on the history resolution
bool bothSymbolsHaveQuotes = spyFlag & ibmFlag;

// Ensure history contains data
Assert.IsTrue(allHistory.Count > 0);

// Asserting that the result matches the expected outcome
Assert.AreEqual(expected, bothSymbolsHaveQuotes);
// Assert that the flags match the expected value
Assert.AreEqual(expected, spyFlag);
Assert.AreEqual(expected, ibmFlag);
}

[Test]
Expand Down

0 comments on commit 56c19f1

Please sign in to comment.