diff --git a/bindings/node/src/lib.rs b/bindings/node/src/lib.rs index c3bc26cf..0dec6f46 100644 --- a/bindings/node/src/lib.rs +++ b/bindings/node/src/lib.rs @@ -305,6 +305,11 @@ impl StochNode { low: Vec, close: Vec, ) -> napi::Result> { + if high.len() != low.len() || low.len() != close.len() { + return Err(NapiError::from_reason( + "high, low, close must be equal length".to_string(), + )); + } let n = high.len(); let mut out = vec![f64::NAN; n * 2]; for i in 0..n { @@ -411,6 +416,11 @@ impl AdxNode { low: Vec, close: Vec, ) -> napi::Result> { + if high.len() != low.len() || low.len() != close.len() { + return Err(NapiError::from_reason( + "high, low, close must be equal length".to_string(), + )); + } let n = high.len(); let mut out = vec![f64::NAN; n * 3]; for i in 0..n { @@ -451,6 +461,11 @@ impl CciNode { low: Vec, close: Vec, ) -> napi::Result> { + if high.len() != low.len() || low.len() != close.len() { + return Err(NapiError::from_reason( + "high, low, close must be equal length".to_string(), + )); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { out.push( @@ -486,6 +501,11 @@ impl WilliamsRNode { low: Vec, close: Vec, ) -> napi::Result> { + if high.len() != low.len() || low.len() != close.len() { + return Err(NapiError::from_reason( + "high, low, close must be equal length".to_string(), + )); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { out.push( @@ -528,6 +548,11 @@ impl MfiNode { close: Vec, volume: Vec, ) -> napi::Result> { + if high.len() != low.len() || low.len() != close.len() || close.len() != volume.len() { + return Err(NapiError::from_reason( + "high, low, close, volume must be equal length".to_string(), + )); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { out.push( @@ -563,6 +588,11 @@ impl PsarNode { low: Vec, close: Vec, ) -> napi::Result> { + if high.len() != low.len() || low.len() != close.len() { + return Err(NapiError::from_reason( + "high, low, close must be equal length".to_string(), + )); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { out.push( @@ -617,6 +647,11 @@ impl KeltnerNode { low: Vec, close: Vec, ) -> napi::Result> { + if high.len() != low.len() || low.len() != close.len() { + return Err(NapiError::from_reason( + "high, low, close must be equal length".to_string(), + )); + } let n = high.len(); let mut out = vec![f64::NAN; n * 3]; for i in 0..n { @@ -661,6 +696,11 @@ impl DonchianNode { } #[napi] pub fn batch(&mut self, high: Vec, low: Vec) -> napi::Result> { + if high.len() != low.len() { + return Err(NapiError::from_reason( + "high and low must be equal length".to_string(), + )); + } let n = high.len(); let mut out = vec![f64::NAN; n * 3]; for i in 0..n { @@ -709,6 +749,11 @@ impl VwapNode { close: Vec, volume: Vec, ) -> napi::Result> { + if high.len() != low.len() || low.len() != close.len() || close.len() != volume.len() { + return Err(NapiError::from_reason( + "high, low, close, volume must be equal length".to_string(), + )); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { out.push( @@ -739,6 +784,11 @@ impl AoNode { } #[napi] pub fn batch(&mut self, high: Vec, low: Vec) -> napi::Result> { + if high.len() != low.len() { + return Err(NapiError::from_reason( + "high and low must be equal length".to_string(), + )); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { out.push( @@ -778,6 +828,11 @@ impl AroonNode { } #[napi] pub fn batch(&mut self, high: Vec, low: Vec) -> napi::Result> { + if high.len() != low.len() { + return Err(NapiError::from_reason( + "high and low must be equal length".to_string(), + )); + } let n = high.len(); let mut out = vec![f64::NAN; n * 2]; for i in 0..n { diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 2f589999..8bf108f4 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -901,6 +901,11 @@ impl PyWilliamsR { let h = high.as_slice().expect("contiguous"); let l = low.as_slice().expect("contiguous"); let c = close.as_slice().expect("contiguous"); + if h.len() != l.len() || l.len() != c.len() { + return Err(PyValueError::new_err( + "high, low, close must be equal length", + )); + } let mut out = Vec::with_capacity(h.len()); for i in 0..h.len() { let candle = wc::Candle::new(c[i], h[i], l[i], c[i], 0.0, 0).map_err(map_err)?; @@ -952,6 +957,11 @@ impl PyAdx { let h = high.as_slice().expect("contiguous"); let l = low.as_slice().expect("contiguous"); let c = close.as_slice().expect("contiguous"); + if h.len() != l.len() || l.len() != c.len() { + return Err(PyValueError::new_err( + "high, low, close must be equal length", + )); + } let n = h.len(); let mut out = vec![f64::NAN; n * 3]; for i in 0..n { @@ -1010,6 +1020,11 @@ impl PyMfi { let l = low.as_slice().expect("contiguous"); let c = close.as_slice().expect("contiguous"); let v = volume.as_slice().expect("contiguous"); + if h.len() != l.len() || l.len() != c.len() || c.len() != v.len() { + return Err(PyValueError::new_err( + "high, low, close, volume must be equal length", + )); + } let mut out = Vec::with_capacity(h.len()); for i in 0..h.len() { let candle = wc::Candle::new(c[i], h[i], l[i], c[i], v[i], 0).map_err(map_err)?; @@ -1097,6 +1112,11 @@ impl PyPsar { let h = high.as_slice().expect("contiguous"); let l = low.as_slice().expect("contiguous"); let c = close.as_slice().expect("contiguous"); + if h.len() != l.len() || l.len() != c.len() { + return Err(PyValueError::new_err( + "high, low, close must be equal length", + )); + } let mut out = Vec::with_capacity(h.len()); for i in 0..h.len() { let candle = wc::Candle::new(c[i], h[i], l[i], c[i], 0.0, 0).map_err(map_err)?; @@ -1148,6 +1168,11 @@ impl PyKeltner { let h = high.as_slice().expect("contiguous"); let l = low.as_slice().expect("contiguous"); let c = close.as_slice().expect("contiguous"); + if h.len() != l.len() || l.len() != c.len() { + return Err(PyValueError::new_err( + "high, low, close must be equal length", + )); + } let n = h.len(); let mut out = vec![f64::NAN; n * 3]; for i in 0..n { @@ -1262,6 +1287,11 @@ impl PyVwap { let l = low.as_slice().expect("contiguous"); let c = close.as_slice().expect("contiguous"); let v = volume.as_slice().expect("contiguous"); + if h.len() != l.len() || l.len() != c.len() || c.len() != v.len() { + return Err(PyValueError::new_err( + "high, low, close, volume must be equal length", + )); + } let mut out = Vec::with_capacity(h.len()); for i in 0..h.len() { let candle = wc::Candle::new(c[i], h[i], l[i], c[i], v[i], 0).map_err(map_err)?; @@ -1309,6 +1339,9 @@ impl PyAo { ) -> PyResult>> { let h = high.as_slice().expect("contiguous"); let l = low.as_slice().expect("contiguous"); + if h.len() != l.len() { + return Err(PyValueError::new_err("high and low must be equal length")); + } let mut out = Vec::with_capacity(h.len()); for i in 0..h.len() { let candle = wc::Candle::new(l[i], h[i], l[i], l[i], 0.0, 0).map_err(map_err)?; @@ -1356,6 +1389,9 @@ impl PyAroon { ) -> PyResult>> { let h = high.as_slice().expect("contiguous"); let l = low.as_slice().expect("contiguous"); + if h.len() != l.len() { + return Err(PyValueError::new_err("high and low must be equal length")); + } let n = h.len(); let mut out = vec![f64::NAN; n * 2]; for i in 0..n { diff --git a/bindings/wasm/src/lib.rs b/bindings/wasm/src/lib.rs index fcf7580d..c49087ff 100644 --- a/bindings/wasm/src/lib.rs +++ b/bindings/wasm/src/lib.rs @@ -342,6 +342,9 @@ impl WasmAdx { low: &[f64], close: &[f64], ) -> Result { + if high.len() != low.len() || low.len() != close.len() { + return Err(JsError::new("high, low, close must be equal length")); + } let n = high.len(); let mut out = vec![f64::NAN; n * 3]; for i in 0..n { @@ -375,6 +378,9 @@ impl WasmWilliamsR { low: &[f64], close: &[f64], ) -> Result { + if high.len() != low.len() || low.len() != close.len() { + return Err(JsError::new("high, low, close must be equal length")); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { let c = make_candle(high[i], low[i], close[i], 0.0)?; @@ -403,6 +409,9 @@ impl WasmCci { low: &[f64], close: &[f64], ) -> Result { + if high.len() != low.len() || low.len() != close.len() { + return Err(JsError::new("high, low, close must be equal length")); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { let c = make_candle(high[i], low[i], close[i], 0.0)?; @@ -432,6 +441,9 @@ impl WasmMfi { close: &[f64], volume: &[f64], ) -> Result { + if high.len() != low.len() || low.len() != close.len() || close.len() != volume.len() { + return Err(JsError::new("high, low, close, volume must be equal length")); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { let c = make_candle(high[i], low[i], close[i], volume[i])?; @@ -460,6 +472,9 @@ impl WasmPsar { low: &[f64], close: &[f64], ) -> Result { + if high.len() != low.len() || low.len() != close.len() { + return Err(JsError::new("high, low, close must be equal length")); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { let c = make_candle(high[i], low[i], close[i], 0.0)?; @@ -492,6 +507,9 @@ impl WasmKeltner { low: &[f64], close: &[f64], ) -> Result { + if high.len() != low.len() || low.len() != close.len() { + return Err(JsError::new("high, low, close must be equal length")); + } let n = high.len(); let mut out = vec![f64::NAN; n * 3]; for i in 0..n { @@ -520,6 +538,9 @@ impl WasmDonchian { }) } pub fn batch(&mut self, high: &[f64], low: &[f64]) -> Result { + if high.len() != low.len() { + return Err(JsError::new("high and low must be equal length")); + } let n = high.len(); let mut out = vec![f64::NAN; n * 3]; for i in 0..n { @@ -560,6 +581,9 @@ impl WasmVwap { close: &[f64], volume: &[f64], ) -> Result { + if high.len() != low.len() || low.len() != close.len() || close.len() != volume.len() { + return Err(JsError::new("high, low, close, volume must be equal length")); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { let c = make_candle(high[i], low[i], close[i], volume[i])?; @@ -583,6 +607,9 @@ impl WasmAo { }) } pub fn batch(&mut self, high: &[f64], low: &[f64]) -> Result { + if high.len() != low.len() { + return Err(JsError::new("high and low must be equal length")); + } let mut out = Vec::with_capacity(high.len()); for i in 0..high.len() { let c = make_candle(high[i], low[i], low[i], 0.0)?; @@ -607,6 +634,9 @@ impl WasmAroon { } /// Returns `[up0, down0, up1, down1, ...]`, length `2n`. pub fn batch(&mut self, high: &[f64], low: &[f64]) -> Result { + if high.len() != low.len() { + return Err(JsError::new("high and low must be equal length")); + } let n = high.len(); let mut out = vec![f64::NAN; n * 2]; for i in 0..n {