TATrend
TA.MA() - 通用移动平均线
Universal Moving Average,可选择多种移动平均类型的通用函数。
语法
python
TA.MA(data)
TA.MA(data, timeperiod=30, matype=0)参数
| 参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| data | array | 价格数据 | - |
| timeperiod | int | 计算周期 | 30 |
| matype | int | MA类型(见下表) | 0 |
MA类型
| matype | 名称 | 说明 |
|---|---|---|
| 0 | SMA | 简单移动平均 |
| 1 | EMA | 指数移动平均 |
| 2 | WMA | 加权移动平均 |
| 3 | DEMA | 双重指数移动平均 |
| 4 | TEMA | 三重指数移动平均 |
| 5 | TRIMA | 三角移动平均 |
| 6 | KAMA | 考夫曼自适应移动平均 |
| 7 | MAMA | MESA自适应移动平均 |
| 8 | T3 | T3移动平均 |
返回值
根据matype返回对应类型的移动平均线数组。
基础示例
python
def main():
records = exchange.GetRecords()
if len(records) < 50:
return
closes = [r['Close'] for r in records]
# 不同类型的MA
sma = TA.MA(closes, 20, 0) # SMA
ema = TA.MA(closes, 20, 1) # EMA
dema = TA.MA(closes, 20, 3) # DEMA
Log(f"SMA: {sma[-1]:.2f}")
Log(f"EMA: {ema[-1]:.2f}")
Log(f"DEMA: {dema[-1]:.2f}")高级应用
多MA对比策略
python
def multi_ma_compare():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
# 同周期不同类型
ma_types = {
0: "SMA",
1: "EMA",
3: "DEMA",
4: "TEMA"
}
price = closes[-1]
signals = []
for matype, name in ma_types.items():
ma = TA.MA(closes, 20, matype)
if price > ma[-1]:
signals.append(f"{name}: 看涨")
else:
signals.append(f"{name}: 看跌")
Log(" | ".join(signals))
# 所有MA一致时交易
all_bullish = all(price > TA.MA(closes, 20, mt)[-1] for mt in ma_types.keys())
all_bearish = all(price < TA.MA(closes, 20, mt)[-1] for mt in ma_types.keys())
if all_bullish:
exchange.Buy(-1, 1)
Log("所有MA一致看涨")
elif all_bearish:
exchange.Sell(-1, 1)
Log("所有MA一致看跌")动态MA选择
python
def dynamic_ma_selection():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
# 根据ADX选择MA类型
highs = [r['High'] for r in records]
lows = [r['Low'] for r in records]
adx = TA.ADX(highs, lows, closes, 14)
if adx[-1] > 30:
# 强趋势,使用快速MA
matype = 4 # TEMA
Log("强趋势,使用TEMA")
elif adx[-1] > 20:
# 中等趋势,使用EMA
matype = 1 # EMA
Log("中等趋势,使用EMA")
else:
# 震荡市,使用平滑MA
matype = 0 # SMA
Log("震荡市,使用SMA")
ma = TA.MA(closes, 20, matype)
# 交易逻辑
price = closes[-1]
if price > ma[-1] and closes[-2] <= ma[-2]:
exchange.Buy(-1, 0.5)
Log("突破MA做多")使用建议
- SMA (0):长期趋势,支撑阻力
- EMA (1):通用,日常交易
- DEMA (3):快速趋势跟踪
- TEMA (4):极速趋势捕捉
- KAMA (6):自适应,全市场
- T3 (8):平滑趋势跟踪
注意事项
- 不同MA类型特性差异大
- 需根据市场状态选择
- MAMA (7) 返回双数组,需特殊处理
- 建议先掌握单一类型再组合使用