CMO Duplex 策略
该策略是 MetaTrader 5 专家顾问 Exp_CMO_Duplex.mq5 在 StockSharp 平台上的移植版本。策略被划分为彼此独立的多头
和空头两条“腿”,它们都基于 Chande 动量振荡指标(CMO)穿越零轴的行为做出决策。每条腿都可以订阅不同的蜡烛
数据、使用独立的指标周期与信号柱偏移,从而在同一标的上构建不对称的配置。
工作原理
- 根据多头与空头腿是否使用同一
DataType,策略会创建一条或两条蜡烛订阅。 - 每条腿拥有自己的 CMO 指标实例,所有计算都只在收盘完成的蜡烛上执行。
SignalBar参数用于指定需要回看多少根已完成的蜡烛来判断交叉:0表示使用最新收盘柱,1表示上一柱,2表示上上一柱,依此类推。- 多头腿: 当所选的 CMO 数值从零上方跌至零或以下时(即穿越零轴向下),在允许开仓的情况下建立或反转为多 头头寸。若较老的指标值位于零下,或达到止损 / 止盈水平,则退出多头。
- 空头腿: 与多头逻辑完全对称。CMO 自零下穿越至零或零上会触发空头开仓,指标值恢复为零上或触发止损 / 止盈 将平仓。
- 当需要反向时,提交的市价单数量等于
Volume + |Position|,因此可以在一次下单动作中关闭旧头寸并开立新方向。 - 启动时调用
StartProtection(),保留 StockSharp 内置的风险保护机制。
参数
| 参数 | 说明 |
|---|---|
LongCandleType |
多头腿使用的蜡烛数据类型。 |
LongCmoPeriod |
多头 CMO 指标的周期。 |
LongSignalBar |
多头信号所使用的历史偏移柱数(0 = 最新收盘柱)。 |
EnableLongEntries |
是否允许开立新的多头头寸。 |
EnableLongExits |
是否允许依据指标信号平掉多头头寸。 |
LongStopLossPoints |
多头止损的距离(以价格步长为单位,0 表示关闭止损)。 |
LongTakeProfitPoints |
多头止盈的距离(以价格步长为单位,0 表示关闭止盈)。 |
ShortCandleType |
空头腿使用的蜡烛数据类型。 |
ShortCmoPeriod |
空头 CMO 指标的周期。 |
ShortSignalBar |
空头信号所使用的历史偏移柱数。 |
EnableShortEntries |
是否允许开立新的空头头寸。 |
EnableShortExits |
是否允许依据指标信号平掉空头头寸。 |
ShortStopLossPoints |
空头止损的距离(以价格步长为单位,0 表示关闭止损)。 |
ShortTakeProfitPoints |
空头止盈的距离(以价格步长为单位,0 表示关闭止盈)。 |
基础的 Strategy.Volume 属性决定默认下单量。当策略需要反向时,会提交一笔数量为 Volume + |Position| 的市价单,以
完成平旧开新的操作。
风险控制
- 每根完成的蜡烛都会检测止损和止盈。多头头寸的止损位于入场价下方,止盈位于入场价上方;空头则反向设置。
- 当价格触及止损或止盈时,策略立即通过市价单平仓;若指标值持续保持错误的符号(多头时 CMO 低于零,空头时高于 零),同样会触发平仓。
- 将距离设置为 0 可以关闭对应的保护,此时头寸完全由振荡器信号管理。
使用提示
- 策略适用于 CMO 在触及零轴附近经常反转的标的,
SignalBar偏移保持了原始专家顾问的延迟特性。 - 多头与空头腿可以共享同一蜡烛序列,也可以运行在不同的时间框架上;若二者
DataType相同,策略会复用单一订阅 以提升效率。 - 由于逻辑完全基于已完成的蜡烛,建议提供连续的蜡烛流(历史回测或实时数据),避免因数据缺失而漏掉信号。
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Two-sided strategy built around the Chande Momentum Oscillator zero-line crossings.
/// Long and short legs can use different candle types, periods and signal offsets.
/// </summary>
public class CmoDuplexStrategy : Strategy
{
private readonly StrategyParam<DataType> _longCandleType;
private readonly StrategyParam<int> _longCmoPeriod;
private readonly StrategyParam<int> _longSignalBar;
private readonly StrategyParam<bool> _enableLongEntries;
private readonly StrategyParam<bool> _enableLongExits;
private readonly StrategyParam<int> _longStopLossPoints;
private readonly StrategyParam<int> _longTakeProfitPoints;
private readonly StrategyParam<DataType> _shortCandleType;
private readonly StrategyParam<int> _shortCmoPeriod;
private readonly StrategyParam<int> _shortSignalBar;
private readonly StrategyParam<bool> _enableShortEntries;
private readonly StrategyParam<bool> _enableShortExits;
private readonly StrategyParam<int> _shortStopLossPoints;
private readonly StrategyParam<int> _shortTakeProfitPoints;
private ChandeMomentumOscillator _longCmo;
private ChandeMomentumOscillator _shortCmo;
private readonly List<decimal> _longValues = new();
private readonly List<decimal> _shortValues = new();
private decimal? _entryPrice;
public DataType LongCandleType
{
get => _longCandleType.Value;
set => _longCandleType.Value = value;
}
public int LongCmoPeriod
{
get => _longCmoPeriod.Value;
set => _longCmoPeriod.Value = value;
}
public int LongSignalBar
{
get => _longSignalBar.Value;
set => _longSignalBar.Value = value;
}
public bool EnableLongEntries
{
get => _enableLongEntries.Value;
set => _enableLongEntries.Value = value;
}
public bool EnableLongExits
{
get => _enableLongExits.Value;
set => _enableLongExits.Value = value;
}
public int LongStopLossPoints
{
get => _longStopLossPoints.Value;
set => _longStopLossPoints.Value = value;
}
public int LongTakeProfitPoints
{
get => _longTakeProfitPoints.Value;
set => _longTakeProfitPoints.Value = value;
}
public DataType ShortCandleType
{
get => _shortCandleType.Value;
set => _shortCandleType.Value = value;
}
public int ShortCmoPeriod
{
get => _shortCmoPeriod.Value;
set => _shortCmoPeriod.Value = value;
}
public int ShortSignalBar
{
get => _shortSignalBar.Value;
set => _shortSignalBar.Value = value;
}
public bool EnableShortEntries
{
get => _enableShortEntries.Value;
set => _enableShortEntries.Value = value;
}
public bool EnableShortExits
{
get => _enableShortExits.Value;
set => _enableShortExits.Value = value;
}
public int ShortStopLossPoints
{
get => _shortStopLossPoints.Value;
set => _shortStopLossPoints.Value = value;
}
public int ShortTakeProfitPoints
{
get => _shortTakeProfitPoints.Value;
set => _shortTakeProfitPoints.Value = value;
}
public CmoDuplexStrategy()
{
_longCandleType = Param(nameof(LongCandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Long Candle Type", "Candle type for the long leg", "Long Leg");
_longCmoPeriod = Param(nameof(LongCmoPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("Long CMO Period", "CMO period for the long leg", "Long Leg");
_longSignalBar = Param(nameof(LongSignalBar), 1)
.SetNotNegative()
.SetDisplay("Long Signal Bar", "Offset in bars for long signals", "Long Leg");
_enableLongEntries = Param(nameof(EnableLongEntries), true)
.SetDisplay("Enable Long Entries", "Allow opening long trades", "Long Leg");
_enableLongExits = Param(nameof(EnableLongExits), true)
.SetDisplay("Enable Long Exits", "Allow closing long trades on signals", "Long Leg");
_longStopLossPoints = Param(nameof(LongStopLossPoints), 1000)
.SetNotNegative()
.SetDisplay("Long Stop Loss", "Stop loss in price steps for longs", "Risk Management");
_longTakeProfitPoints = Param(nameof(LongTakeProfitPoints), 2000)
.SetNotNegative()
.SetDisplay("Long Take Profit", "Take profit in price steps for longs", "Risk Management");
_shortCandleType = Param(nameof(ShortCandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Short Candle Type", "Candle type for the short leg", "Short Leg");
_shortCmoPeriod = Param(nameof(ShortCmoPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("Short CMO Period", "CMO period for the short leg", "Short Leg");
_shortSignalBar = Param(nameof(ShortSignalBar), 1)
.SetNotNegative()
.SetDisplay("Short Signal Bar", "Offset in bars for short signals", "Short Leg");
_enableShortEntries = Param(nameof(EnableShortEntries), true)
.SetDisplay("Enable Short Entries", "Allow opening short trades", "Short Leg");
_enableShortExits = Param(nameof(EnableShortExits), true)
.SetDisplay("Enable Short Exits", "Allow closing short trades on signals", "Short Leg");
_shortStopLossPoints = Param(nameof(ShortStopLossPoints), 1000)
.SetNotNegative()
.SetDisplay("Short Stop Loss", "Stop loss in price steps for shorts", "Risk Management");
_shortTakeProfitPoints = Param(nameof(ShortTakeProfitPoints), 2000)
.SetNotNegative()
.SetDisplay("Short Take Profit", "Take profit in price steps for shorts", "Risk Management");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, LongCandleType);
if (!Equals(LongCandleType, ShortCandleType))
yield return (Security, ShortCandleType);
}
protected override void OnReseted()
{
base.OnReseted();
_longCmo = null;
_shortCmo = null;
_entryPrice = null;
_longValues.Clear();
_shortValues.Clear();
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_longCmo = new ChandeMomentumOscillator { Length = LongCmoPeriod };
_shortCmo = new ChandeMomentumOscillator { Length = ShortCmoPeriod };
var longSubscription = SubscribeCandles(LongCandleType);
longSubscription.Bind(_longCmo, ProcessLongCandle);
if (Equals(LongCandleType, ShortCandleType))
{
longSubscription.Bind(_shortCmo, ProcessShortCandle).Start();
}
else
{
longSubscription.Start();
var shortSubscription = SubscribeCandles(ShortCandleType);
shortSubscription.Bind(_shortCmo, ProcessShortCandle).Start();
}
// no fixed protection needed
}
private void ProcessLongCandle(ICandleMessage candle, decimal cmoValue)
{
if (candle.State != CandleStates.Finished)
return;
if (_longCmo == null || !_longCmo.IsFormed)
return;
_longValues.Add(cmoValue);
var shift = Math.Max(1, LongSignalBar);
TrimBuffer(_longValues, shift + 3);
if (_longValues.Count < shift + 1)
return;
var currentIndex = _longValues.Count - shift;
var previousIndex = currentIndex - 1;
if (previousIndex < 0)
return;
var current = _longValues[currentIndex];
var previous = _longValues[previousIndex];
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (Position > 0 && _entryPrice is decimal entryPrice)
{
var step = Security?.PriceStep ?? 1m;
var stopPrice = LongStopLossPoints > 0 ? entryPrice - LongStopLossPoints * step : (decimal?)null;
var takePrice = LongTakeProfitPoints > 0 ? entryPrice + LongTakeProfitPoints * step : (decimal?)null;
var exitBySignal = EnableLongExits && previous < 0m;
if ((takePrice.HasValue && candle.HighPrice >= takePrice.Value) ||
(stopPrice.HasValue && candle.LowPrice <= stopPrice.Value) ||
exitBySignal)
{
SellMarket();
_entryPrice = null;
}
}
var crossDown = previous > 0m && current <= 0m;
if (EnableLongEntries && crossDown && Position <= 0)
{
if (true)
{
BuyMarket();
_entryPrice = candle.ClosePrice;
}
}
}
private void ProcessShortCandle(ICandleMessage candle, decimal cmoValue)
{
if (candle.State != CandleStates.Finished)
return;
if (_shortCmo == null || !_shortCmo.IsFormed)
return;
_shortValues.Add(cmoValue);
var shift = Math.Max(1, ShortSignalBar);
TrimBuffer(_shortValues, shift + 3);
if (_shortValues.Count < shift + 1)
return;
var currentIndex = _shortValues.Count - shift;
var previousIndex = currentIndex - 1;
if (previousIndex < 0)
return;
var current = _shortValues[currentIndex];
var previous = _shortValues[previousIndex];
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (Position < 0 && _entryPrice is decimal entryPrice)
{
var step = Security?.PriceStep ?? 1m;
var stopPrice = ShortStopLossPoints > 0 ? entryPrice + ShortStopLossPoints * step : (decimal?)null;
var takePrice = ShortTakeProfitPoints > 0 ? entryPrice - ShortTakeProfitPoints * step : (decimal?)null;
var exitBySignal = EnableShortExits && previous > 0m;
if ((takePrice.HasValue && candle.LowPrice <= takePrice.Value) ||
(stopPrice.HasValue && candle.HighPrice >= stopPrice.Value) ||
exitBySignal)
{
BuyMarket();
_entryPrice = null;
}
}
var crossUp = previous < 0m && current >= 0m;
if (EnableShortEntries && crossUp && Position >= 0)
{
if (true)
{
SellMarket();
_entryPrice = candle.ClosePrice;
}
}
}
private static void TrimBuffer(List<decimal> values, int maxCount)
{
if (values.Count <= maxCount)
return;
values.RemoveRange(0, values.Count - maxCount);
}
}
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.Strategies import Strategy
from StockSharp.Algo.Indicators import ChandeMomentumOscillator
class cmo_duplex_strategy(Strategy):
"""CMO Duplex: two-sided strategy using Chande Momentum Oscillator zero-line crossings."""
def __init__(self):
super(cmo_duplex_strategy, self).__init__()
self._long_candle_type = self.Param("LongCandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Long Candle Type", "Candle type for the long leg", "Long Leg")
self._long_cmo_period = self.Param("LongCmoPeriod", 14) \
.SetGreaterThanZero() \
.SetDisplay("Long CMO Period", "CMO period for the long leg", "Long Leg")
self._long_signal_bar = self.Param("LongSignalBar", 1) \
.SetDisplay("Long Signal Bar", "Offset in bars for long signals", "Long Leg")
self._enable_long_entries = self.Param("EnableLongEntries", True) \
.SetDisplay("Enable Long Entries", "Allow opening long trades", "Long Leg")
self._enable_long_exits = self.Param("EnableLongExits", True) \
.SetDisplay("Enable Long Exits", "Allow closing long trades on signals", "Long Leg")
self._long_stop_loss_points = self.Param("LongStopLossPoints", 1000) \
.SetDisplay("Long Stop Loss", "Stop loss in price steps for longs", "Risk Management")
self._long_take_profit_points = self.Param("LongTakeProfitPoints", 2000) \
.SetDisplay("Long Take Profit", "Take profit in price steps for longs", "Risk Management")
self._short_candle_type = self.Param("ShortCandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Short Candle Type", "Candle type for the short leg", "Short Leg")
self._short_cmo_period = self.Param("ShortCmoPeriod", 14) \
.SetGreaterThanZero() \
.SetDisplay("Short CMO Period", "CMO period for the short leg", "Short Leg")
self._short_signal_bar = self.Param("ShortSignalBar", 1) \
.SetDisplay("Short Signal Bar", "Offset in bars for short signals", "Short Leg")
self._enable_short_entries = self.Param("EnableShortEntries", True) \
.SetDisplay("Enable Short Entries", "Allow opening short trades", "Short Leg")
self._enable_short_exits = self.Param("EnableShortExits", True) \
.SetDisplay("Enable Short Exits", "Allow closing short trades on signals", "Short Leg")
self._short_stop_loss_points = self.Param("ShortStopLossPoints", 1000) \
.SetDisplay("Short Stop Loss", "Stop loss in price steps for shorts", "Risk Management")
self._short_take_profit_points = self.Param("ShortTakeProfitPoints", 2000) \
.SetDisplay("Short Take Profit", "Take profit in price steps for shorts", "Risk Management")
self._long_values = []
self._short_values = []
self._entry_price = None
@property
def LongCandleType(self):
return self._long_candle_type.Value
@property
def LongCmoPeriod(self):
return int(self._long_cmo_period.Value)
@property
def LongSignalBar(self):
return int(self._long_signal_bar.Value)
@property
def EnableLongEntries(self):
return self._enable_long_entries.Value
@property
def EnableLongExits(self):
return self._enable_long_exits.Value
@property
def LongStopLossPoints(self):
return int(self._long_stop_loss_points.Value)
@property
def LongTakeProfitPoints(self):
return int(self._long_take_profit_points.Value)
@property
def ShortCandleType(self):
return self._short_candle_type.Value
@property
def ShortCmoPeriod(self):
return int(self._short_cmo_period.Value)
@property
def ShortSignalBar(self):
return int(self._short_signal_bar.Value)
@property
def EnableShortEntries(self):
return self._enable_short_entries.Value
@property
def EnableShortExits(self):
return self._enable_short_exits.Value
@property
def ShortStopLossPoints(self):
return int(self._short_stop_loss_points.Value)
@property
def ShortTakeProfitPoints(self):
return int(self._short_take_profit_points.Value)
def OnStarted2(self, time):
super(cmo_duplex_strategy, self).OnStarted2(time)
self._long_values = []
self._short_values = []
self._entry_price = None
self._long_cmo = ChandeMomentumOscillator()
self._long_cmo.Length = self.LongCmoPeriod
self._short_cmo = ChandeMomentumOscillator()
self._short_cmo.Length = self.ShortCmoPeriod
long_subscription = self.SubscribeCandles(self.LongCandleType)
same_type = (self.LongCandleType == self.ShortCandleType)
if same_type:
long_subscription.Bind(self._long_cmo, self.process_long_candle)
long_subscription.Bind(self._short_cmo, self.process_short_candle).Start()
else:
long_subscription.Bind(self._long_cmo, self.process_long_candle).Start()
short_subscription = self.SubscribeCandles(self.ShortCandleType)
short_subscription.Bind(self._short_cmo, self.process_short_candle).Start()
def process_long_candle(self, candle, cmo_value):
if candle.State != CandleStates.Finished:
return
if not self._long_cmo.IsFormed:
return
cmo_val = float(cmo_value)
self._long_values.append(cmo_val)
shift = max(1, self.LongSignalBar)
self._trim_buffer(self._long_values, shift + 3)
if len(self._long_values) < shift + 1:
return
current_index = len(self._long_values) - shift
previous_index = current_index - 1
if previous_index < 0:
return
current = self._long_values[current_index]
previous = self._long_values[previous_index]
# Manage long exits
if self.Position > 0 and self._entry_price is not None:
sec = self.Security
step = float(sec.PriceStep) if sec is not None and sec.PriceStep is not None else 1.0
stop_price = self._entry_price - self.LongStopLossPoints * step if self.LongStopLossPoints > 0 else None
take_price = self._entry_price + self.LongTakeProfitPoints * step if self.LongTakeProfitPoints > 0 else None
exit_by_signal = self.EnableLongExits and previous < 0
hit_take = take_price is not None and float(candle.HighPrice) >= take_price
hit_stop = stop_price is not None and float(candle.LowPrice) <= stop_price
if hit_take or hit_stop or exit_by_signal:
self.SellMarket()
self._entry_price = None
# Long entry: CMO crosses down through zero
cross_down = previous > 0 and current <= 0
if self.EnableLongEntries and cross_down and self.Position <= 0:
self.BuyMarket()
self._entry_price = float(candle.ClosePrice)
def process_short_candle(self, candle, cmo_value):
if candle.State != CandleStates.Finished:
return
if not self._short_cmo.IsFormed:
return
cmo_val = float(cmo_value)
self._short_values.append(cmo_val)
shift = max(1, self.ShortSignalBar)
self._trim_buffer(self._short_values, shift + 3)
if len(self._short_values) < shift + 1:
return
current_index = len(self._short_values) - shift
previous_index = current_index - 1
if previous_index < 0:
return
current = self._short_values[current_index]
previous = self._short_values[previous_index]
# Manage short exits
if self.Position < 0 and self._entry_price is not None:
sec = self.Security
step = float(sec.PriceStep) if sec is not None and sec.PriceStep is not None else 1.0
stop_price = self._entry_price + self.ShortStopLossPoints * step if self.ShortStopLossPoints > 0 else None
take_price = self._entry_price - self.ShortTakeProfitPoints * step if self.ShortTakeProfitPoints > 0 else None
exit_by_signal = self.EnableShortExits and previous > 0
hit_take = take_price is not None and float(candle.LowPrice) <= take_price
hit_stop = stop_price is not None and float(candle.HighPrice) >= stop_price
if hit_take or hit_stop or exit_by_signal:
self.BuyMarket()
self._entry_price = None
# Short entry: CMO crosses up through zero
cross_up = previous < 0 and current >= 0
if self.EnableShortEntries and cross_up and self.Position >= 0:
self.SellMarket()
self._entry_price = float(candle.ClosePrice)
def _trim_buffer(self, values, max_count):
if len(values) > max_count:
del values[:len(values) - max_count]
def OnReseted(self):
super(cmo_duplex_strategy, self).OnReseted()
self._long_values = []
self._short_values = []
self._entry_price = None
def CreateClone(self):
return cmo_duplex_strategy()