在 GitHub 上查看
ColorMaRsi Trigger MMRec Duplex 策略
概述
该策略将 MetaTrader 专家顾问 Exp_ColorMaRsi-Trigger_MMRec_Duplex.mq5 迁移到 StockSharp 高级 API。系统包含两个独立的
MaRsi-Trigger 模块:一个负责做多信号,另一个负责做空信号。每个模块都会比较快、慢移动平均线以及快、慢 RSI,并将结
果压缩在 [-1, 1] 区间内:+1 表示多头状态,-1 表示空头状态,0 表示信号不一致。
同时实现了“MMRec”资金管理模块。它会跟踪最近的多空交易结果,一旦在滑动窗口内出现指定数量的亏损,下一次开仓就会采
用缩减的手数,从而复现 MetaTrader 库 TradeAlgorithms.mqh 中的自适应仓位算法。
交易逻辑
指标流程(针对每个模块):
- 根据设定的应用价格和周期计算快慢移动平均 (
MA_fast/MA_slow)。
- 计算快慢 RSI (
RSI_fast/RSI_slow);两者的价格输入可以不同。
- 构建颜色值:初始为
0,若 MA_fast > MA_slow 加 +1,否则减 1;对 RSI 亦然,随后把结果限制在 [-1, 1]。
- 将颜色值写入历史,并使用
SignalBar 指定的偏移读取(与原始 EA 一致)。
多头模块:
- 入场:仅在没有持多仓时触发(若有空头先平仓)。要求前一个颜色值为
+1,当前颜色值小于等于 0,表示多头信号
刚刚转弱。
- 出场:若前一个颜色值变为
-1 且允许平仓,则市价卖出。
空头模块:
- 入场:仅在没有持空仓时触发(若有多头先平仓)。要求前一个颜色值为
-1,当前颜色值大于等于 0,表示空头信号
转弱。
- 出场:若前一个颜色值变为
+1 且允许平仓,则市价买入。
止损/止盈:可选参数,以价格最小变动单位表示,每根完成的 K 线都会检查是否触发,对应的持仓会立即平掉。
资金管理:分别记录多、空方向的交易盈亏。当最近 HistoryDepth 笔交易中的亏损次数达到 LossTrigger 时,下一笔
订单采用缩减手数,否则使用常规手数。
参数
| 组别 |
名称 |
说明 |
默认值 |
| 多头模块 |
LongCandleType |
多头模块使用的 K 线周期。 |
H4 |
|
LongAllowOpen / LongAllowClose |
是否允许开多 / 平多。 |
true |
|
LongStopLossPoints / LongTakeProfitPoints |
止损与止盈的点数(乘以 PriceStep)。填写 0 可关闭。 |
1000 / 2000 |
|
LongSignalBar |
读取指标时向前回看的已完成 K 线数量。 |
1 |
|
LongRsiPeriod / LongRsiLongPeriod |
快、慢 RSI 周期。 |
3 / 13 |
|
LongMaPeriod / LongMaLongPeriod |
快、慢移动平均周期。 |
5 / 10 |
|
LongRsiPrice / LongRsiLongPrice |
快、慢 RSI 使用的应用价格。 |
Weighted / Median |
|
LongMaPrice / LongMaLongPrice |
快、慢移动平均使用的应用价格。 |
Close / Close |
|
LongMaType / LongMaLongType |
移动平均算法(Simple、Exponential、Smoothed、Weighted)。 |
Exponential / Exponential |
| 资金管理 |
LongNormalVolume / LongReducedVolume |
常规与缩减的多头手数。 |
0.1 / 0.01 |
|
LongHistoryDepth |
多头方向参与统计的历史交易数量。 |
5 |
|
LongLossTrigger |
多头方向触发缩减手数所需的亏损次数。 |
3 |
| 组别 |
名称 |
说明 |
默认值 |
| 空头模块 |
ShortCandleType |
空头模块使用的 K 线周期。 |
H4 |
|
ShortAllowOpen / ShortAllowClose |
是否允许开空 / 平空。 |
true |
|
ShortStopLossPoints / ShortTakeProfitPoints |
止损与止盈的点数。 |
1000 / 2000 |
|
ShortSignalBar |
读取指标时的偏移量。 |
1 |
|
ShortRsiPeriod / ShortRsiLongPeriod |
快、慢 RSI 周期。 |
3 / 13 |
|
ShortMaPeriod / ShortMaLongPeriod |
快、慢移动平均周期。 |
5 / 10 |
|
ShortRsiPrice / ShortRsiLongPrice |
快、慢 RSI 使用的应用价格。 |
Weighted / Median |
|
ShortMaPrice / ShortMaLongPrice |
快、慢移动平均使用的应用价格。 |
Close / Close |
|
ShortMaType / ShortMaLongType |
移动平均算法。 |
Exponential / Exponential |
| 资金管理 |
ShortNormalVolume / ShortReducedVolume |
常规与缩减的空头手数。 |
0.1 / 0.01 |
|
ShortHistoryDepth |
空头方向参与统计的历史交易数量。 |
5 |
|
ShortLossTrigger |
空头方向触发缩减手数所需的亏损次数。 |
3 |
说明
- 应用价格选项遵循 MetaTrader 约定,例如
Weighted = (High + Low + 2 * Close) / 4,Typical = (High + Low + Close) / 3。
- 当多空模块使用相同的周期(默认设置)时,策略会复用同一份蜡烛订阅同时驱动两个计算器。
- 将
LossTrigger 设为 0 会立即启用缩减手数,与原始 MMRec 逻辑一致。
- 策略全部使用市价单,因此无需 MetaTrader 中的
Deviation 滑点参数。
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;
public class ColorMaRsiTriggerMmRecDuplexStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _stopLossPoints;
private readonly StrategyParam<int> _takeProfitPoints;
private ExponentialMovingAverage _fast;
private ExponentialMovingAverage _slow;
private decimal _prevFast;
private decimal _prevSlow;
private decimal _entryPrice;
private int _cooldown;
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }
public ColorMaRsiTriggerMmRecDuplexStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
}
protected override void OnReseted()
{
base.OnReseted();
_fast = null; _slow = null;
_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_fast = new ExponentialMovingAverage { Length = FastPeriod };
_slow = new ExponentialMovingAverage { Length = SlowPeriod };
var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
subscription.Bind(_fast, _slow, ProcessCandle);
subscription.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }
var close = candle.ClosePrice;
var step = Security?.PriceStep ?? 1m;
if (Position > 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
}
else if (Position < 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
}
if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }
_prevFast = fastValue; _prevSlow = slowValue;
}
}
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 color_ma_rsi_trigger_mm_rec_duplex_strategy(Strategy):
def __init__(self):
super(color_ma_rsi_trigger_mm_rec_duplex_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 14) \
.SetDisplay("Fast Period", "Fast MA period", "Indicator")
self._slow_period = self.Param("SlowPeriod", 50) \
.SetDisplay("Slow Period", "Slow MA period", "Indicator")
self._stop_loss_points = self.Param("StopLossPoints", 200) \
.SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 400) \
.SetDisplay("Take Profit", "Take-profit in price steps", "Risk")
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def stop_loss_points(self):
return self._stop_loss_points.Value
@property
def take_profit_points(self):
return self._take_profit_points.Value
def OnReseted(self):
super(color_ma_rsi_trigger_mm_rec_duplex_strategy, self).OnReseted()
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(color_ma_rsi_trigger_mm_rec_duplex_strategy, self).OnStarted2(time)
self._fast = ExponentialMovingAverage()
self._fast.Length = self.fast_period
self._slow = ExponentialMovingAverage()
self._slow.Length = self.slow_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(5)))
subscription.Bind(self._fast, self._slow, self._process_candle)
subscription.Start()
def _process_candle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fast_val = float(fast_value)
slow_val = float(slow_value)
if not self._fast.IsFormed or not self._slow.IsFormed:
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_fast = fast_val
self._prev_slow = slow_val
return
close = float(candle.ClosePrice)
step = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 1.0
if self.Position > 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close <= self._entry_price - self.stop_loss_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close >= self._entry_price + self.take_profit_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
elif self.Position < 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close >= self._entry_price + self.stop_loss_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close <= self._entry_price - self.take_profit_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._prev_fast <= self._prev_slow and fast_val > slow_val and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = close
self._cooldown = 100
elif self._prev_fast >= self._prev_slow and fast_val < slow_val and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = close
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
def CreateClone(self):
return color_ma_rsi_trigger_mm_rec_duplex_strategy()