在 GitHub 上查看
Exp ColorX2MA X2 策略
该策略将 MT5 专家 "Exp_ColorX2MA_X2" 移植到 StockSharp。它在高周期上使用 ColorX2MA 判断趋势,在低周期上使用 ColorX2MA 生成进出场信号。每个 ColorX2MA 都由两层可配置均线级联组成,并根据最终曲线的斜率自动着色。当低周期的颜色在高周期趋势方向上发生转换时触发交易。
实现保留了原始指标的所有价格类型,同时支持最常用的平滑算法(SMA、EMA、SMMA、LWMA、Jurik)。如果 Jurik 指标公开 Phase 属性,会使用配置的阶段值进行设置。
交易规则
- 做多入场
- 高周期 ColorX2MA 颜色为多头(trend direction > 0)。
- 低周期 ColorX2MA 在上一根柱子为多头颜色,本柱收盘变为中性或空头颜色(
Clr[1] == 1 且 Clr[0] != 1)。
- 允许做多。
- 做空入场
- 高周期 ColorX2MA 颜色为空头(trend direction < 0)。
- 低周期 ColorX2MA 在上一根柱子为空头颜色,本柱收盘变为中性或多头颜色(
Clr[1] == 2 且 Clr[0] != 2)。
- 允许做空。
- 平多
- 出现低周期空头颜色(
Clr[1] == 2)并且开启了二级多头平仓,或高周期趋势翻为空头且启用了一级多头平仓。
- 平空
- 出现低周期多头颜色(
Clr[1] == 1)并且开启了二级空头平仓,或高周期趋势翻为多头且启用了一级空头平仓。
- 止损/止盈
- 可选的止损与止盈以点数表示(乘以合约的价格步长),在每根完成的信号周期 K 线上根据极值与持仓均价进行检查。
默认参数
- 趋势周期:6 小时 K 线。
- 信号周期:30 分钟 K 线。
- 趋势平滑:SMA(12) → Jurik(5, Phase 15)。
- 信号平滑:SMA(12) → Jurik(5, Phase 15)。
- 应用价格:收盘价。
- 信号偏移:两个周期均为 1 根柱子。
- 权限:允许多空方向的开仓和平仓。
- 止损:1000 点(根据价格步长换算)。
- 止盈:2000 点(根据价格步长换算)。
筛选与说明
- 方向:多空双向,可通过权限开关控制。
- 周期:双周期结构(趋势周期 + 信号周期)。
- 指标:双层 ColorX2MA,可自定义两级平滑方式。
- 平滑支持:
Sma、Ema、Smma、Lwma、Jurik,其他原库算法暂未实现。
- 价格类型:完整支持 12 种公式,包括 TrendFollow 与 Demark 价格。
- 止损:可选的固定距离止损/止盈。
- 复杂度:中等,需要同步两个时间框架和颜色缓冲。
- 适用市场:外汇、指数、加密等需要 ColorX2MA 趋势过滤的品种。
使用提示
- 建议保持高周期明显大于信号周期,以降低震荡噪声。
- 通过调整
SignalSignalBar 控制信号灵敏度:数值越小越敏感,越大越平滑。
- 如果品种没有提供
PriceStep,止损和止盈距离将按价格单位直接解释。
- Jurik 平滑需要 StockSharp 的相关指标许可,若不可用可改用其他平滑方式。
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>
/// ColorX2MA X2 strategy (simplified). Uses dual EMA smoothing to detect trend color transitions.
/// Buys when the smoothed MA turns up, sells when it turns down.
/// </summary>
public class ExpColorX2MaX2Strategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int FastLength
{
get => _fastLength.Value;
set => _fastLength.Value = value;
}
public int SlowLength
{
get => _slowLength.Value;
set => _slowLength.Value = value;
}
public ExpColorX2MaX2Strategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Candles", "General");
_fastLength = Param(nameof(FastLength), 12)
.SetGreaterThanZero()
.SetDisplay("Fast Length", "Fast EMA period", "Indicators");
_slowLength = Param(nameof(SlowLength), 26)
.SetGreaterThanZero()
.SetDisplay("Slow Length", "Slow EMA period", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fastEma = new ExponentialMovingAverage { Length = FastLength };
var slowEma = new ExponentialMovingAverage { Length = SlowLength };
decimal prevFast = 0, prevSlow = 0;
var hasPrev = false;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(fastEma, slowEma, (ICandleMessage candle, decimal fastValue, decimal slowValue) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!hasPrev)
{
prevFast = fastValue;
prevSlow = slowValue;
hasPrev = true;
return;
}
if (!IsFormedAndOnlineAndAllowTrading())
{
prevFast = fastValue;
prevSlow = slowValue;
return;
}
// Fast crosses above slow
if (prevFast <= prevSlow && fastValue > slowValue && Position <= 0)
BuyMarket();
// Fast crosses below slow
else if (prevFast >= prevSlow && fastValue < slowValue && Position >= 0)
SellMarket();
prevFast = fastValue;
prevSlow = slowValue;
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fastEma);
DrawIndicator(area, slowEma);
DrawOwnTrades(area);
}
}
}
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class exp_color_x2_ma_x2_strategy(Strategy):
def __init__(self):
super(exp_color_x2_ma_x2_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Candles", "General")
self._fast_length = self.Param("FastLength", 12) \
.SetDisplay("Fast Length", "Fast EMA period", "Indicators")
self._slow_length = self.Param("SlowLength", 26) \
.SetDisplay("Slow Length", "Slow EMA period", "Indicators")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
@property
def CandleType(self):
return self._candle_type.Value
@property
def FastLength(self):
return self._fast_length.Value
@property
def SlowLength(self):
return self._slow_length.Value
def OnReseted(self):
super(exp_color_x2_ma_x2_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(exp_color_x2_ma_x2_strategy, self).OnStarted2(time)
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
fast_ema = ExponentialMovingAverage()
fast_ema.Length = self.FastLength
slow_ema = ExponentialMovingAverage()
slow_ema.Length = self.SlowLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(fast_ema, slow_ema, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, fast_ema)
self.DrawIndicator(area, slow_ema)
self.DrawOwnTrades(area)
def _on_process(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fv = float(fast_value)
sv = float(slow_value)
if not self._has_prev:
self._prev_fast = fv
self._prev_slow = sv
self._has_prev = True
return
if self._prev_fast <= self._prev_slow and fv > sv and self.Position <= 0:
self.BuyMarket()
elif self._prev_fast >= self._prev_slow and fv < sv and self.Position >= 0:
self.SellMarket()
self._prev_fast = fv
self._prev_slow = sv
def CreateClone(self):
return exp_color_x2_ma_x2_strategy()