TAMomentum
TA.ROC()
变动率指标 (Rate of Change)
当前价格相对于 N 周期前的百分比变化,以百分比形式衡量动量。
语法
python
TA.ROC(data, timeperiod=10)参数
| 参数名 | 类型 | 必选 | 默认值 | 说明 |
|---|---|---|---|---|
| data | any | 否 | - | 价格数据数组 |
| timeperiod | any | 否 | - | 时间周期,默认 10 |
返回值
返回变动率值数组(百分比形式)
计算方法
ROC = (Close[t] - Close[t - n]) / Close[t - n] × 100
信号解读
- ROC > 0: 价格上涨
- ROC < 0: 价格下跌
- ROC 穿越 0: 趋势反转
- ROC > 10%: 强势上涨
- ROC < -10%: 强势下跌
基础示例
python
def main():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
roc = TA.ROC(closes, 10)
Log(f"10周期变动率: {roc[-1]:.2f}%")
if roc[-1] > 10:
Log("强势上涨")
elif roc[-1] < -10:
Log("强势下跌")
# 穿越零线
if roc[-1] > 0 and roc[-2] <= 0:
Log("ROC 上穿零线")
elif roc[-1] < 0 and roc[-2] >= 0:
Log("ROC 下穿零线")高级应用
1. 多周期 ROC
python
def main():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
roc_5 = TA.ROC(closes, 5)
roc_10 = TA.ROC(closes, 10)
roc_20 = TA.ROC(closes, 20)
Log(f"ROC(5): {roc_5[-1]:.2f}%")
Log(f"ROC(10): {roc_10[-1]:.2f}%")
Log(f"ROC(20): {roc_20[-1]:.2f}%")
# 多周期共振
if roc_5[-1] > 0 and roc_10[-1] > 0 and roc_20[-1] > 0:
Log("多周期ROC均为正值,强势上涨")2. ROC 极值策略
python
def main():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
roc = TA.ROC(closes, 12)
# 设定阈值
threshold_high = 15
threshold_low = -15
if roc[-1] > threshold_high:
Log(f"ROC超过{threshold_high}%,可能超买")
elif roc[-1] < threshold_low:
Log(f"ROC低于{threshold_low}%,可能超卖")参数优化建议
| 周期 | 推荐参数 | 适用场景 |
|---|---|---|
| 短线 | ROC(5-10) | 短期动量 |
| 中线 | ROC(12-20) | 中期趋势 |
| 长线 | ROC(25-50) | 长期动量 |
与其他指标配合
ROC + MA
python
roc = TA.ROC(closes, 12)
ma = TA.MA(closes, 20)
if roc[-1] > 0 and closes[-1] > ma[-1]:
Log("ROC正值 + 均线多头")注意事项
⚠️ 重要提醒:
- 百分比形式: 便于跨品种比较
- 对比 MOM: ROC 是相对值,MOM 是绝对值
- 适合趋势: ROC 更适合趋势判断
相关指标
- MOM - 动量指标 - 绝对价格差版本