Files

126 lines
4.0 KiB
Python

"""
自定义指标库 — 已全面接入 ferro-ta 原生 Rust 实现。
v2 变更: 所有在 raptorbt (ferro-ta) 中已有原生实现的指标, 不再用 Python
重新计算, 而是直接转发到原生函数, 获得亚毫秒级性能。
原生转发 (10 个): typical_price→typprice, cci, williams_r→willr,
roc, trix, dmi→adx_all, ichimoku, parabolic_sar→sar, mfi, obv
原生组合 (1 个): awesome_oscillator = sma(medprice) - sma(medprice)
使用方式:
from my_indicators import cci, awesome_oscillator
cci_values = cci(high, low, close, period=20)
"""
import numpy as np
import raptorbt
# ============================================================================
# 原生转发层 — 直接调用 ferro-ta Rust 实现
# ============================================================================
def typical_price(high: np.ndarray, low: np.ndarray, close: np.ndarray) -> np.ndarray:
"""典型价格 (H+L+C)/3 — 转发 raptorbt.typprice"""
return raptorbt.typprice(high, low, close)
def cci(high: np.ndarray, low: np.ndarray, close: np.ndarray, period: int = 20) -> np.ndarray:
"""商品通道指数 — 转发 raptorbt.cci"""
return raptorbt.cci(high, low, close, period)
def williams_r(high: np.ndarray, low: np.ndarray, close: np.ndarray, period: int = 14) -> np.ndarray:
"""威廉指标 (-100~0) — 转发 raptorbt.willr"""
return raptorbt.willr(high, low, close, period)
def roc(data: np.ndarray, period: int = 12) -> np.ndarray:
"""变化率 — 转发 raptorbt.roc"""
return raptorbt.roc(data, period)
def trix(data: np.ndarray, period: int = 15) -> np.ndarray:
"""三重指数平滑变化率 — 转发 raptorbt.trix"""
return raptorbt.trix(data, period)
def dmi(high: np.ndarray, low: np.ndarray, close: np.ndarray, period: int = 14):
"""
方向运动指标 (DMI) — 转发 raptorbt.adx_all
返回: (plus_di, minus_di, adx)
"""
adx, plus_di, minus_di = raptorbt.adx_all(high, low, close, period)
return plus_di, minus_di, adx
def ichimoku(
high: np.ndarray,
low: np.ndarray,
close: np.ndarray,
tenkan_period: int = 9,
kijun_period: int = 26,
senkou_b_period: int = 52,
displacement: int = 26,
):
"""一目均衡表 — 转发 raptorbt.ichimoku
返回: (tenkan, kijun, senkou_a, senkou_b, chikou)
"""
return raptorbt.ichimoku(
high, low, close, tenkan_period, kijun_period, senkou_b_period, displacement
)
def parabolic_sar(
high: np.ndarray,
low: np.ndarray,
close: np.ndarray,
step: float = 0.02,
max_step: float = 0.2,
) -> np.ndarray:
"""抛物线 SAR — 转发 raptorbt.sar (close 参数仅用于保持签名兼容, 不参与计算)"""
return raptorbt.sar(high, low, acceleration=step, maximum=max_step)
def mfi(
high: np.ndarray,
low: np.ndarray,
close: np.ndarray,
volume: np.ndarray,
period: int = 14,
) -> np.ndarray:
"""资金流量指数 (0~100) — 转发 raptorbt.mfi"""
return raptorbt.mfi(high, low, close, volume, period)
def obv(close: np.ndarray, volume: np.ndarray) -> np.ndarray:
"""能量潮 — 转发 raptorbt.obv"""
return raptorbt.obv(close, volume)
# ============================================================================
# 原生组合层 — 基于 raptorbt 原生函数组合 (无需新增 Rust 代码)
# ============================================================================
def awesome_oscillator(
high: np.ndarray,
low: np.ndarray,
fast_period: int = 5,
slow_period: int = 34,
) -> np.ndarray:
"""
鳄鱼振荡器 (Awesome Oscillator)
AO = SMA(median_price, fast) - SMA(median_price, slow)
其中 median_price = (high + low) / 2
基于 raptorbt.medprice + raptorbt.sma 原生 Rust 实现。
"""
midpoint = raptorbt.medprice(high, low) # 原生: (H+L)/2
sma_fast = raptorbt.sma(midpoint, fast_period) # 原生 SMA
sma_slow = raptorbt.sma(midpoint, slow_period) # 原生 SMA
return sma_fast - sma_slow