第三版

This commit is contained in:
YuWuKunCheng
2026-05-26 09:06:28 +08:00
parent bf96517c9d
commit 7416bfc73a
17 changed files with 4622 additions and 1 deletions
+576
View File
@@ -0,0 +1,576 @@
use pyo3::prelude::*;
use pyo3::types::PyType;
// ========== 平滑异同移动平均线 ==========
#[pyclass(name = "平滑异同移动平均线")]
#[derive(Clone)]
pub struct 线Py {
pub(crate) inner: chanlun::indicators::线,
}
#[pymethods]
impl 线Py {
#[new]
fn new() -> Self {
unimplemented!("使用 首次计算 或 增量计算 创建")
}
#[getter]
fn (&self) -> String {
self.inner..to_string()
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn 线(&self) -> i64 {
self.inner.线
}
#[getter]
fn 线(&self) -> i64 {
self.inner.线
}
#[getter]
fn (&self) -> i64 {
self.inner.
}
#[getter]
fn DIF(&self) -> Option<f64> {
self.inner.DIF
}
#[getter]
fn DEA(&self) -> Option<f64> {
self.inner.DEA
}
#[getter]
fn MACD柱(&self) -> f64 {
self.inner.MACD柱
}
#[getter]
fn 线EMA(&self) -> Option<f64> {
self.inner.线EMA
}
#[getter]
fn 线EMA(&self) -> Option<f64> {
self.inner.线EMA
}
#[getter]
fn DEA_EMA(&self) -> Option<f64> {
self.inner.DEA_EMA
}
fn __str__(&self) -> String {
format!(
"平滑异同移动平均线(DIF={:?}, DEA={:?}, BAR={:?})",
self.inner.DIF, self.inner.DEA, self.inner.MACD柱
)
}
fn __repr__(&self) -> String {
self.__str__()
}
#[classmethod]
#[pyo3(signature = (初始收盘价, 初始时间, 快线周期 = None, 慢线周期 = None, 信号周期 = None))]
fn (
_cls: &Bound<'_, PyType>,
: f64,
: i64,
线: Option<i64>,
线: Option<i64>,
: Option<i64>,
) -> Self {
let inner = chanlun::indicators::线::(
,
,
线.unwrap_or(12),
线.unwrap_or(26),
.unwrap_or(9),
);
Self { inner }
}
#[classmethod]
#[pyo3(signature = (k线, 计算方式, 快线周期 = None, 慢线周期 = None, 信号周期 = None))]
fn _K线(
_cls: &Bound<'_, PyType>,
k线: &Bound<'_, PyAny>,
: &str,
线: Option<i64>,
线: Option<i64>,
: Option<i64>,
) -> PyResult<Self> {
let = crate::indicators_py::K线取值(k线, )?;
Ok(Self {
inner: chanlun::indicators::线::(
,
(k线)?,
线.unwrap_or(12),
线.unwrap_or(26),
.unwrap_or(9),
),
})
}
#[classmethod]
fn (
_cls: &Bound<'_, PyType>,
MACD: &Bound<'_, 线Py>,
: f64,
: i64,
) -> Self {
let inner = chanlun::indicators::线::(
&MACD.borrow().inner,
,
,
);
Self { inner }
}
#[classmethod]
fn _K线(
_cls: &Bound<'_, PyType>,
MACD: &Bound<'_, 线Py>,
K线: &Bound<'_, PyAny>,
: &str,
) -> PyResult<Self> {
let = K线取值(K线, )?;
Ok(Self {
inner: chanlun::indicators::线::(
&MACD.borrow().inner,
,
(K线)?,
),
})
}
}
// ========== 相对强弱指数 ==========
#[pyclass(name = "相对强弱指数")]
#[derive(Clone)]
pub struct Py {
pub(crate) inner: chanlun::indicators::,
}
#[pymethods]
impl Py {
#[new]
fn new() -> Self {
unimplemented!("使用 首次计算 或 增量计算 创建")
}
#[getter]
fn (&self) -> String {
self.inner..to_string()
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn (&self) -> i64 {
self.inner.
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn RSI_SMA周期(&self) -> Option<i64> {
self.inner.RSI_SMA周期
}
#[getter]
fn RSI(&self) -> Option<f64> {
self.inner.RSI
}
#[getter]
fn (&self) -> Option<f64> {
self.inner.
}
#[getter]
fn (&self) -> Option<f64> {
self.inner.
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn RSI_SMA(&self) -> Option<f64> {
self.inner.RSI_SMA
}
#[getter]
fn RSI历史队列(&self) -> Vec<f64> {
self.inner.RSI历史队列.clone()
}
fn __str__(&self) -> String {
format!("相对强弱指数(RSI={:?})", self.inner.RSI)
}
fn __repr__(&self) -> String {
self.__str__()
}
#[classmethod]
#[pyo3(signature = (初始收盘价, 初始时间, 周期 = None, 超买阈值 = None, 超卖阈值 = None, RSI_SMA周期 = None))]
fn (
_cls: &Bound<'_, PyType>,
: f64,
: i64,
: Option<i64>,
: Option<f64>,
: Option<f64>,
RSI_SMA周期: Option<i64>,
) -> Self {
Self {
inner: chanlun::indicators::::(
,
,
.unwrap_or(14),
.unwrap_or(70.0),
.unwrap_or(30.0),
RSI_SMA周期,
),
}
}
#[classmethod]
#[pyo3(signature = (k线, 计算方式, 周期 = None, 超买阈值 = None, 超卖阈值 = None, RSI_SMA周期 = None))]
fn _K线(
_cls: &Bound<'_, PyType>,
k线: &Bound<'_, PyAny>,
: &str,
: Option<i64>,
: Option<f64>,
: Option<f64>,
RSI_SMA周期: Option<i64>,
) -> PyResult<Self> {
let = K线取值(k线, )?;
Ok(Self {
inner: chanlun::indicators::::(
,
(k线)?,
.unwrap_or(14),
.unwrap_or(70.0),
.unwrap_or(30.0),
RSI_SMA周期,
),
})
}
#[classmethod]
fn (
_cls: &Bound<'_, PyType>,
RSI: &Bound<'_, Py>,
: f64,
: i64,
) -> Self {
Self {
inner: chanlun::indicators::::(
&RSI.borrow().inner,
,
,
),
}
}
#[classmethod]
fn _K线(
_cls: &Bound<'_, PyType>,
RSI: &Bound<'_, Py>,
K线: &Bound<'_, PyAny>,
: &str,
) -> PyResult<Self> {
let = K线取值(K线, )?;
Ok(Self {
inner: chanlun::indicators::::(
&RSI.borrow().inner,
,
(K线)?,
),
})
}
}
// ========== 随机指标 ==========
#[pyclass(name = "随机指标")]
#[derive(Clone)]
pub struct Py {
pub(crate) inner: chanlun::indicators::,
}
#[pymethods]
impl Py {
#[new]
fn new() -> Self {
unimplemented!("使用 首次计算 或 增量计算 创建")
}
#[getter]
fn (&self) -> String {
self.inner..to_string()
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn N(&self) -> i64 {
self.inner.N
}
#[getter]
fn M1(&self) -> i64 {
self.inner.M1
}
#[getter]
fn M2(&self) -> i64 {
self.inner.M2
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[getter]
fn RSV(&self) -> Option<f64> {
self.inner.RSV
}
#[getter]
fn K(&self) -> Option<f64> {
self.inner.K
}
#[getter]
fn D(&self) -> Option<f64> {
self.inner.D
}
#[getter]
fn J(&self) -> Option<f64> {
self.inner.J
}
#[getter]
fn (&self) -> Vec<f64> {
self.inner..clone()
}
#[getter]
fn (&self) -> Vec<f64> {
self.inner..clone()
}
#[getter]
fn RSV(&self) -> Option<f64> {
self.inner.RSV
}
#[getter]
fn K(&self) -> Option<f64> {
self.inner.K
}
#[getter]
fn D(&self) -> Option<f64> {
self.inner.D
}
fn __str__(&self) -> String {
format!(
"随机指标(K={:?}, D={:?}, J={:?})",
self.inner.K, self.inner.D, self.inner.J
)
}
fn __repr__(&self) -> String {
self.__str__()
}
#[classmethod]
#[pyo3(signature = (初始最高价, 初始最低价, 初始收盘价, 初始时间, N = None, M1 = None, M2 = None, 超买阈值 = None, 超卖阈值 = None))]
fn (
_cls: &Bound<'_, PyType>,
: f64,
: f64,
: f64,
: i64,
N: Option<i64>,
M1: Option<i64>,
M2: Option<i64>,
: Option<f64>,
: Option<f64>,
) -> Self {
Self {
inner: chanlun::indicators::::(
,
,
,
,
N.unwrap_or(9),
M1.unwrap_or(3),
M2.unwrap_or(3),
.unwrap_or(80.0),
.unwrap_or(20.0),
),
}
}
#[classmethod]
#[pyo3(signature = (k线, _计算方式, RSV周期 = None, K值平滑周期 = None, D值平滑周期 = None, 超买阈值 = None, 超卖阈值 = None))]
fn _K线(
_cls: &Bound<'_, PyType>,
k线: &Bound<'_, PyAny>,
_计算方式: &str,
RSV周期: Option<i64>,
K值平滑周期: Option<i64>,
D值平滑周期: Option<i64>,
: Option<f64>,
: Option<f64>,
) -> PyResult<Self> {
let : f64 = k线.getattr("收盘价")?.extract()?;
let : f64 = k线.getattr("")?.extract()?;
let : f64 = k线.getattr("")?.extract()?;
Ok(Self {
inner: chanlun::indicators::::(
,
,
,
(k线)?,
RSV周期.unwrap_or(9),
K值平滑周期.unwrap_or(3),
D值平滑周期.unwrap_or(3),
.unwrap_or(80.0),
.unwrap_or(20.0),
),
})
}
#[classmethod]
fn (
_cls: &Bound<'_, PyType>,
KDJ: &Bound<'_, Py>,
: f64,
: f64,
: f64,
: i64,
) -> Self {
Self {
inner: chanlun::indicators::::(
&KDJ.borrow().inner,
,
,
,
,
),
}
}
#[classmethod]
fn _K线(
_cls: &Bound<'_, PyType>,
KDJ: &Bound<'_, Py>,
K线: &Bound<'_, PyAny>,
_计算方式: &str,
) -> PyResult<Self> {
let : f64 = K线.getattr("收盘价")?.extract()?;
let : f64 = K线.getattr("")?.extract()?;
let : f64 = K线.getattr("")?.extract()?;
Ok(Self {
inner: chanlun::indicators::::(
&KDJ.borrow().inner,
,
,
,
(K线)?,
),
})
}
}
// ========== 指标 (static namespace) ==========
#[pyclass(name = "指标")]
pub struct Py;
#[pymethods]
impl Py {
#[classmethod]
fn K线取值(
_cls: &Bound<'_, PyType>,
k线: &Bound<'_, PyAny>,
: &str,
) -> PyResult<f64> {
K线取值(k线, )
}
}
// ========== Helper functions ==========
pub(crate) fn K线取值(k线: &Bound<'_, PyAny>, : &str) -> PyResult<f64> {
let : f64 = k线.getattr("开盘价")?.extract()?;
let : f64 = k线.getattr("")?.extract()?;
let : f64 = k线.getattr("")?.extract()?;
let : f64 = k线.getattr("收盘价")?.extract()?;
Ok(chanlun::indicators::K线取值(
,
,
,
,
,
))
}
fn (k线: &Bound<'_, PyAny>) -> PyResult<i64> {
let ts = k线.getattr("时间戳")?;
if let Ok(i) = ts.extract::<i64>() {
return Ok(i);
}
// Try float
if let Ok(f) = ts.extract::<f64>() {
return Ok(f as i64);
}
// Try calling .timestamp() if it's a datetime
if let Ok(method) = ts.getattr("timestamp") {
let result: f64 = method.call0()?.extract()?;
return Ok(result as i64);
}
Err(pyo3::exceptions::PyTypeError::new_err("无法获取时间戳"))
}
pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<线Py>()?;
m.add_class::<Py>()?;
m.add_class::<Py>()?;
m.add_class::<Py>()?;
Ok(())
}