Skip to content

Commit c6c4c1e

Browse files
Fix fundamental security direct access timestamp (#9255)
- Fix the date used by fundamental data accessed directly through security. Updating regression algorithm asserting behavior
1 parent c02a8fa commit c6c4c1e

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

Algorithm.CSharp/FundamentalRegressionAlgorithm.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,17 @@ public override void Initialize()
8080
throw new RegressionTestException($"Unexpected {nameof(Fundamental)} data count {history[0].Values.Count}, expected 2!");
8181
}
8282

83+
// assert all fundamental API data match
8384
foreach (var ticker in new[] {"AAPL", "SPY"})
8485
{
85-
if (!history[0].TryGetValue(ticker, out var fundamental) || fundamental.Price == 0)
86+
var fundamentalThroughSecurity = Securities[ticker].Fundamentals;
87+
var fundamentalThroughAlgo = Fundamentals(ticker);
88+
89+
if (!history[1].TryGetValue(ticker, out var fundamental) || fundamental.Price == 0
90+
|| fundamentalThroughSecurity.Price != fundamental.Price
91+
|| fundamentalThroughSecurity.EndTime != fundamental.EndTime
92+
|| fundamentalThroughAlgo.Price != fundamental.Price
93+
|| fundamentalThroughAlgo.EndTime != fundamental.EndTime)
8694
{
8795
throw new RegressionTestException($"Unexpected {ticker} fundamental data");
8896
}
@@ -142,7 +150,24 @@ public IEnumerable<Symbol> FundamentalSelectionFunction(IEnumerable<Fundamental>
142150
var sortedByPeRatio = sortedByDollarVolume.OrderByDescending(x => x.ValuationRatios.PERatio);
143151

144152
// take the top entries from our sorted collection
145-
var topFine = sortedByPeRatio.Take(NumberOfSymbolsFundamental);
153+
var topFine = sortedByPeRatio.Take(NumberOfSymbolsFundamental).ToArray();
154+
155+
// selection fundamental data should match all other APIs
156+
foreach (var fundamentalPoint in topFine)
157+
{
158+
var symbol = fundamentalPoint.Symbol;
159+
var fundamentalThroughSecurity = Securities.ContainsKey(symbol) ? Securities[symbol].Fundamentals : null;
160+
var fundamentalThroughAlgo = Fundamentals(symbol);
161+
162+
if (fundamentalPoint.Price == 0
163+
|| fundamentalThroughSecurity != null && (fundamentalThroughSecurity.Price != fundamentalPoint.Price
164+
|| fundamentalThroughSecurity.EndTime != fundamentalPoint.EndTime)
165+
|| fundamentalThroughAlgo.Price != fundamentalPoint.Price
166+
|| fundamentalThroughAlgo.EndTime != fundamentalPoint.EndTime)
167+
{
168+
throw new RegressionTestException($"Unexpected {symbol} fundamental data in selection");
169+
}
170+
}
146171

147172
// we need to return only the symbol objects
148173
return topFine.Select(x => x.Symbol);

Algorithm/QCAlgorithm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3379,7 +3379,7 @@ public Symbol[] CIK(int cik, DateTime? tradingDate = null)
33793379
[DocumentationAttribute(SecuritiesAndPortfolio)]
33803380
public Fundamental Fundamentals(Symbol symbol)
33813381
{
3382-
return new Fundamental(Time, symbol) { EndTime = Time };
3382+
return Fundamental.ForDate(Time, symbol);
33833383
}
33843384

33853385
/// <summary>

Common/Data/Fundamental/Fundamental.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ public Fundamental(DateTime time, Symbol symbol)
7171
{
7272
}
7373

74+
/// <summary>
75+
/// Creates a new instance
76+
/// </summary>
77+
/// <param name="time">The current time</param>
78+
/// <param name="symbol">The associated symbol</param>
79+
public static Fundamental ForDate(DateTime time, Symbol symbol)
80+
{
81+
// Important: set EndTime to time so that time is previous day midnight, if we just set time, EndTime would be NEXT day midnight.
82+
// Note: data for T date is available on T+1 date, fundamental selection also handles this, see BaseDataCollectionSubscriptionEnumeratorFactory
83+
return new Fundamental(time, symbol) { EndTime = time };
84+
}
85+
7486
/// <summary>
7587
/// Return the URL string source of the file. This will be converted to a stream
7688
/// </summary>

Common/Securities/Security.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public Fundamental Fundamentals
597597
{
598598
get
599599
{
600-
return new Fundamental(LocalTime, Symbol);
600+
return Fundamental.ForDate(LocalTime, Symbol);
601601
}
602602
}
603603

0 commit comments

Comments
 (0)