feat: init the repo
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
use crate::validation;
|
||||
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
|
||||
use pyo3::prelude::*;
|
||||
|
||||
/// Average Price: (open + high + low + close) / 4.
|
||||
#[pyfunction]
|
||||
pub fn avgprice<'py>(
|
||||
py: Python<'py>,
|
||||
open: PyReadonlyArray1<'py, f64>,
|
||||
high: PyReadonlyArray1<'py, f64>,
|
||||
low: PyReadonlyArray1<'py, f64>,
|
||||
close: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<Bound<'py, PyArray1<f64>>> {
|
||||
let opens = open.as_slice()?;
|
||||
let highs = high.as_slice()?;
|
||||
let lows = low.as_slice()?;
|
||||
let closes = close.as_slice()?;
|
||||
let n = opens.len();
|
||||
validation::validate_equal_length(&[
|
||||
(n, "open"),
|
||||
(highs.len(), "high"),
|
||||
(lows.len(), "low"),
|
||||
(closes.len(), "close"),
|
||||
])?;
|
||||
let result: Vec<f64> = opens
|
||||
.iter()
|
||||
.zip(highs.iter())
|
||||
.zip(lows.iter())
|
||||
.zip(closes.iter())
|
||||
.map(|(((&o, &h), &l), &c)| (o + h + l + c) / 4.0)
|
||||
.collect();
|
||||
Ok(result.into_pyarray(py))
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use crate::validation;
|
||||
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
|
||||
use pyo3::prelude::*;
|
||||
|
||||
/// Median Price: (high + low) / 2.
|
||||
#[pyfunction]
|
||||
pub fn medprice<'py>(
|
||||
py: Python<'py>,
|
||||
high: PyReadonlyArray1<'py, f64>,
|
||||
low: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<Bound<'py, PyArray1<f64>>> {
|
||||
let highs = high.as_slice()?;
|
||||
let lows = low.as_slice()?;
|
||||
let n = highs.len();
|
||||
validation::validate_equal_length(&[(n, "high"), (lows.len(), "low")])?;
|
||||
let result: Vec<f64> = highs
|
||||
.iter()
|
||||
.zip(lows.iter())
|
||||
.map(|(&h, &l)| (h + l) / 2.0)
|
||||
.collect();
|
||||
Ok(result.into_pyarray(py))
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
//! Price transformations — helper functions to synthesize OHLC arrays into single price arrays.
|
||||
//! Each transform lives in its own file for maintainability.
|
||||
|
||||
mod avgprice;
|
||||
mod medprice;
|
||||
mod typprice;
|
||||
mod wclprice;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::avgprice::avgprice, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::medprice::medprice, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::typprice::typprice, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::wclprice::wclprice, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
use crate::validation;
|
||||
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
|
||||
use pyo3::prelude::*;
|
||||
|
||||
/// Typical Price: (high + low + close) / 3.
|
||||
#[pyfunction]
|
||||
pub fn typprice<'py>(
|
||||
py: Python<'py>,
|
||||
high: PyReadonlyArray1<'py, f64>,
|
||||
low: PyReadonlyArray1<'py, f64>,
|
||||
close: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<Bound<'py, PyArray1<f64>>> {
|
||||
let highs = high.as_slice()?;
|
||||
let lows = low.as_slice()?;
|
||||
let closes = close.as_slice()?;
|
||||
let n = highs.len();
|
||||
validation::validate_equal_length(&[
|
||||
(n, "high"),
|
||||
(lows.len(), "low"),
|
||||
(closes.len(), "close"),
|
||||
])?;
|
||||
let result: Vec<f64> = highs
|
||||
.iter()
|
||||
.zip(lows.iter())
|
||||
.zip(closes.iter())
|
||||
.map(|((&h, &l), &c)| (h + l + c) / 3.0)
|
||||
.collect();
|
||||
Ok(result.into_pyarray(py))
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
use crate::validation;
|
||||
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
|
||||
use pyo3::prelude::*;
|
||||
|
||||
/// Weighted Close Price: (high + low + close * 2) / 4.
|
||||
#[pyfunction]
|
||||
pub fn wclprice<'py>(
|
||||
py: Python<'py>,
|
||||
high: PyReadonlyArray1<'py, f64>,
|
||||
low: PyReadonlyArray1<'py, f64>,
|
||||
close: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<Bound<'py, PyArray1<f64>>> {
|
||||
let highs = high.as_slice()?;
|
||||
let lows = low.as_slice()?;
|
||||
let closes = close.as_slice()?;
|
||||
let n = highs.len();
|
||||
validation::validate_equal_length(&[
|
||||
(n, "high"),
|
||||
(lows.len(), "low"),
|
||||
(closes.len(), "close"),
|
||||
])?;
|
||||
let result: Vec<f64> = highs
|
||||
.iter()
|
||||
.zip(lows.iter())
|
||||
.zip(closes.iter())
|
||||
.map(|((&h, &l), &c)| (h + l + c * 2.0) / 4.0)
|
||||
.collect();
|
||||
Ok(result.into_pyarray(py))
|
||||
}
|
||||
Reference in New Issue
Block a user