morningstar apparently snaps backward
All checks were successful
Generic zig build / build (push) Successful in 34s
All checks were successful
Generic zig build / build (push) Successful in 34s
This commit is contained in:
parent
070f2352f4
commit
5052492ffd
1 changed files with 5 additions and 4 deletions
|
|
@ -30,9 +30,9 @@ pub const PerformanceResult = struct {
|
||||||
|
|
||||||
/// Compute total return from adjusted close prices.
|
/// Compute total return from adjusted close prices.
|
||||||
/// Candles must be sorted by date ascending.
|
/// 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 {
|
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
|
/// 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) {
|
const candidate = switch (direction) {
|
||||||
// First candle on or after target
|
// First candle on or after target
|
||||||
.forward => if (lo < candles.len) candles[lo] else return null,
|
.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))
|
.backward => if (lo < candles.len and candles[lo].date.eql(target))
|
||||||
candles[lo]
|
candles[lo]
|
||||||
else if (lo > 0)
|
else if (lo > 0)
|
||||||
candles[lo - 1]
|
candles[lo - 1]
|
||||||
else
|
else
|
||||||
return null,
|
candles[0],
|
||||||
};
|
};
|
||||||
|
|
||||||
// Reject if the snap distance exceeds tolerance
|
// Reject if the snap distance exceeds tolerance
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue