TAMomentum
TA.WILLR()
威廉指标 (Williams' %R)
与随机指标类似,衡量超买超卖状态,值域为 -100 到 0。
语法
python
TA.WILLR(high, low, close, timeperiod=14)参数
| 参数名 | 类型 | 必选 | 默认值 | 说明 |
|---|---|---|---|---|
| high | any | 否 | - | 最高价数组 |
| low | any | 否 | - | 最低价数组 |
| close | any | 否 | - | 收盘价数组 |
| timeperiod | any | 否 | - | 时间周期,默认 14 |
返回值
返回 Williams %R 值数组,范围 -100 到 0
计算方法
WILLR = (最高价 - 收盘价) / (最高价 - 最低价) × (-100)
信号解读
超买超卖
- WILLR > -20: 超买区域
- WILLR < -80: 超卖区域
- WILLR = -50: 中性区域
背离
- 顶背离: 价格创新高,WILLR 未创新高(未更接近 0)
- 底背离: 价格创新低,WILLR 未创新低(未更接近 -100)
基础示例
python
def main():
records = exchange.GetRecords()
highs = [r['High'] for r in records]
lows = [r['Low'] for r in records]
closes = [r['Close'] for r in records]
willr = TA.WILLR(highs, lows, closes, 14)
Log(f"Williams %R: {willr[-1]:.2f}")
if willr[-1] > -20:
Log("威廉指标超买:", willr[-1])
elif willr[-1] < -80:
Log("威廉指标超卖:", willr[-1])
else:
Log("威廉指标正常区间:", willr[-1])高级应用
1. WILLR 背离检测
python
def detect_willr_divergence(prices, willr_values, period=10):
"""检测 WILLR 背离"""
if len(prices) < period or len(willr_values) < period:
return None
# 顶背离(价格新高,WILLR 未创新高)
if (prices[-1] > max(prices[-period:-1]) and
willr_values[-1] < max(willr_values[-period:-1])):
return "顶背离"
# 底背离(价格新低,WILLR 未创新低)
if (prices[-1] < min(prices[-period:-1]) and
willr_values[-1] > min(willr_values[-period:-1])):
return "底背离"
return None
def main():
records = exchange.GetRecords()
highs = [r['High'] for r in records]
lows = [r['Low'] for r in records]
closes = [r['Close'] for r in records]
willr = TA.WILLR(highs, lows, closes, 14)
divergence = detect_willr_divergence(closes, willr, 10)
if divergence:
Log(f"WILLR {divergence}")2. WILLR 极值策略
python
def main():
records = exchange.GetRecords()
highs = [r['High'] for r in records]
lows = [r['Low'] for r in records]
closes = [r['Close'] for r in records]
willr = TA.WILLR(highs, lows, closes, 14)
# 从超卖区反转
if willr[-2] < -80 and willr[-1] >= -80:
Log("WILLR 脱离超卖区,买入信号")
# 从超买区反转
elif willr[-2] > -20 and willr[-1] <= -20:
Log("WILLR 脱离超买区,卖出信号")
# 极端值
if willr[-1] > -10:
Log("WILLR 极度超买")
elif willr[-1] < -90:
Log("WILLR 极度超卖")参数优化建议
| 周期 | 推荐参数 | 特点 |
|---|---|---|
| 短线 | WILLR(10) | 敏感 |
| 标准 | WILLR(14) | 经典配置 |
| 长线 | WILLR(20) | 平滑 |
与其他指标配合
WILLR + RSI
python
willr = TA.WILLR(highs, lows, closes, 14)
rsi = TA.RSI(closes, 14)
# 双重超卖
if willr[-1] < -80 and rsi[-1] < 30:
Log("WILLR + RSI 双重超卖")WILLR + STOCH
python
willr = TA.WILLR(highs, lows, closes, 14)
k, d = TA.STOCH(highs, lows, closes, 9, 3, 0, 3, 0)
# 多指标确认
if willr[-1] < -80 and k[-1] < 20:
Log("WILLR + KDJ 双重超卖确认")注意事项
⚠️ 重要提醒:
- 反向思维: WILLR 值域是负数,-20 是超买,-80 是超卖
- 与 STOCH 相似: 本质上是 STOCH 的变形
- 假信号: 强趋势中可能钝化
- 配合趋势: 最好在明确趋势背景下使用
相关指标
- STOCH - 随机指标 - 类似原理,不同值域
- CCI - 商品通道指标 - 另一种超买超卖指标