MCPcopy Create free account
hub / github.com/Open-Quant/openquant / time_bars

Function time_bars

crates/openquant/src/data_structures.rs:93–120  ·  view source on GitHub ↗

Construct time bars using a fixed interval. The interval applies from the start timestamp of the current bar; the trade that crosses the interval boundary is included in the closing bar, and accumulation restarts afterward.

(trades: &[Trade], interval: Duration)

Source from the content-addressed store, hash-verified

91/// timestamp of the current bar; the trade that crosses the interval boundary is
92/// included in the closing bar, and accumulation restarts afterward.
93pub fn time_bars(trades: &[Trade], interval: Duration) -> Vec<StandardBar> {
94 assert!(interval.num_microseconds().unwrap_or(0) > 0, "interval must be positive");
95
96 let mut bars = Vec::new();
97 if trades.is_empty() {
98 return bars;
99 }
100
101 let mut start_idx = 0;
102 let mut bar_start = trades[0].timestamp;
103
104 for (i, trade) in trades.iter().enumerate() {
105 let elapsed = trade.timestamp - bar_start;
106 if elapsed >= interval {
107 bars.push(build_bar(&trades[start_idx..=i]));
108 start_idx = i + 1;
109 if start_idx < trades.len() {
110 bar_start = trades[start_idx].timestamp;
111 }
112 }
113 }
114
115 if start_idx < trades.len() {
116 bars.push(build_bar(&trades[start_idx..]));
117 }
118
119 bars
120}
121
122/// Construct run bars by counting consecutive price-direction runs. A bar closes when
123/// `threshold` consecutive moves occur in the same direction. Trailing partial bars

Callers 2

bars_build_time_barsFunction · 0.85

Calls 3

build_barFunction · 0.85
is_emptyMethod · 0.80
lenMethod · 0.80

Tested by 1