ADX CCI MA
该策略结合 ADX、CCI 和可配置移动平均线以交易强势趋势。
当 +DI 上穿 -DI 且 CCI > 100、ADX 高于阈值时做多(若启用 MA 过滤器则收盘价需高于 MA);当 -DI 上穿 +DI 且 CCI < -100、ADX 高于阈值时做空(收盘价低于 MA)。
包含百分比止损和止盈,可选 MA 风险管理:连续多根 K 线与 MA 方向相反时平仓。
细节
- 入场条件:+DI/-DI 交叉并伴随 CCI 极值且 ADX >
AdxThreshold,可选与 MA 的位置。 - 多空方向:双向。
- 出场条件:触发止损或止盈,可选 MA 风险管理。
- 止损:是,止盈和止损。
- 默认值:
EnableLong= trueEnableShort= trueTakeProfitPercent= 2mStopLossPercent= 1mCciPeriod= 15AdxLength= 10AdxThreshold= 20mUseMaTrend= trueMaType= MovingAverageTypeEnum.SimpleMaLength= 200UseMaRiskManagement= falseMaRiskExitCandles= 2CandleType= TimeSpan.FromMinutes(5)
- 过滤器:
- 类别:趋势
- 方向:双向
- 指标:ADX, CCI, MA
- 止损:是
- 复杂度:中
- 时间框架:日内 (5m)
- 季节性:否
- 神经网络:否
- 背离:否
- 风险等级:中
namespace StockSharp.Samples.Strategies;
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
/// <summary>
/// Strategy combining ADX, CCI and moving average trend filter.
/// Enters when +DI crosses -DI and CCI confirms extreme values.
/// </summary>
public class AdxCciMaStrategy : Strategy
{
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<int> _adxLength;
private readonly StrategyParam<decimal> _adxThreshold;
private readonly StrategyParam<int> _maLength;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cooldownBars;
private decimal _prevPlusDi;
private decimal _prevMinusDi;
private int _cooldownRemaining;
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public int AdxLength { get => _adxLength.Value; set => _adxLength.Value = value; }
public decimal AdxThreshold { get => _adxThreshold.Value; set => _adxThreshold.Value = value; }
public int MaLength { get => _maLength.Value; set => _maLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }
public AdxCciMaStrategy()
{
_cciPeriod = Param(nameof(CciPeriod), 15)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "Period for CCI", "Indicators");
_adxLength = Param(nameof(AdxLength), 10)
.SetGreaterThanZero()
.SetDisplay("ADX Length", "Length for ADX", "Indicators");
_adxThreshold = Param(nameof(AdxThreshold), 20m)
.SetDisplay("ADX Threshold", "ADX level to confirm trend", "Indicators");
_maLength = Param(nameof(MaLength), 50)
.SetGreaterThanZero()
.SetDisplay("MA Length", "Length of moving average", "MA Trend");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for candles", "General");
_cooldownBars = Param(nameof(CooldownBars), 10)
.SetDisplay("Cooldown Bars", "Bars between trades", "Risk");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevPlusDi = 0;
_prevMinusDi = 0;
_cooldownRemaining = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var cci = new CommodityChannelIndex { Length = CciPeriod };
var adx = new AverageDirectionalIndex { Length = AdxLength };
var ma = new SimpleMovingAverage { Length = MaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(adx, cci, ma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue adxValue, IIndicatorValue cciValue, IIndicatorValue maValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var adxTyped = (IAverageDirectionalIndexValue)adxValue;
if (adxTyped.MovingAverage is not decimal adx)
return;
var dx = adxTyped.Dx;
if (dx.Plus is not decimal plusDi || dx.Minus is not decimal minusDi)
return;
var cci = cciValue.ToDecimal();
var ma = maValue.ToDecimal();
if (_prevPlusDi == 0)
{
_prevPlusDi = plusDi;
_prevMinusDi = minusDi;
return;
}
if (_cooldownRemaining > 0)
{
_cooldownRemaining--;
_prevPlusDi = plusDi;
_prevMinusDi = minusDi;
return;
}
var longSignal = plusDi > minusDi && _prevPlusDi <= _prevMinusDi;
var shortSignal = minusDi > plusDi && _prevMinusDi <= _prevPlusDi;
if (longSignal && cci > 0m && adx >= AdxThreshold && candle.ClosePrice > ma && Position <= 0)
{
if (Position < 0)
BuyMarket(Math.Abs(Position));
BuyMarket(Volume);
_cooldownRemaining = CooldownBars;
}
else if (shortSignal && cci < 0m && adx >= AdxThreshold && candle.ClosePrice < ma && Position >= 0)
{
if (Position > 0)
SellMarket(Math.Abs(Position));
SellMarket(Volume);
_cooldownRemaining = CooldownBars;
}
_prevPlusDi = plusDi;
_prevMinusDi = minusDi;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import CommodityChannelIndex, AverageDirectionalIndex, SimpleMovingAverage, IndicatorHelper
from StockSharp.Algo.Strategies import Strategy
class adx_cci_ma_strategy(Strategy):
def __init__(self):
super(adx_cci_ma_strategy, self).__init__()
self._cci_period = self.Param("CciPeriod", 15) \
.SetGreaterThanZero() \
.SetDisplay("CCI Period", "Period for CCI", "Indicators")
self._adx_length = self.Param("AdxLength", 10) \
.SetGreaterThanZero() \
.SetDisplay("ADX Length", "Length for ADX", "Indicators")
self._adx_threshold = self.Param("AdxThreshold", 20.0) \
.SetDisplay("ADX Threshold", "ADX level to confirm trend", "Indicators")
self._ma_length = self.Param("MaLength", 50) \
.SetGreaterThanZero() \
.SetDisplay("MA Length", "Length of moving average", "MA Trend")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30))) \
.SetDisplay("Candle Type", "Timeframe for candles", "General")
self._cooldown_bars = self.Param("CooldownBars", 10) \
.SetDisplay("Cooldown Bars", "Bars between trades", "Risk")
self._prev_plus_di = 0.0
self._prev_minus_di = 0.0
self._cooldown_remaining = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(adx_cci_ma_strategy, self).OnReseted()
self._prev_plus_di = 0.0
self._prev_minus_di = 0.0
self._cooldown_remaining = 0
def OnStarted2(self, time):
super(adx_cci_ma_strategy, self).OnStarted2(time)
cci = CommodityChannelIndex()
cci.Length = int(self._cci_period.Value)
adx = AverageDirectionalIndex()
adx.Length = int(self._adx_length.Value)
ma = SimpleMovingAverage()
ma.Length = int(self._ma_length.Value)
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(adx, cci, ma, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ma)
self.DrawOwnTrades(area)
def _on_process(self, candle, adx_value, cci_value, ma_value):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
adx_ma = adx_value.MovingAverage
if adx_ma is None:
return
adx_v = float(adx_ma)
dx = adx_value.Dx
plus_di_val = dx.Plus
minus_di_val = dx.Minus
if plus_di_val is None or minus_di_val is None:
return
plus_di = float(plus_di_val)
minus_di = float(minus_di_val)
cci_v = float(IndicatorHelper.ToDecimal(cci_value))
ma_v = float(IndicatorHelper.ToDecimal(ma_value))
close = float(candle.ClosePrice)
if self._prev_plus_di == 0:
self._prev_plus_di = plus_di
self._prev_minus_di = minus_di
return
if self._cooldown_remaining > 0:
self._cooldown_remaining -= 1
self._prev_plus_di = plus_di
self._prev_minus_di = minus_di
return
threshold = float(self._adx_threshold.Value)
cooldown = int(self._cooldown_bars.Value)
long_signal = plus_di > minus_di and self._prev_plus_di <= self._prev_minus_di
short_signal = minus_di > plus_di and self._prev_minus_di <= self._prev_plus_di
if long_signal and cci_v > 0 and adx_v >= threshold and close > ma_v and self.Position <= 0:
if self.Position < 0:
self.BuyMarket(Math.Abs(self.Position))
self.BuyMarket(self.Volume)
self._cooldown_remaining = cooldown
elif short_signal and cci_v < 0 and adx_v >= threshold and close < ma_v and self.Position >= 0:
if self.Position > 0:
self.SellMarket(Math.Abs(self.Position))
self.SellMarket(self.Volume)
self._cooldown_remaining = cooldown
self._prev_plus_di = plus_di
self._prev_minus_di = minus_di
def CreateClone(self):
return adx_cci_ma_strategy()