ATR Stop
定义
GetAtrStop( lookback_periods=21, multiplier=3, end_type=EndType.CLOSE)
描述
ATR Stop,也称为 ATR 通道止损 或 波动性止损,是一种基于平均真实波幅(Average True Range, ATR) 的动态止损和趋势跟踪
参数
| 参数名 |
类型 |
描述 |
| lookback_periods |
Int |
ATR 评估的周期数 ()。必须大于 1 |
| multiplier |
Int |
乘数设置 ATR 频段宽度。必须大于 0,通常设置为 2 到 3 左右 |
返回值
| 返回值 |
类型 |
描述 |
| Date |
Time |
日期 |
| AtrStop |
decimal? |
ATR 追踪止损线包含上行和下行 |
| BuyStop |
decimal? |
上行止损线 |
| SellStop |
decimal? |
下行止损线 |
public override void OnData(Slice slice)
{
///指标对象
QuoteCurrentDay(slice, 1, 1, (dic) =>
{
if (dic.Count>0)
{
foreach (var item in dic.Keys)
{
///获取指标结果
var resp=dic[item].GetAtrStop(2);
Console.WriteLine( resp.ToJson());
}
}
});
}
# ATR指标
self.atr = self.ATR(self.symbol, 22, MovingAverageType.Simple, Resolution.Daily)
# 追踪最高价和最低价
self.highest_high = None
self.lowest_low = None
self.lookback_period = 22 # 与ATR周期一致
# ATR乘数
self.atr_multiplier = 3.0
if not data.ContainsKey(self.symbol) or not self.atr.IsReady:
return
current_price = data[self.symbol].Price
atr_value = self.atr.Current.Value
# 初始化最高价和最低价
if self.highest_high is None:
self.highest_high = current_price
self.lowest_low = current_price
# 更新最高价和最低价
self.highest_high = max(self.highest_high, current_price)
self.lowest_low = min(self.lowest_low, current_price)
# 计算ATR Stop
self.long_stop = self.highest_high - (atr_value * self.atr_multiplier)
self.short_stop = self.lowest_low + (atr_value * self.atr_multiplier)# ATR指标
self.atr = self.ATR(self.symbol, 22, MovingAverageType.Simple, Resolution.Daily)
# 追踪最高价和最低价
self.highest_high = None
self.lowest_low = None
self.lookback_period = 22 # 与ATR周期一致
# ATR乘数
self.atr_multiplier = 3.0
if not data.ContainsKey(self.symbol) or not self.atr.IsReady:
return
current_price = data[self.symbol].Price
atr_value = self.atr.Current.Value
# 初始化最高价和最低价
if self.highest_high is None:
self.highest_high = current_price
self.lowest_low = current_price
# 更新最高价和最低价
self.highest_high = max(self.highest_high, current_price)
self.lowest_low = min(self.lowest_low, current_price)
# 计算ATR Stop
self.long_stop = self.highest_high - (atr_value * self.atr_multiplier)
self.short_stop = self.lowest_low + (atr_value * self.atr_multiplier)