在 GitHub 上查看

Exp Cronex AO 策略

该策略将 MetaTrader 专家顾问 Exp_CronexAO 移植到 StockSharp 高级 API。原始机器人通过比较 Cronex Awesome Oscillator(AO) 的两条平滑曲线来交易。本移植版本订阅可配置的 K 线序列,计算 AO,并连续执行两次移动平均平滑以重建 Cronex 曲线,当快线穿越慢线时开仓或平仓。

交易逻辑

  1. 基于所选 K 线计算 Awesome Oscillator。
  2. 对振荡器进行两次简单移动平均平滑:第一次平滑生成 Cronex 快线,第二次平滑处理快线得到信号线。
  3. 回看 SignalBar 根已完成的 K 线,比较该根 K 线上以及再前一根 K 线上的两条 Cronex 曲线。
  4. 当快线位于慢线上方并且在回看柱上发生向上交叉时产生 买入 信号。策略可选地先平掉空头仓位,并在允许的情况下按市价建立多头仓位。
  5. 卖出 信号与上述规则相反:快线必须位于慢线之下,并且在回看柱上完成向下交叉。策略可选地平掉多头仓位,并在允许时开立空头。
  6. 每次开仓都会按交易品种的点数添加止损与止盈距离。

策略始终只保留一个净头寸。当方向发生变化时,会把平掉反向仓位所需的数量与新的下单数量合并为一笔市价单,以模拟 MetaTrader 净值账户的处理方式。

参数

参数 说明
CandleType 用于 Cronex AO 计算的 K 线数据类型,默认是 8 小时时间框架。
FastPeriod 对 AO 进行第一次平滑时使用的周期长度。
SlowPeriod 对快线进行第二次平滑时使用的周期长度。
SignalBar 回看的已完成柱数,交叉必须出现在该柱上,并会结合前一柱确认方向。
BuyOpenEnabled / SellOpenEnabled 是否允许开多或开空。
BuyCloseEnabled / SellCloseEnabled 是否允许在出现反向信号时平多或平空。
TakeProfit 止盈点数,大于零时在每次开仓后附加。
StopLoss 止损点数,大于零时在每次开仓后附加。

风险控制

止损和止盈与 MetaTrader 版本一样以点数表示,每次新建仓位时都会重新计算,使保护性订单始终匹配当前的净仓位规模。

与 MetaTrader 版本的差异

  • 本实现为两个 Cronex 平滑阶段都使用简单移动平均。原始 XMA 类支持多种平滑方法,但其默认配置正好对应于这里复现的简单平均。
  • 未移植 TradeAlgorithms 库中的滑点和资金管理函数。仓位规模通过策略的 Volume 属性配置。
  • 下单依赖 StockSharp 的净头寸模式。方向反转时会发送一笔足够大的市价单,同时平仓并建立新方向,与 MT5 净值账户的行为一致。
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 ExpCronexAOStrategy : 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 ExpCronexAOStrategy()
	{
		_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;
	}
}