GitHub で見る

AI グリッド戦略

AI グリッド戦略は、現在の価格の周囲に複数層の買い注文と売り注文を配置します。この戦略はブレイクアウト(ストップ)とカウンタートレンド(リミット)の両アプローチに対応しています。注文が約定すると、テイクプロフィット注文が自動的に発注されます。

詳細

  • エントリー条件: 価格がグリッドのいずれかのレベルに達する。
  • ロング/ショート: AllowLongAllowShort で制御。
  • エグジット条件: 固定距離 TakeProfit 後にテイクプロフィット。
  • ストップ: ストップロスなし。
  • デフォルト値:
    • GridSize = 50m
    • GridSteps = 10
    • TakeProfit = 50m
    • AllowLong = true
    • AllowShort = true
    • UseBreakout = true
    • UseCounter = true
    • CandleType = TimeSpan.FromMinutes(1)
  • フィルター:
    • カテゴリ: Grid
    • 方向: 両方
    • インジケーター: なし
    • ストップ: テイクプロフィットのみ
    • 複雑さ: 中級
    • 時間軸: イントラデイ (1m)
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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-style mean reversion strategy.
/// Buys dips relative to SMA, sells rallies, using ATR-based grid spacing.
/// </summary>
public class AiGridStrategy : Strategy
{
	private readonly StrategyParam<int> _maLength;
	private readonly StrategyParam<DataType> _candleType;

	public int MaLength { get => _maLength.Value; set => _maLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public AiGridStrategy()
	{
		_maLength = Param(nameof(MaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("MA Length", "SMA 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 OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var sma = new SimpleMovingAverage { Length = MaLength };
		var atr = new StandardDeviation { Length = 14 };

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

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

		if (atrVal <= 0)
			return;

		var close = candle.ClosePrice;
		var deviation = close - smaVal;

		// Grid buy: price dropped by more than 1 ATR below SMA
		if (deviation < -atrVal && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// Grid sell: price rose by more than 1 ATR above SMA
		else if (deviation > atrVal && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}
		// Take profit at mean
		else if (Position > 0 && close > smaVal)
		{
			SellMarket();
		}
		else if (Position < 0 && close < smaVal)
		{
			BuyMarket();
		}
	}
}