ADX 减弱策略
平均方向指数(ADX)衡量趋势强度。当 ADX 开始下降时,通常说明当前走势动能减弱。本策略在价格位于简单移动平均线另一侧时,逆势交易这种减弱的趋势。
测试表明年均收益约为 136%,该策略在股票市场表现最佳。
每根K线计算 ADX 和 MA。如果 ADX 相比上一值下降且价格在 MA 上方,则入场做多;若 ADX 下跌而价格在 MA 下方,则做空。风险通过固定百分比止损控制。
由于该方法预期的是动能减弱而非完全反转,持仓通常直到 ADX 再次上升或止损触发。
细节
- 入场条件:ADX 低于前一值,且价格相对于 MA 的位置。
- 多/空:双向。
- 退出条件:止损。
- 止损:是,按百分比。
- 默认值:
AdxPeriod= 14MaPeriod= 20StopLoss= 2%CandleType= 15 分钟
- 过滤条件:
- 类别: 趋势跟随
- 方向: 双向
- 指标: ADX, 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>
/// ADX Weakening strategy.
/// Enters when ADX is decreasing (trend weakening) using price vs SMA for direction.
/// ADX weakening + price above SMA = buy (reversal up expected).
/// ADX weakening + price below SMA = sell (reversal down expected).
/// Exits on SMA cross.
/// </summary>
public class AdxWeakeningStrategy : Strategy
{
private readonly StrategyParam<int> _adxPeriod;
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cooldownBars;
private decimal _prevAdx;
private int _cooldown;
/// <summary>
/// ADX period.
/// </summary>
public int AdxPeriod
{
get => _adxPeriod.Value;
set => _adxPeriod.Value = value;
}
/// <summary>
/// MA Period.
/// </summary>
public int MAPeriod
{
get => _maPeriod.Value;
set => _maPeriod.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Cooldown bars.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public AdxWeakeningStrategy()
{
_adxPeriod = Param(nameof(AdxPeriod), 14)
.SetRange(7, 28)
.SetDisplay("ADX Period", "Period for ADX", "Indicators");
_maPeriod = Param(nameof(MAPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("MA Period", "Period for SMA", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_cooldownBars = Param(nameof(CooldownBars), 500)
.SetRange(1, 1000)
.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevAdx = default;
_cooldown = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevAdx = 0;
_cooldown = 0;
var sma = new SimpleMovingAverage { Length = MAPeriod };
var adx = new AverageDirectionalIndex { Length = AdxPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(sma, adx, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sma);
DrawIndicator(area, adx);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue smaIv, IIndicatorValue adxIv)
{
if (candle.State != CandleStates.Finished)
return;
if (!adxIv.IsFormed || !smaIv.IsFormed)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var smaValue = smaIv.ToDecimal();
var adxTyped = (AverageDirectionalIndexValue)adxIv;
if (adxTyped.MovingAverage is not decimal adxValue)
return;
if (_prevAdx == 0)
{
_prevAdx = adxValue;
return;
}
if (_cooldown > 0)
{
_cooldown--;
_prevAdx = adxValue;
return;
}
var isWeakening = adxValue < _prevAdx;
if (Position == 0 && isWeakening && candle.ClosePrice > smaValue)
{
BuyMarket();
_cooldown = CooldownBars;
}
else if (Position == 0 && isWeakening && candle.ClosePrice < smaValue)
{
SellMarket();
_cooldown = CooldownBars;
}
else if (Position > 0 && candle.ClosePrice < smaValue)
{
SellMarket();
_cooldown = CooldownBars;
}
else if (Position < 0 && candle.ClosePrice > smaValue)
{
BuyMarket();
_cooldown = CooldownBars;
}
_prevAdx = adxValue;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import SimpleMovingAverage, AverageDirectionalIndex
from StockSharp.Algo.Strategies import Strategy
class adx_weakening_strategy(Strategy):
"""
ADX Weakening strategy.
Enters when ADX is decreasing (trend weakening) using price vs SMA for direction.
ADX weakening + price above SMA = buy.
ADX weakening + price below SMA = sell.
Exits on SMA cross.
"""
def __init__(self):
super(adx_weakening_strategy, self).__init__()
self._adx_period = self.Param("AdxPeriod", 14).SetDisplay("ADX Period", "Period for ADX", "Indicators")
self._ma_period = self.Param("MAPeriod", 20).SetDisplay("MA Period", "Period for SMA", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._cooldown_bars = self.Param("CooldownBars", 500).SetDisplay("Cooldown Bars", "Bars to wait between trades", "General")
self._prev_adx = 0.0
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(adx_weakening_strategy, self).OnReseted()
self._prev_adx = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(adx_weakening_strategy, self).OnStarted2(time)
self._prev_adx = 0.0
self._cooldown = 0
sma = SimpleMovingAverage()
sma.Length = self._ma_period.Value
adx = AverageDirectionalIndex()
adx.Length = self._adx_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(sma, adx, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawIndicator(area, adx)
self.DrawOwnTrades(area)
def _process_candle(self, candle, sma_iv, adx_iv):
if candle.State != CandleStates.Finished:
return
if not adx_iv.IsFormed or not sma_iv.IsFormed:
return
sma_value = float(sma_iv.Value)
adx_ma = adx_iv.MovingAverage
if adx_ma is None:
return
adx_value = float(adx_ma)
if self._prev_adx == 0:
self._prev_adx = adx_value
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_adx = adx_value
return
is_weakening = adx_value < self._prev_adx
close = float(candle.ClosePrice)
cd = self._cooldown_bars.Value
if self.Position == 0 and is_weakening and close > sma_value:
self.BuyMarket()
self._cooldown = cd
elif self.Position == 0 and is_weakening and close < sma_value:
self.SellMarket()
self._cooldown = cd
elif self.Position > 0 and close < sma_value:
self.SellMarket()
self._cooldown = cd
elif self.Position < 0 and close > sma_value:
self.BuyMarket()
self._cooldown = cd
self._prev_adx = adx_value
def CreateClone(self):
return adx_weakening_strategy()