feat(factors): 新增 Phase 1-2 数学和统计因子函数
- 新增 atan, log1p 数学函数 - 新增 ts_var, ts_skew, ts_kurt, ts_pct_change, ts_ema 统计函数 - 新增 ts_atr, ts_rsi, ts_obv TA-Lib 技术指标函数 - 新增完整集成测试覆盖所有新函数
This commit is contained in:
@@ -190,6 +190,130 @@ def ts_cov(x: Union[Node, str], y: Union[Node, str], window: int) -> FunctionNod
|
||||
return FunctionNode("ts_cov", x, y, window)
|
||||
|
||||
|
||||
def ts_var(x: Union[Node, str], window: int) -> FunctionNode:
|
||||
"""时间序列方差。
|
||||
|
||||
计算给定因子在滚动窗口内的方差。
|
||||
|
||||
Args:
|
||||
x: 输入因子表达式或字段名字符串
|
||||
window: 滚动窗口大小
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("ts_var", x, window)
|
||||
|
||||
|
||||
def ts_skew(x: Union[Node, str], window: int) -> FunctionNode:
|
||||
"""时间序列偏度。
|
||||
|
||||
计算给定因子在滚动窗口内的偏度(三阶矩)。
|
||||
|
||||
Args:
|
||||
x: 输入因子表达式或字段名字符串
|
||||
window: 滚动窗口大小
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("ts_skew", x, window)
|
||||
|
||||
|
||||
def ts_kurt(x: Union[Node, str], window: int) -> FunctionNode:
|
||||
"""时间序列峰度。
|
||||
|
||||
计算给定因子在滚动窗口内的峰度(四阶矩)。
|
||||
|
||||
Args:
|
||||
x: 输入因子表达式或字段名字符串
|
||||
window: 滚动窗口大小
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("ts_kurt", x, window)
|
||||
|
||||
|
||||
def ts_pct_change(x: Union[Node, str], periods: int) -> FunctionNode:
|
||||
"""时间序列百分比变化。
|
||||
|
||||
计算给定因子与 N 个周期前的百分比变化:(x - x.shift(n)) / x.shift(n)。
|
||||
|
||||
Args:
|
||||
x: 输入因子表达式或字段名字符串
|
||||
periods: 滞后期数
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("ts_pct_change", x, periods)
|
||||
|
||||
|
||||
def ts_ema(x: Union[Node, str], window: int) -> FunctionNode:
|
||||
"""指数移动平均。
|
||||
|
||||
计算给定因子的指数移动平均值。
|
||||
|
||||
Args:
|
||||
x: 输入因子表达式或字段名字符串
|
||||
window: 指数移动平均的 span 参数
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("ts_ema", x, window)
|
||||
|
||||
|
||||
def ts_atr(
|
||||
high: Union[Node, str], low: Union[Node, str], close: Union[Node, str], window: int
|
||||
) -> FunctionNode:
|
||||
"""平均真实波幅 (Average True Range)。
|
||||
|
||||
计算给定窗口内的平均真实波幅,使用 TA-Lib 实现。
|
||||
|
||||
Args:
|
||||
high: 最高价表达式或字段名字符串
|
||||
low: 最低价表达式或字段名字符串
|
||||
close: 收盘价表达式或字段名字符串
|
||||
window: 滚动窗口大小
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("ts_atr", high, low, close, window)
|
||||
|
||||
|
||||
def ts_rsi(close: Union[Node, str], window: int) -> FunctionNode:
|
||||
"""相对强弱指数 (Relative Strength Index)。
|
||||
|
||||
计算给定窗口内的 RSI 值,使用 TA-Lib 实现。
|
||||
|
||||
Args:
|
||||
close: 收盘价表达式或字段名字符串
|
||||
window: 滚动窗口大小
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("ts_rsi", close, window)
|
||||
|
||||
|
||||
def ts_obv(close: Union[Node, str], volume: Union[Node, str]) -> FunctionNode:
|
||||
"""能量潮指标 (On Balance Volume)。
|
||||
|
||||
计算 OBV 值,使用 TA-Lib 实现。
|
||||
|
||||
Args:
|
||||
close: 收盘价表达式或字段名字符串
|
||||
volume: 成交量表达式或字段名字符串
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("ts_obv", close, volume)
|
||||
|
||||
|
||||
def ts_rank(x: Union[Node, str], window: int) -> FunctionNode:
|
||||
"""时间序列排名。
|
||||
|
||||
@@ -429,6 +553,34 @@ def clip(
|
||||
return FunctionNode("clip", x, _ensure_node(lower), _ensure_node(upper))
|
||||
|
||||
|
||||
def atan(x: Union[Node, str]) -> FunctionNode:
|
||||
"""反正切函数。
|
||||
|
||||
计算输入值的反正切值(弧度)。
|
||||
|
||||
Args:
|
||||
x: 输入因子表达式或字段名字符串
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("atan", x)
|
||||
|
||||
|
||||
def log1p(x: Union[Node, str]) -> FunctionNode:
|
||||
"""log(1+x) 函数。
|
||||
|
||||
计算 log(1+x),对 x 接近 0 的情况更精确。
|
||||
|
||||
Args:
|
||||
x: 输入因子表达式或字段名字符串
|
||||
|
||||
Returns:
|
||||
FunctionNode: 函数调用节点
|
||||
"""
|
||||
return FunctionNode("log1p", x)
|
||||
|
||||
|
||||
# ==================== 条件函数 ====================
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user