ATR 疲劳策略
平均真实波幅(ATR)的突然飙升意味着波动扩大,但往往很快衰减。本策略寻找 ATR 高于其均线一定倍数的情况,配合反转蜡烛捕捉随后的回落。
测试表明年均收益约为 139%,该策略在股票市场表现最佳。
每根K线更新 ATR 及其平均值。如果 ATR 超过均值乘以设定倍数且蜡烛收盘方向与前一波相反,则开仓。止损为按价格百分比计算的跟踪止损,随着波动爆发消退而跟随仓位移动。
仓位通常依靠止损退出,目标是利用波动爆发后迅速回调的机会。
细节
- 入场条件:ATR 高于均线且出现反转蜡烛。
- 多/空:双向。
- 退出条件:止损。
- 止损:有,百分比跟踪止损。
- 默认值:
AtrPeriod= 14AtrAvgPeriod= 20AtrMultiplier= 1.5MaPeriod= 20StopLossPercent= 2CandleType= 5 分钟
- 过滤条件:
- 类别: 反转
- 方向: 双向
- 指标: ATR, MA
- 止损: 有
- 复杂度: 中等
- 时间框架: 日内
- 季节性: 无
- 神经网络: 无
- 背离: 无
- 风险级别: 中等
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// ATR Exhaustion strategy.
/// An ATR spike is an ATR above AtrMultiplier times its own AtrAvgPeriod moving average. While flat, a bullish candle on a spike
/// buys when the price moving average has been falling and a bearish one sells when it has been rising.
/// The only exit is a trailing percent stop.
/// </summary>
public class AtrExhaustionStrategy : Strategy
{
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<int> _atrAvgPeriod;
private readonly StrategyParam<decimal> _atrMultiplier;
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
private SimpleMovingAverage _atrAverage;
private decimal? _prevMa;
/// <summary>
/// ATR period.
/// </summary>
public int AtrPeriod
{
get => _atrPeriod.Value;
set => _atrPeriod.Value = value;
}
/// <summary>
/// Period of the moving average of ATR.
/// </summary>
public int AtrAvgPeriod
{
get => _atrAvgPeriod.Value;
set => _atrAvgPeriod.Value = value;
}
/// <summary>
/// How many times its average ATR must exceed to count as a spike.
/// </summary>
public decimal AtrMultiplier
{
get => _atrMultiplier.Value;
set => _atrMultiplier.Value = value;
}
/// <summary>
/// Period of the price moving average that defines the prior move.
/// </summary>
public int MaPeriod
{
get => _maPeriod.Value;
set => _maPeriod.Value = value;
}
/// <summary>
/// Trailing stop-loss percentage.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public AtrExhaustionStrategy()
{
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Period", "Period for ATR", "Indicators");
_atrAvgPeriod = Param(nameof(AtrAvgPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("ATR Average Period", "Period of the moving average of ATR", "Indicators");
_atrMultiplier = Param(nameof(AtrMultiplier), 1.5m)
.SetGreaterThanZero()
.SetDisplay("ATR Multiplier", "How many times its average ATR must exceed", "Indicators");
_maPeriod = Param(nameof(MaPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("MA Period", "Price moving average that defines the prior move", "Indicators");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetNotNegative()
.SetDisplay("Stop Loss %", "Trailing stop loss percentage", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_atrAverage = null;
_prevMa = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_atrAverage = new SimpleMovingAverage { Length = AtrAvgPeriod };
_prevMa = null;
var sma = new SimpleMovingAverage { Length = MaPeriod };
var atr = new AverageTrueRange { Length = AtrPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(sma, atr, ProcessCandle)
.Start();
StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), isStopTrailing: true, useMarketOrders: true, isLocalStop: true);
// The stop has to see prices between candles, not only at their close.
foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
{
var quotes = new Subscription(DataType.Level1, Security);
quotes.MarketData.BuildField = field;
SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
}
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sma);
DrawIndicator(area, atr);
DrawOwnTrades(area);
}
}
private void ObserveProtectionQuote(Level1ChangeMessage quote)
{
// The high-level handler activates native protection before this callback, also between signal bars.
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue smaValue, IIndicatorValue atrValue)
{
if (candle.State != CandleStates.Finished || !atrValue.IsFormed)
return;
var atr = atrValue.GetValue<decimal>();
var atrAverage = _atrAverage.Process(atr, candle.OpenTime, true);
if (!smaValue.IsFormed)
return;
var ma = smaValue.GetValue<decimal>();
var prevMa = _prevMa;
_prevMa = ma;
if (!_atrAverage.IsFormed || prevMa is not decimal lastMa || Position != 0 || !IsFormedAndOnlineAndAllowTrading())
return;
if (atr <= atrAverage.GetValue<decimal>() * AtrMultiplier)
return;
var close = candle.ClosePrice;
if (close > candle.OpenPrice && ma < lastMa)
BuyMarket(Volume);
else if (close < candle.OpenPrice && ma > lastMa)
SellMarket(Volume);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
clr.AddReference("StockSharp.BusinessEntities")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes, Level1Fields
from StockSharp.BusinessEntities import Subscription
from StockSharp.Algo.Indicators import SimpleMovingAverage, AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
from indicator_extensions import *
class atr_exhaustion_strategy(Strategy):
"""
ATR Exhaustion strategy.
An ATR spike is an ATR above AtrMultiplier times its own AtrAvgPeriod moving average. While flat, a bullish candle on a spike
buys when the price moving average has been falling and a bearish one sells when it has been rising.
The only exit is a trailing percent stop.
"""
def __init__(self):
super(atr_exhaustion_strategy, self).__init__()
self._atr_period = self.Param("AtrPeriod", 14).SetGreaterThanZero().SetDisplay("ATR Period", "Period for ATR", "Indicators")
self._atr_avg_period = self.Param("AtrAvgPeriod", 20).SetGreaterThanZero().SetDisplay("ATR Average Period", "Period of the moving average of ATR", "Indicators")
self._atr_multiplier = self.Param("AtrMultiplier", 1.5).SetGreaterThanZero().SetDisplay("ATR Multiplier", "How many times its average ATR must exceed", "Indicators")
self._ma_period = self.Param("MaPeriod", 20).SetGreaterThanZero().SetDisplay("MA Period", "Price moving average that defines the prior move", "Indicators")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0).SetNotNegative().SetDisplay("Stop Loss %", "Trailing stop loss percentage", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._atr_average = None
self._prev_ma = None
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(atr_exhaustion_strategy, self).OnReseted()
self._atr_average = None
self._prev_ma = None
def OnStarted2(self, time):
super(atr_exhaustion_strategy, self).OnStarted2(time)
self._atr_average = SimpleMovingAverage()
self._atr_average.Length = self._atr_avg_period.Value
self._prev_ma = None
sma = SimpleMovingAverage()
sma.Length = self._ma_period.Value
atr = AverageTrueRange()
atr.Length = self._atr_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(sma, atr, self._process_candle).Start()
self.StartProtection(Unit(), Unit(Decimal(self._stop_loss_percent.Value), UnitTypes.Percent), isStopTrailing=True, useMarketOrders=True, isLocalStop=True)
# The stop has to see prices between candles, not only at their close.
for field in (Level1Fields.BestBidPrice, Level1Fields.BestAskPrice):
quotes = Subscription(DataType.Level1, self.Security)
quotes.MarketData.BuildField = field
self.SubscribeLevel1(quotes).Bind(self._observe_protection_quote).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawIndicator(area, atr)
self.DrawOwnTrades(area)
def _observe_protection_quote(self, quote):
# The high-level handler activates native protection before the callback, including between bars.
pass
def _process_candle(self, candle, sma_value, atr_value):
if candle.State != CandleStates.Finished or not atr_value.IsFormed:
return
atr = atr_value.GetValue[Decimal](None)
atr_average = process_value(self._atr_average, atr, candle.OpenTime, True)
if not sma_value.IsFormed:
return
ma = sma_value.GetValue[Decimal](None)
prev_ma = self._prev_ma
self._prev_ma = ma
if not self._atr_average.IsFormed or prev_ma is None or self.Position != 0 or not self.IsFormedAndOnlineAndAllowTrading():
return
if atr <= to_decimal(atr_average) * Decimal(self._atr_multiplier.Value):
return
close = candle.ClosePrice
if close > candle.OpenPrice and ma < prev_ma:
self.BuyMarket(self.Volume)
elif close < candle.OpenPrice and ma > prev_ma:
self.SellMarket(self.Volume)
def CreateClone(self):
return atr_exhaustion_strategy()