在 GitHub 上查看

网格交易策略

该策略实现了基础网格系统。根据 GridStep 间隔在价格上方放置 Buy Stop,在下方放置 Sell Stop。每个订单使用固定的止盈距离。当总利润超过 ProfitTarget 时,策略关闭所有仓位并重置网格。可选地,新的订单数量按马丁公式增加。

细节

  • 入场条件:
    • 在当前价格上方一格放置 Buy Stop。
    • 在当前价格下方一格放置 Sell Stop。
  • 多空方向: 双向。
  • 出场条件:
    • 每个头寸按固定止盈退出。
    • 总利润达到 ProfitTarget 时全部平仓。
  • 止损: 仅止盈。
  • 过滤器: 无。
using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Simple grid trading strategy.
/// Buys when price moves up a grid level, sells when down.
/// Closes on profit target.
/// </summary>
public class GridStrategy : Strategy
{
	private readonly StrategyParam<decimal> _gridStep;
	private readonly StrategyParam<decimal> _profitTarget;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _lastGridLevel;
	private decimal _entryPrice;

	public decimal GridStep { get => _gridStep.Value; set => _gridStep.Value = value; }
	public decimal ProfitTarget { get => _profitTarget.Value; set => _profitTarget.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public GridStrategy()
	{
		_gridStep = Param(nameof(GridStep), 500m)
			.SetGreaterThanZero()
			.SetDisplay("Grid Step", "Step size in price units", "General");

		_profitTarget = Param(nameof(ProfitTarget), 2000m)
			.SetDisplay("Profit Target", "Profit to close position", "Risk");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_lastGridLevel = 0;
		_entryPrice = 0;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		SubscribeCandles(CandleType)
			.Bind(ProcessCandle)
			.Start();
	}

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

		var step = GridStep;
		var currentLevel = Math.Floor(candle.ClosePrice / step) * step;

		if (_lastGridLevel == 0)
		{
			_lastGridLevel = currentLevel;
			return;
		}

		if (currentLevel > _lastGridLevel)
		{
			if (Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
				_entryPrice = candle.ClosePrice;
			}
		}
		else if (currentLevel < _lastGridLevel)
		{
			if (Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
				_entryPrice = candle.ClosePrice;
			}
		}

		_lastGridLevel = currentLevel;

		// Check profit target
		if (Position > 0 && _entryPrice > 0)
		{
			if (candle.ClosePrice - _entryPrice >= ProfitTarget)
			{
				SellMarket();
				_entryPrice = 0;
			}
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (_entryPrice - candle.ClosePrice >= ProfitTarget)
			{
				BuyMarket();
				_entryPrice = 0;
			}
		}
	}
}