在 GitHub 上查看
CCI Expert 策略
概述
本策略是将 MetaTrader 的 “CCI-Expert” 专家顾问移植到 StockSharp 平台后的版本。策略只使用一个时间框架上的商品通道指数(CCI)指标,并严格等待三根已完成的 K 线后再做出开仓或平仓决定。
交易逻辑
- 订阅所选的蜡烛序列,并按设定周期计算 CCI。
- 检查最近三根已经结束的 CCI 数值:
- 做多条件:当前值和上一根值均高于
+1,而倒数第三根值低于 +1。
- 做空条件:当前值和上一根值均低于
+1,而倒数第三根值高于 +1。
- 当没有持仓且点差过滤条件满足时,只开一笔市价仓位。
- 只有在出现反向信号且该仓位已经处于盈利状态(当前收盘价优于进场价)时才会平仓。
风险控制
- 可以固定下单手数,也可以在
FixedVolume 为零时根据风险百分比与止损距离自动计算手数。
- 通过
StartProtection 自动设置以“点”为单位的止损和止盈挂单。
MaxSpreadPoints 参数提供可选的点差过滤器,当前买卖价差超过阈值时暂停交易。
参数
| 参数 |
说明 |
默认值 |
FixedVolume |
固定手数,设置为零时启用风险计算模式。 |
0.1 |
RiskPercent |
当 FixedVolume 为零时,用于计算下单量的账户风险百分比。 |
0 |
TakeProfitPoints |
止盈距离(点)。 |
150 |
StopLossPoints |
止损距离(点)。 |
600 |
MaxSpreadPoints |
允许的最大点差(点),为零表示关闭过滤。 |
30 |
CciPeriod |
CCI 指标周期。 |
14 |
CandleType |
策略使用的蜡烛时间框架。 |
15 分钟 |
说明
- 与原始 EA 相同,CCI 的阈值固定在
+1 和 -1,因此只有在形成明确的三步结构后才会触发交易。
- 风险百分比模式需要交易所提供的
PriceStep、StepPrice、VolumeStep 等元数据才能正确换算点数和资金风险。
- 策略会在图表上绘制蜡烛、CCI 指标线以及自身成交,方便回测和实时监控。
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// CCI Expert strategy: CCI crossover with level-based signals.
/// Buys when CCI crosses above +1 from below, sells when crosses below -1 from above.
/// </summary>
public class CciExpertStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cciPeriod;
private decimal? _prevCci;
private decimal? _prevPrevCci;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public CciExpertStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_cciPeriod = Param(nameof(CciPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "CCI period", "Indicators");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevCci = null;
_prevPrevCci = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevCci = null;
_prevPrevCci = null;
var cci = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(cci, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal cciValue)
{
if (candle.State != CandleStates.Finished) return;
if (_prevCci is decimal prev && _prevPrevCci is decimal prev2)
{
// Long: CCI stayed above +100 for 2 bars while prior bar was below
var longSignal = cciValue > 100m && prev > 100m && prev2 < 100m;
// Short: CCI stayed below -100 for 2 bars while prior bar was above
var shortSignal = cciValue < -100m && prev < -100m && prev2 > -100m;
if (longSignal && Position <= 0)
BuyMarket();
else if (shortSignal && Position >= 0)
SellMarket();
}
_prevPrevCci = _prevCci;
_prevCci = cciValue;
}
}
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 CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
class cci_expert_strategy(Strategy):
"""
CCI Expert strategy: CCI crossover with level-based signals.
Buys when CCI stays above +100 for 2 bars, sells when below -100 for 2 bars.
"""
def __init__(self):
super(cci_expert_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._cci_period = self.Param("CciPeriod", 14) \
.SetDisplay("CCI Period", "CCI period", "Indicators")
self._prev_cci = None
self._prev_prev_cci = None
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(cci_expert_strategy, self).OnReseted()
self._prev_cci = None
self._prev_prev_cci = None
def OnStarted2(self, time):
super(cci_expert_strategy, self).OnStarted2(time)
self._prev_cci = None
self._prev_prev_cci = None
cci = CommodityChannelIndex()
cci.Length = self._cci_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(cci, self.on_process).Start()
def on_process(self, candle, cci_value):
if candle.State != CandleStates.Finished:
return
if self._prev_cci is not None and self._prev_prev_cci is not None:
long_signal = cci_value > 100 and self._prev_cci > 100 and self._prev_prev_cci < 100
short_signal = cci_value < -100 and self._prev_cci < -100 and self._prev_prev_cci > -100
if long_signal and self.Position <= 0:
self.BuyMarket()
elif short_signal and self.Position >= 0:
self.SellMarket()
self._prev_prev_cci = self._prev_cci
self._prev_cci = cci_value
def CreateClone(self):
return cci_expert_strategy()