GitHub で見る

AI 出来高戦略

AI 出来高戦略は突然の参加急増を狙います。現在の出来高がその EMA を所定の乗数で上回ると出来高スパイクが発生します。スパイクが 50 期間の価格 EMA とローソク足の色に一致する場合、その方向にエントリーします。各取引は固定バー数後に決済されます。

詳細

  • エントリー条件: 出来高 > VolumeEMA * VolumeMultiplier かつ価格が 50 EMA の上/下でローソク足の色が一致。
  • ロング/ショート: 両方向。
  • エグジット条件: ExitBars 本のローソク足後にポジション決済。
  • ストップ: なし。
  • デフォルト値:
    • VolumeEmaLength = 20
    • VolumeMultiplier = 2.0
    • ExitBars = 5
    • CandleType = TimeSpan.FromMinutes(1)
  • フィルター:
    • カテゴリ: 出来高ブレイクアウト
    • 方向: 両方
    • インジケーター: EMA, Volume EMA
    • ストップ: いいえ
    • 複雑さ: 基本
    • 時間軸: 任意
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// AI Volume Strategy - trades volume spikes in trend direction.
/// Uses EMA for trend and volume EMA for spike detection.
/// </summary>
public class AiVolumeStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _priceEmaLength;
	private readonly StrategyParam<int> _volumeEmaLength;
	private readonly StrategyParam<decimal> _volumeMultiplier;
	private readonly StrategyParam<int> _exitBars;
	private readonly StrategyParam<int> _cooldownBars;

	private SimpleMovingAverage _volumeSma;
	private int _barsInPosition;
	private int _cooldownRemaining;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int PriceEmaLength { get => _priceEmaLength.Value; set => _priceEmaLength.Value = value; }
	public int VolumeEmaLength { get => _volumeEmaLength.Value; set => _volumeEmaLength.Value = value; }
	public decimal VolumeMultiplier { get => _volumeMultiplier.Value; set => _volumeMultiplier.Value = value; }
	public int ExitBars { get => _exitBars.Value; set => _exitBars.Value = value; }
	public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }

	public AiVolumeStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_priceEmaLength = Param(nameof(PriceEmaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("Price EMA Length", "Length for price EMA", "Parameters");

		_volumeEmaLength = Param(nameof(VolumeEmaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("Volume EMA Length", "Length for volume EMA", "Parameters");

		_volumeMultiplier = Param(nameof(VolumeMultiplier), 1.0m)
			.SetDisplay("Volume Multiplier", "Multiplier for volume spike detection", "Parameters");

		_exitBars = Param(nameof(ExitBars), 20)
			.SetGreaterThanZero()
			.SetDisplay("Exit Bars", "Exit position after this many bars", "Risk");

		_cooldownBars = Param(nameof(CooldownBars), 15)
			.SetDisplay("Cooldown Bars", "Bars between trades", "Risk");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_volumeSma = null;
		_barsInPosition = 0;
		_cooldownRemaining = 0;
	}

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

		var priceEma = new ExponentialMovingAverage { Length = PriceEmaLength };
		_volumeSma = new SimpleMovingAverage { Length = VolumeEmaLength };

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

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

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

		var volumeResult = _volumeSma.Process(new DecimalIndicatorValue(_volumeSma, candle.TotalVolume, candle.ServerTime));

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		// Time-based exit
		if (Position != 0)
		{
			_barsInPosition++;
			if (_barsInPosition >= ExitBars)
			{
				if (Position > 0)
					SellMarket(Math.Abs(Position));
				else
					BuyMarket(Math.Abs(Position));
				_barsInPosition = 0;
				_cooldownRemaining = CooldownBars;
				return;
			}
		}

		if (_cooldownRemaining > 0)
		{
			_cooldownRemaining--;
			return;
		}

		var avgVolume = _volumeSma.IsFormed ? volumeResult.ToDecimal() : 0m;
		var volumeSpike = avgVolume > 0 && candle.TotalVolume > avgVolume * VolumeMultiplier;
		// If no volume data, use price action only
		var useVolumeFilter = avgVolume > 0;

		var trendUp = candle.ClosePrice > priceEmaValue;
		var trendDown = candle.ClosePrice < priceEmaValue;
		var isBullish = candle.ClosePrice > candle.OpenPrice;
		var isBearish = candle.ClosePrice < candle.OpenPrice;

		var longOk = trendUp && isBullish && (!useVolumeFilter || volumeSpike);
		var shortOk = trendDown && isBearish && (!useVolumeFilter || volumeSpike);

		if (longOk && Position <= 0)
		{
			if (Position < 0)
				BuyMarket(Math.Abs(Position));
			BuyMarket(Volume);
			_barsInPosition = 0;
			_cooldownRemaining = CooldownBars;
		}
		else if (shortOk && Position >= 0)
		{
			if (Position > 0)
				SellMarket(Math.Abs(Position));
			SellMarket(Volume);
			_barsInPosition = 0;
			_cooldownRemaining = CooldownBars;
		}
	}
}