1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::by_asset::stochrsi::indicator_by_assets;
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::by_option::stochrsi::indicator_by_options;
use crate::indicators::simd_indicators::{
max_simd::SimdState as MaxSimdState, min_simd::SimdState as MinSimdState,
rsi_simd::SimdState as RsiSimdState,
};
use crate::indicators::stochrsi::State;
use crate::ring_buffer::single_buffer::mirror_buffer::MirrorBuffer as SingleMirrorBuffer;
use std::f64;
use std::simd::{cmp::SimdPartialOrd, Select, Simd};
pub mod assets {
use super::*;
use crate::ring_buffer::multi_buffer::mirror_buffer::{MinMaxBuffer, MirrorBuffer};
use crate::ring_buffer::multi_buffer::multi_buffer::MultiBuffer;
/// SIMD-parallel state for computing the Stochastic RSI across `N` assets simultaneously.
/// Each field is a SIMD vector where lane `i` corresponds to asset `i`.
pub struct SimdState<const N: usize> {
/// Rolling buffer of RSI values for each lane, used to find the min/max RSI over the period.
pub buffer: MultiBuffer<N>,
/// Sub-state tracking the rolling minimum of RSI values for each lane.
pub min_state: MinSimdState<N>,
/// Sub-state tracking the rolling maximum of RSI values for each lane.
pub max_state: MaxSimdState<N>,
/// Sub-state tracking the underlying RSI computation for each lane.
pub rsi_state: RsiSimdState<N>,
}
impl<const N: usize> SimdState<N> {
/// Gathers `N` scalar [`State`] references into a single `SimdState`, packing each field into a SIMD lane.
pub fn new(states: &mut [&mut State]) -> Self {
let mut min_refs = Vec::with_capacity(N);
let mut max_refs = Vec::with_capacity(N);
let mut rsi_refs = Vec::with_capacity(N);
let mut buffer_slices = Vec::with_capacity(N);
let capacity = states[0].buffer.capacity;
for state in states.iter_mut() {
min_refs.push(&mut state.min_state);
max_refs.push(&mut state.max_state);
rsi_refs.push(&mut state.rsi_state);
buffer_slices.push(state.buffer.get_slice());
}
let buffer_refs: [&[f64]; N] =
buffer_slices.try_into().unwrap_or_else(|v: Vec<&[f64]>| {
panic!("Expected {} buffer slices, got {}", N, v.len())
});
let buffer = MultiBuffer::from_slice(buffer_refs, capacity);
let min_state = MinSimdState::new(&mut min_refs);
let max_state = MaxSimdState::new(&mut max_refs);
let rsi_state = RsiSimdState::new(&mut rsi_refs);
Self {
buffer,
min_state,
max_state,
rsi_state,
}
}
/// Writes the SIMD state back into `N` existing mutable scalar [`State`] references in place.
pub fn write_states(&self, states: &mut [&mut State]) {
let mut max_refs = Vec::with_capacity(N);
let mut min_refs = Vec::with_capacity(N);
let mut rsi_refs = Vec::with_capacity(N);
let buffers = self.buffer.to_single_buffers();
// Collect references and values
// Use zip to pair states with buffers
for (state, buffer) in states.iter_mut().zip(buffers.into_iter()) {
max_refs.push(&mut state.max_state);
min_refs.push(&mut state.min_state);
rsi_refs.push(&mut state.rsi_state);
state.buffer = buffer;
}
self.rsi_state.write_states(&mut rsi_refs);
self.max_state.write_states(&mut max_refs);
self.min_state.write_states(&mut min_refs);
}
/// Advances one bar of the Stochastic RSI computation for `N` asset lanes simultaneously.
///
/// Computes RSI for the new bar, appends it to the rolling RSI buffer, then applies
/// the stochastic formula `100 * (rsi - min_rsi) / (max_rsi - min_rsi)` over the window.
/// Returns 0 when the RSI range is below [`f64::EPSILON`].
///
/// # Returns
///
/// `(stochrsi, rsi)` — the Stochastic RSI value and the underlying RSI value for each lane.
#[inline(always)]
pub fn calc_simd<const CHUNK_SIZE: usize>(
&mut self,
real: Simd<f64, N>,
multipliers: (Simd<f64, N>, Simd<f64, N>),
period: usize,
) -> (Simd<f64, N>, Simd<f64, N>) {
let rsi = self.rsi_state.calc_simd(real, multipliers);
self.buffer.push(rsi.to_array());
let (min, _) = self
.buffer
.min::<CHUNK_SIZE>(&mut self.min_state, rsi, period);
let (max, _) = self
.buffer
.max::<CHUNK_SIZE>(&mut self.max_state, rsi, period);
let kdif = max - min;
let kfast = kdif
.simd_lt(Simd::splat(f64::EPSILON))
.select(Simd::splat(0.0), Simd::splat(100.0) * (rsi - min) / kdif);
(kfast, rsi)
}
}
}
pub mod options {
use super::*;
use crate::ring_buffer::{
unsync_multi_buffer::{
mirror_buffer::MinMaxBuffer,
multi_buffer::{MirrorBuffer, UnsyncBuffer},
},
//single_buffer::mirror_buffer::MirrorBuffer as SingleMirrorBuffer,
};
/// SIMD-parallel state for computing the Stochastic RSI across `N` option sets simultaneously.
/// Each field is a SIMD vector where lane `i` corresponds to option set `i`.
pub struct SimdState<const N: usize> {
/// Rolling buffer of RSI values for each option lane.
pub buffer: UnsyncBuffer<N, f64>,
/// Sub-state tracking the rolling minimum of RSI values for each option lane.
pub min_state: MinSimdState<N>,
/// Sub-state tracking the rolling maximum of RSI values for each option lane.
pub max_state: MaxSimdState<N>,
/// Sub-state tracking the underlying RSI computation for each option lane.
pub rsi_state: RsiSimdState<N>,
}
impl<const N: usize> SimdState<N> {
/// Gathers `N` scalar [`State`] references into a single `SimdState`, packing each field into a SIMD lane.
pub fn new(states: &mut [&mut State]) -> Self {
let mut min_refs = Vec::with_capacity(N);
let mut max_refs = Vec::with_capacity(N);
let mut rsi_refs = Vec::with_capacity(N);
let mut buffer_refs = Vec::with_capacity(N);
// Collect references and values
for state in states.iter_mut() {
min_refs.push(&mut state.min_state);
max_refs.push(&mut state.max_state);
rsi_refs.push(&mut state.rsi_state);
buffer_refs.push(&state.buffer);
}
let buffer = UnsyncBuffer::from_buffers(buffer_refs);
let min_state = MinSimdState::new(&mut min_refs);
let max_state = MaxSimdState::new(&mut max_refs);
let rsi_state = RsiSimdState::new(&mut rsi_refs);
Self {
buffer,
min_state,
max_state,
rsi_state,
}
}
/// Writes the SIMD state back into `N` existing mutable scalar [`State`] references in place.
pub fn write_states(&self, states: &mut [&mut State]) {
let mut max_refs = Vec::with_capacity(N);
let mut min_refs = Vec::with_capacity(N);
let mut rsi_refs = Vec::with_capacity(N);
let buffers = self.buffer.to_f64_buffers();
// Collect references and values
// Use zip to pair states with buffers
for (state, buffer) in states.iter_mut().zip(buffers.into_iter()) {
max_refs.push(&mut state.max_state);
min_refs.push(&mut state.min_state);
rsi_refs.push(&mut state.rsi_state);
state.buffer = buffer;
}
self.max_state.write_states(&mut max_refs);
self.min_state.write_states(&mut min_refs);
self.rsi_state.write_states(&mut rsi_refs);
}
/// Advances one bar of the Stochastic RSI computation for `N` option lanes simultaneously.
///
/// Each lane uses its own period. Computes RSI, then applies the stochastic formula
/// over each lane's own rolling window size.
///
/// # Returns
///
/// `(stochrsi, rsi)` — the Stochastic RSI and underlying RSI for each option lane.
#[inline(always)]
pub fn calc_simd(
&mut self,
real: Simd<f64, N>,
multipliers: (Simd<f64, N>, Simd<f64, N>),
period: Simd<usize, N>,
) -> (Simd<f64, N>, Simd<f64, N>) {
let rsi = self.rsi_state.calc_simd(real, multipliers);
self.buffer.push(rsi);
let (min, _) = self.buffer.min(&mut self.min_state, rsi, period);
let (max, _) = self.buffer.max(&mut self.max_state, rsi, period);
let kdif = max - min;
let kfast = kdif
.simd_lt(Simd::splat(f64::EPSILON))
.select(Simd::splat(0.0), Simd::splat(100.0) * (rsi - min) / kdif);
(kfast, rsi)
}
/// Advances one bar of the Stochastic RSI assuming the buffer is already full.
///
/// # Safety
///
/// Caller must guarantee the internal RSI buffer has been fully populated before calling.
#[inline(always)]
pub unsafe fn calc_simd_unchecked(
&mut self,
real: Simd<f64, N>,
multipliers: (Simd<f64, N>, Simd<f64, N>),
period: Simd<usize, N>,
) -> (Simd<f64, N>, Simd<f64, N>) {
let rsi = self.rsi_state.calc_simd(real, multipliers);
self.buffer.push_unchecked(rsi);
let (min, _) = self.buffer.min(&mut self.min_state, rsi, period);
let (max, _) = self.buffer.max(&mut self.max_state, rsi, period);
let kdif = max - min;
let kfast = kdif
.simd_lt(Simd::splat(f64::EPSILON))
.select(Simd::splat(0.0), Simd::splat(100.0) * (rsi - min) / kdif);
(kfast, rsi)
}
}
}