在 GitHub 上查看

Cronex AC

Cronex AC 策略在 StockSharp 高阶 API 中还原了经典的 Cronex Acceleration/Deceleration (AC) 专家顾问。策略将 Accelerator 振荡器连续进行两次平滑处理,当快速线与信号线发生交叉时执行开仓或平仓:向上交叉开多单、平空单,向下交叉开空单、平多单。

交易逻辑

  1. 根据选定的 K 线序列计算 Accelerator/Deceleration 振荡器数值。
  2. 按照所选类型对振荡器进行两次移动平均平滑:第一次得到“快线”,第二次得到“信号线”。
  3. SignalBar 指定的历史 K 线上读取信号,并额外向前查看一根 K 线以确认是否发生真正的穿越。
  4. 当快线上穿信号线时(若允许),策略先平掉空头仓位,再开立多头仓位。
  5. 当快线下穿信号线时(若允许),策略先平掉多头仓位,再开立空头仓位。
  6. 下单手数等于参数 Volume 加上当前持仓的绝对值,使策略能够通过一次市价单直接完成反手。

策略完全基于收盘后的数据做决策,并保留了 MQL5 版本中针对多、空方向分别授权的开仓与平仓开关。

参数

名称 类型 默认值 说明
SmoothingType CronexMovingAverageType Simple 应用于 AC 振荡器的移动平均类型,可选:Simple、Exponential、Smoothed、Weighted。
FastPeriod int 14 第一次平滑的周期(快线)。
SlowPeriod int 25 第二次平滑的周期(信号线)。
SignalBar int 1 读取信号所使用的已完成 K 线偏移,1 代表上一根 K 线,与原版 Cronex 保持一致。
CandleType DataType TimeFrame(8h) 计算所使用的 K 线类型。
EnableLongEntry bool true 允许在向上交叉时开多。
EnableShortEntry bool true 允许在向下交叉时开空。
EnableLongExit bool true 允许在快线跌破信号线时平多。
EnableShortExit bool true 允许在快线上破信号线时平空。
Volume decimal 策略默认 下单数量。策略会自动加上当前持仓的绝对值,以便一次性完成反手。

图表展示

当界面存在图表区域时,策略会绘制:

  • 选定周期的价格 K 线;
  • Accelerator 振荡器曲线;
  • 快线与信号线;
  • 策略自身的成交记录,用于直观验证。

说明

  • 所有计算都基于已完成的 K 线 (CandleStates.Finished),避免信号重绘。
  • 内部缓冲区仅保存满足 SignalBar 位移所需的最少数据,保持与 MQL 原策略一致的行为。
  • 原始 MQL 版本中的止损、止盈及滑点参数未移植,可在 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;

public class CronexAcStrategy : 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 CronexAcStrategy()
	{
		_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;
	}
}