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)
| 91 | /// timestamp of the current bar; the trade that crosses the interval boundary is |
| 92 | /// included in the closing bar, and accumulation restarts afterward. |
| 93 | pub 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 |