Cheduecoglioni 交替策略
概述
该策略是 MQL5 专家顾问“cheduecoglioni”的 StockSharp 版本。它始终保持账户在市,通过固定顺序交替做空和做多。每笔交易都携带固定的止盈和止损距离,这些距离以点值表示,并根据标的物精度转换为绝对价格。
交易逻辑
- 策略订阅所配置的蜡烛序列(默认 1 分钟),仅在蜡烛完全收盘后执行逻辑,用来替代原始 EA 中基于 Tick 的循环。
- 当没有持仓并且不存在等待成交的市价单时,按照
_nextSide状态记录的方向发送市价单。启动后的第一笔交易为卖出,与 MQL5 实现保持一致。 - 持仓被激活后,算法等待仓位通过保护性订单或手工操作离场。一旦仓位恢复为零,下一笔交易方向立即翻转,从而始终开立与上一次相反的仓位。
- 通过
StartProtection自动附加止盈与止损,确保每笔交易都具有预期的风险回报距离。
参数
Trade Volume– 每笔市价单的下单量,对应原始输入InpLots。Take Profit (pips)– 止盈点数。策略会根据点值换算成绝对价格偏移。Stop Loss (pips)– 止损点数,使用相同的点值逻辑进行转换。Candle Type– 触发决策循环的蜡烛类型,可设置任意支持的DataType。
实现细节
- 点值来自
Security.PriceStep。若品种为 3 位或 5 位小数的外汇报价,会额外乘以 10,使分数点还原为标准点,复现 MQL 脚本中的调整。 - 使用等待标志防止在上一笔市价单尚未成交时重复下单;若经纪商拒绝订单,
OnOrderFailed会清除标志,下一根蜡烛即可重试。 OnPositionChanged追踪当前仓位方向,并在仓位清零后切换_nextSide,实现与原版 EA 相同的交替开仓行为。StartProtection以市价退出方式管理保护性订单,等价于原脚本在下单时立即设置止盈止损。
注意事项
- 目前尚未创建 Python 版本。
- 本策略不会修改任何单元测试。
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;
using StockSharp.Algo;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Alternates buy and sell market orders with fixed stop loss and take profit distances.
/// </summary>
public class CheduecoglioniAlternatingStrategy : Strategy
{
private readonly StrategyParam<decimal> _tradeVolume;
private readonly StrategyParam<decimal> _takeProfitPips;
private readonly StrategyParam<decimal> _stopLossPips;
private readonly StrategyParam<DataType> _candleType;
private decimal _pipSize;
private Sides _nextSide;
private Sides? _activeSide;
/// <summary>
/// Initializes a new instance of the <see cref="CheduecoglioniAlternatingStrategy"/> class.
/// </summary>
public CheduecoglioniAlternatingStrategy()
{
_tradeVolume = Param(nameof(TradeVolume), 1m)
.SetDisplay("Trade Volume", "Volume per trade", "General")
.SetGreaterThanZero();
_takeProfitPips = Param(nameof(TakeProfitPips), 10m)
.SetDisplay("Take Profit (pips)", "Distance to take profit", "Risk")
.SetGreaterThanZero();
_stopLossPips = Param(nameof(StopLossPips), 10m)
.SetDisplay("Stop Loss (pips)", "Distance to stop loss", "Risk")
.SetGreaterThanZero();
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Source candles for timing", "General");
}
/// <summary>
/// Volume used for each market order.
/// </summary>
public decimal TradeVolume
{
get => _tradeVolume.Value;
set => _tradeVolume.Value = value;
}
/// <summary>
/// Take profit distance expressed in pips.
/// </summary>
public decimal TakeProfitPips
{
get => _takeProfitPips.Value;
set => _takeProfitPips.Value = value;
}
/// <summary>
/// Stop loss distance expressed in pips.
/// </summary>
public decimal StopLossPips
{
get => _stopLossPips.Value;
set => _stopLossPips.Value = value;
}
/// <summary>
/// Candle type that triggers trading decisions.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
Volume = TradeVolume;
_nextSide = Sides.Sell;
_activeSide = null;
_pipSize = 0m;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
Volume = TradeVolume; // Align the base volume with the strategy parameter.
var priceStep = Security?.PriceStep ?? 0m;
if (priceStep <= 0m)
{
var decimals = Security?.Decimals ?? 4;
priceStep = (decimal)Math.Pow(10, -decimals);
}
_pipSize = priceStep;
var secDecimals = Security?.Decimals;
if (secDecimals is int digits && (digits == 3 || digits == 5))
{
_pipSize *= 10m; // Convert from fractional pip to full pip for FX symbols.
}
if (_pipSize <= 0m)
{
_pipSize = 1m; // Fallback to a neutral value if the instrument metadata is missing.
}
StartProtection(
takeProfit: new Unit(TakeProfitPips * _pipSize, UnitTypes.Absolute),
stopLoss: new Unit(StopLossPips * _pipSize, UnitTypes.Absolute),
useMarketOrders: true);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
// Strategy has no bound indicators, always allow trading.
if (Position != 0)
return; // Skip if a position exists.
var volume = TradeVolume;
if (volume <= 0m)
return;
if (_nextSide == Sides.Buy)
BuyMarket();
else
SellMarket();
}
/// <inheritdoc />
protected override void OnPositionReceived(Position position)
{
base.OnPositionReceived(position);
if (Position > 0)
{
_activeSide = Sides.Buy;
return;
}
if (Position < 0)
{
_activeSide = Sides.Sell;
return;
}
if (_activeSide.HasValue)
{
_nextSide = _activeSide == Sides.Buy ? Sides.Sell : Sides.Buy; // Alternate direction after a flat position.
_activeSide = null;
}
}
}
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, Unit, UnitTypes, Sides
from StockSharp.Algo.Strategies import Strategy
class cheduecoglioni_alternating_strategy(Strategy):
"""
Alternates buy and sell market orders with fixed stop loss and take profit distances.
"""
def __init__(self):
super(cheduecoglioni_alternating_strategy, self).__init__()
self._take_profit_pips = self.Param("TakeProfitPips", 10.0) \
.SetDisplay("Take Profit (pips)", "Distance to take profit", "Risk")
self._stop_loss_pips = self.Param("StopLossPips", 10.0) \
.SetDisplay("Stop Loss (pips)", "Distance to stop loss", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Source candles for timing", "General")
self._pip_size = 0.0
self._next_side = Sides.Sell
self._active_side = None
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(cheduecoglioni_alternating_strategy, self).OnReseted()
self._next_side = Sides.Sell
self._active_side = None
self._pip_size = 0.0
def OnStarted2(self, time):
super(cheduecoglioni_alternating_strategy, self).OnStarted2(time)
price_step = 1.0
if self.Security is not None and self.Security.PriceStep is not None and float(self.Security.PriceStep) > 0:
price_step = float(self.Security.PriceStep)
else:
decimals = 4
if self.Security is not None and self.Security.Decimals is not None:
decimals = int(self.Security.Decimals)
price_step = 10.0 ** (-decimals)
self._pip_size = price_step
if self.Security is not None and self.Security.Decimals is not None:
digits = int(self.Security.Decimals)
if digits == 3 or digits == 5:
self._pip_size *= 10.0
if self._pip_size <= 0:
self._pip_size = 1.0
self.StartProtection(
takeProfit=Unit(self._take_profit_pips.Value * self._pip_size, UnitTypes.Absolute),
stopLoss=Unit(self._stop_loss_pips.Value * self._pip_size, UnitTypes.Absolute),
useMarketOrders=True)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self.on_process).Start()
def on_process(self, candle):
if candle.State != CandleStates.Finished:
return
if self.Position != 0:
return
if self._next_side == Sides.Buy:
self.BuyMarket()
else:
self.SellMarket()
def OnPositionReceived(self, position):
super(cheduecoglioni_alternating_strategy, self).OnPositionReceived(position)
if self.Position > 0:
self._active_side = Sides.Buy
return
if self.Position < 0:
self._active_side = Sides.Sell
return
if self._active_side is not None:
if self._active_side == Sides.Buy:
self._next_side = Sides.Sell
else:
self._next_side = Sides.Buy
self._active_side = None
def CreateClone(self):
return cheduecoglioni_alternating_strategy()