Refine jq microcap execution alignment

This commit is contained in:
boris
2026-04-21 05:45:27 -07:00
parent 0fe681ff5f
commit b0c8ea1a51
3 changed files with 125 additions and 135 deletions

View File

@@ -311,6 +311,7 @@ struct SymbolPriceSeries {
last_prices: Vec<f64>,
open_prefix: Vec<f64>,
close_prefix: Vec<f64>,
prev_close_prefix: Vec<f64>,
last_prefix: Vec<f64>,
}
@@ -326,6 +327,7 @@ impl SymbolPriceSeries {
let last_prices = sorted.iter().map(|row| row.last_price).collect::<Vec<_>>();
let open_prefix = prefix_sums(&opens);
let close_prefix = prefix_sums(&closes);
let prev_close_prefix = prefix_sums(&prev_closes);
let last_prefix = prefix_sums(&last_prices);
Self {
@@ -336,6 +338,7 @@ impl SymbolPriceSeries {
last_prices,
open_prefix,
close_prefix,
prev_close_prefix,
last_prefix,
}
}
@@ -363,29 +366,16 @@ impl SymbolPriceSeries {
}
fn decision_price_on_or_before(&self, date: NaiveDate) -> Option<f64> {
let end = self.end_index(date)?;
let end = self.decision_end_index(date)?;
if end == 0 {
return None;
}
let last_idx = end - 1;
if self.dates.get(last_idx).copied() == Some(date) {
let prev_close = self.prev_closes.get(last_idx).copied().unwrap_or_default();
if prev_close.is_finite() && prev_close > 0.0 {
return Some(prev_close);
}
}
self.closes.get(last_idx).copied()
self.prev_closes.get(end - 1).copied()
}
fn decision_end_index(&self, date: NaiveDate) -> Option<usize> {
match self.dates.binary_search(&date) {
Ok(idx) => {
if idx == 0 {
None
} else {
Some(idx)
}
}
Ok(idx) => Some(idx + 1),
Err(0) => None,
Err(idx) => Some(idx),
}
@@ -400,7 +390,7 @@ impl SymbolPriceSeries {
return None;
}
let start = end - lookback;
let sum = self.close_prefix[end] - self.close_prefix[start];
let sum = self.prev_close_prefix[end] - self.prev_close_prefix[start];
Some(sum / lookback as f64)
}