quantwave_core/indicators/
oc_price_rsi.rs1use crate::indicators::metadata::{IndicatorMetadata, ParamDef};
2use crate::indicators::momentum::RSI;
3use crate::traits::Next;
4
5#[derive(Debug, Clone)]
11pub struct OCPriceRSI {
12 rsi: RSI,
13}
14
15impl OCPriceRSI {
16 pub fn new(period: usize) -> Self {
17 Self {
18 rsi: RSI::new(period),
19 }
20 }
21}
22
23impl Default for OCPriceRSI {
24 fn default() -> Self {
25 Self::new(14)
26 }
27}
28
29impl Next<(f64, f64)> for OCPriceRSI {
30 type Output = f64;
31
32 fn next(&mut self, input: (f64, f64)) -> Self::Output {
33 let (open, close) = input;
34 let oc_avg = (open + close) / 2.0;
35 self.rsi.next(oc_avg)
36 }
37}
38
39pub const OC_PRICE_RSI_METADATA: IndicatorMetadata = IndicatorMetadata {
40 name: "OCPriceRSI",
41 description: "RSI calculated using the average of Open and Close prices to reduce noise.",
42 usage: "Use to measure momentum on the open-to-close price differential rather than close-to-close, capturing intraday directional strength more directly.",
43 keywords: &["oscillator", "rsi", "ehlers", "momentum"],
44 ehlers_summary: "Ehlers computes this RSI variant on the difference between the open and close price of each bar rather than on the closing price series. The open-close differential captures the net directional pressure within each bar, producing a momentum oscillator more sensitive to intraday commitment than standard RSI.",
45 params: &[ParamDef {
46 name: "period",
47 default: "14",
48 description: "RSI period",
49 }],
50 formula_source: "https://github.com/lavs9/quantwave/blob/main/references/Ehlers%20Papers/EveryLittleBitHelps.pdf",
51 formula_latex: r#"
52\[
53Input = \frac{Open + Close}{2}
54\]
55\[
56RSI = \text{Wilder's RSI}(Input, Period)
57\]
58"#,
59 gold_standard_file: "oc_price_rsi.json",
60 category: "Ehlers DSP",
61};
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66 use crate::test_utils::{assert_indicator_parity_oc, load_gold_standard_oc};
67 use crate::traits::Next;
68 use proptest::prelude::*;
69
70 #[test]
71 fn test_oc_price_rsi_gold_standard() {
72 let case = load_gold_standard_oc("oc_price_rsi");
73 let ocrsi = OCPriceRSI::new(14);
74 assert_indicator_parity_oc(ocrsi, &case.input, &case.expected);
75 }
76
77 #[test]
78 fn test_oc_price_rsi_basic() {
79 let mut ocrsi = OCPriceRSI::new(14);
80 for i in 0..50 {
81 let val = ocrsi.next((100.0 + i as f64, 101.0 + i as f64));
82 if i >= 14 {
83 assert!(!val.is_nan());
84 }
85 }
86 }
87
88 proptest! {
89 #[test]
90 fn test_oc_price_rsi_parity(
91 opens in prop::collection::vec(1.0..100.0, 50..100),
92 closes in prop::collection::vec(1.0..100.0, 50..100),
93 ) {
94 let period = 14;
95 let mut ocrsi = OCPriceRSI::new(period);
96
97 let min_len = opens.len().min(closes.len());
98 let inputs: Vec<(f64, f64)> = opens[..min_len].iter().cloned().zip(closes[..min_len].iter().cloned()).collect();
99 let streaming_results: Vec<f64> = inputs.iter().map(|&x| ocrsi.next(x)).collect();
100
101 let mut batch_results = Vec::with_capacity(min_len);
103 let mut rsi = RSI::new(period);
104 for &(o, c) in &inputs {
105 batch_results.push(rsi.next((o + c) / 2.0));
106 }
107
108 for (s, b) in streaming_results.iter().zip(batch_results.iter()) {
109 if s.is_nan() {
110 assert!(b.is_nan());
111 } else {
112 approx::assert_relative_eq!(s, b, epsilon = 1e-10);
113 }
114 }
115 }
116 }
117}