在 GitHub 上查看

Perceptron AC 策略

该策略基于 Accelerator Oscillator (AC) 构建简单感知器。 当前蜡烛及历史 7、14、21 根蜡烛的 AC 值与可调权重相乘, 其加权和决定交易方向。

工作原理

  1. 计算 AC:取 Awesome Oscillator 与其 5 周期 SMA 之差。
  2. 保存最近 22 个 AC 值,以获取 0、7、14、21 根前的值。
  3. 计算感知器输出: P = (X1-100)*AC[0] + (X2-100)*AC[7] + (X3-100)*AC[14] + (X4-100)*AC[21]
  4. P > 0 开多仓或保持多仓;若 P < 0 开空仓或保持空仓。
  5. 当盈利超过初始止损 StopLoss 后:
    • P 变号,则反手;
    • 否则将止损追踪至当前价格上下 StopLoss

参数

  • X1 – 当前 AC 权重(默认 288)。
  • X2 – 前 7 根 AC 权重(默认 216)。
  • X3 – 前 14 根 AC 权重(默认 144)。
  • X4 – 前 21 根 AC 权重(默认 72)。
  • Stop Loss – 价格单位止损与反手阈值(默认 300)。
  • Volume – 下单数量(默认 1)。
  • Candle Type – 订阅的蜡烛类型(默认 5 分钟)。

交易规则

  • P > 0 且无持仓时做多。
  • P < 0 且无持仓时做空。
  • 持仓盈利达到 StopLoss * 2 后移动止损。
  • 若此时 P 变号则反手。

原始版本

根据 MQL/11102 中的 MQL4 脚本 auto_m5.mq4 转换而来。

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;

/// <summary>
/// Perceptron-based strategy using EMA crossover.
/// </summary>
public class PerceptronAcStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public PerceptronAcStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Perceptron");
		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Perceptron");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastVal, decimal slowVal)
	{
		if (candle.State != CandleStates.Finished) return;

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}