TAMath ops
MIN - 最小值
TA.MIN() - 指定周期内的最小值
语法
python
result = TA.MIN(records, period)参数
| 参数 | 类型 | 说明 |
|---|---|---|
| records | array | K线数据数组 |
| period | int | 周期长度 |
返回值
返回一个数组,每个元素是对应周期内的最小收盘价。
计算方法
对于每个位置i,返回从 i-period+1 到 i 这段时间内的最小值。
使用场景
- 支撑位识别:找出近期最低价
- 突破检测:判断是否创新低
- 止损设置:基于最低价设置止损
基础示例
python
def onTick():
exchange.SetContractType("swap")
records = exchange.GetRecords()
if len(records) < 30:
return
# 计算20周期最低价
min_20 = TA.MIN(records, 20)
current_price = records[-1].Close
Log("20日最低价:", min_20[-1])
# 跌破20日新低
if current_price < min_20[-2]:
Log("跌破20日新低!")
exchange.SetDirection("sell")
exchange.Sell(-1, 1)高级应用
1. 支撑位买入
python
def onTick():
records = exchange.GetRecords()
# 20日最低价作为支撑
min_20 = TA.MIN(records, 20)
support = min_20[-1]
current_price = records[-1].Close
# 价格接近支撑位(误差2%内)
if abs(current_price - support) / support < 0.02:
# RSI确认超卖
rsi = TA.RSI(records, 14)
if rsi[-1] < 35:
Log("接近支撑位且超卖,买入")
exchange.SetDirection("buy")
exchange.Buy(-1, 1)2. 移动止损(空单)
python
# 全局变量
entry_price = None
lowest_price = None
def onTick():
global entry_price, lowest_price
records = exchange.GetRecords()
current_price = records[-1].Close
position = exchange.GetPosition()
if len(position) > 0 and position[0].Type == 1: # 持有空单
# 更新最低价
if lowest_price is None:
lowest_price = current_price
else:
lowest_price = min(lowest_price, current_price)
# 从最低价反弹5%止损
rebound = (current_price - lowest_price) / lowest_price
if rebound > 0.05:
Log(f"从最低价{lowest_price}反弹{rebound*100:.2f}%,止损")
exchange.SetDirection("closesell")
exchange.Buy(-1, position[0].Amount)
entry_price = None
lowest_price = None3. 新低检测策略
python
def onTick():
records = exchange.GetRecords()
# 不同周期的最低价
min_20 = TA.MIN(records, 20)
min_60 = TA.MIN(records, 60)
min_120 = TA.MIN(records, 120)
current_price = records[-1].Close
# 同时创多个周期新低
new_low_count = 0
if current_price <= min_20[-1]:
new_low_count += 1
Log("创20日新低")
if current_price <= min_60[-1]:
new_low_count += 1
Log("创60日新低")
if current_price <= min_120[-1]:
new_low_count += 1
Log("创120日新低")
if new_low_count >= 2:
Log(f"同时创{new_low_count}个周期新低,弱势破位")
# 如果持有多单,止损
position = exchange.GetPosition()
if len(position) > 0 and position[0].Type == 0:
exchange.SetDirection("closebuy")
exchange.Sell(-1, position[0].Amount)4. 区间振荡策略
python
def onTick():
records = exchange.GetRecords()
period = 30
# 区间上下沿
max_val = TA.MAX(records, period)
min_val = TA.MIN(records, period)
# 区间中线
mid_val = (max_val[-1] + min_val[-1]) / 2
current_price = records[-1].Close
# 价格位置
range_width = max_val[-1] - min_val[-1]
if current_price < min_val[-1] + range_width * 0.2:
Log("接近下沿,买入")
exchange.SetDirection("buy")
exchange.Buy(-1, 1)
elif current_price > max_val[-1] - range_width * 0.2:
Log("接近上沿,卖出")
exchange.SetDirection("sell")
exchange.Sell(-1, 1)注意事项
- 前期数据:前period-1个数据点无法计算完整周期
- 实时更新:最新K线未完成时,最小值可能变化
- 极端情况:单根K线暴跌可能大幅拉低最小值