在 GitHub 上查看

Aeron Robot 网格策略

该策略基于 AeronRobot EA 的网格对冲思想。它在预设的价格间隔处放置买卖单,并在每次新下单后按比例增加手数。策略旨在利用价格的小幅波动获利,同时通过可配置的止盈、止损和交易数量限制来控制风险。

当价格按 Gap 参数移动时,会以 LotsFactor 倍数增加的手数开立新的订单。价格回到 TakeProfit 点时锁定利润,若价格反向达到 StopLoss 点则退出。若启用 Hedging,可同时持有多头和空头仓位。

详情

  • 入场条件
    • 做多:价格从上一笔买单下跌 Gap 点。
    • 做空:价格从上一笔卖单上涨 Gap 点。
  • 仓位管理:每次新单的手数乘以 LotsFactor
  • 出场条件
    • 当盈利超过 TakeProfit 点时平仓。
    • 当亏损超过 StopLoss 点时平仓。
  • 参数
    • FirstLot – 初始手数。
    • LotsFactor – 手数递增系数。
    • Gap – 网格间距(点)。
    • GapFactor – 每次交易后间距的放大系数。
    • MaxTrades – 每个方向的最大挂单数量。
    • Hedging – 允许同时持有多空仓位。
    • TakeProfit – 止盈点数。
    • StopLoss – 止损点数。
    • CandleType – 使用的蜡烛图周期。
  • 多空方向:均可。
  • 筛选标签
    • 类别:网格 / 逆势
    • 方向:双向
    • 指标:无
    • 止损:有
    • 复杂度:中等
    • 周期:短期
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:高
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>
/// EMA crossover strategy (converted from grid).
/// </summary>
public class AeronRobotStrategy : 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 AeronRobotStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "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;
	}
}