在 GitHub 上查看

Cm Manual Grid

Cm Manual Grid 在当前价格周围放置可配置的止损和限价订单网格。每个新订单的数量按固定值增加。策略可以在达到目标利润时分别关闭多头或空头仓位,并包含跟踪利润机制。

详情

  • 类型:网格交易,使用挂单
  • 订单:Buy Stop、Sell Stop、Buy Limit、Sell Limit
  • 数量:初始 Lot,增量 LotPlus
  • 利润管理
    • CloseProfitB 关闭多头仓位
    • CloseProfitS 关闭空头仓位
    • ProfitClose 关闭所有仓位
    • TralStartTralClose 控制跟踪利润
  • 默认值
    • OrdersBuyStop = 5
    • OrdersSellStop = 5
    • OrdersBuyLimit = 5
    • OrdersSellLimit = 5
    • FirstLevel = 5 步
    • StepBuyStop = 10
    • StepSellStop = 10
    • StepBuyLimit = 10
    • StepSellLimit = 10
    • Lot = 0.1
    • LotPlus = 0.1
    • CloseProfitB = 10
    • CloseProfitS = 10
    • ProfitClose = 10
    • TralStart = 10
    • TralClose = 5
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>
/// Grid strategy that places buy/sell orders at regular intervals
/// based on price distance from a reference level (SMA).
/// Buys below SMA, sells above SMA with grid step spacing.
/// </summary>
public class CmManualGridStrategy : Strategy
{
	private readonly StrategyParam<int> _smaPeriod;
	private readonly StrategyParam<decimal> _gridStep;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _lastBuyPrice;
	private decimal _lastSellPrice;

	public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
	public decimal GridStep { get => _gridStep.Value; set => _gridStep.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public CmManualGridStrategy()
	{
		_smaPeriod = Param(nameof(SmaPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("SMA Period", "Moving average period for center", "Indicators");

		_gridStep = Param(nameof(GridStep), 200m)
			.SetGreaterThanZero()
			.SetDisplay("Grid Step", "Price distance between grid levels", "Grid");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_lastBuyPrice = 0;
		_lastSellPrice = 0;
	}

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

		_lastBuyPrice = 0;
		_lastSellPrice = 0;

		var sma = new SimpleMovingAverage { Length = SmaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(sma, ProcessCandle).Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, sma);
			DrawOwnTrades(area);
		}
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var price = candle.ClosePrice;
		var step = GridStep;

		// Buy when price is below SMA and spaced from last buy
		if (price < smaValue - step)
		{
			if (_lastBuyPrice == 0 || price <= _lastBuyPrice - step)
			{
				BuyMarket();
				_lastBuyPrice = price;
			}
		}

		// Sell when price is above SMA and spaced from last sell
		if (price > smaValue + step)
		{
			if (_lastSellPrice == 0 || price >= _lastSellPrice + step)
			{
				SellMarket();
				_lastSellPrice = price;
			}
		}

		// Reset grid when price returns to center
		if (price > smaValue && _lastBuyPrice != 0)
			_lastBuyPrice = 0;
		if (price < smaValue && _lastSellPrice != 0)
			_lastSellPrice = 0;
	}
}