morningstar apparently snaps backward
All checks were successful
Generic zig build / build (push) Successful in 34s

This commit is contained in:
Emil Lerch 2026-03-24 05:23:35 -07:00
parent 070f2352f4
commit 5052492ffd
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -30,9 +30,9 @@ pub const PerformanceResult = struct {
/// Compute total return from adjusted close prices.
/// Candles must be sorted by date ascending.
/// `from` snaps forward (first trading day on/after), `to` snaps backward.
/// `from` snaps backward (last trading day on/before), `to` snaps backward.
pub fn totalReturnFromAdjClose(candles: []const Candle, from: Date, to: Date) ?PerformanceResult {
return totalReturnFromAdjCloseSnap(candles, from, to, .forward);
return totalReturnFromAdjCloseSnap(candles, from, to, .backward);
}
/// Same as totalReturnFromAdjClose but both dates snap backward
@ -253,13 +253,14 @@ fn findNearestCandle(candles: []const Candle, target: Date, direction: SearchDir
const candidate = switch (direction) {
// First candle on or after target
.forward => if (lo < candles.len) candles[lo] else return null,
// Last candle on or before target
// Last candle on or before target; if target is before all data,
// fall back to first candle (snap distance check will reject if too far)
.backward => if (lo < candles.len and candles[lo].date.eql(target))
candles[lo]
else if (lo > 0)
candles[lo - 1]
else
return null,
candles[0],
};
// Reject if the snap distance exceeds tolerance