在 GitHub 上查看

订单情绪策略

概述

该策略根据盘口中买单和卖单的失衡进行交易。它计算双方的订单数量和总成交量比值,当某一方的优势超过设定阈值时开仓。策略只在指定的时间窗口内运行。

开仓后会降低阈值来监控相反的一方。如果相反一方超过这些降低后的阈值则平仓。同时使用以价格点表示的止损和止盈。

交易规则

  • 做多入场
    • 买量 / 卖量 >= DiffVolumesEx买单数 / 卖单数 >= DiffTradersEx
    • 任意一侧满足 MinTradersMinVolume
    • 当前时间通过 CheckTradingTime
  • 做空入场:使用对称逻辑判断卖方优势。
  • 平仓
    • 多单在 卖量 / 买量 > 1 / DiffVolumes卖单数 / 买单数 > 1 / DiffTraders 时平仓
    • 空单在 卖量 / 买量 < DiffVolumes卖单数 / 买单数 < DiffTraders 时平仓
    • 交易时间之外全部平仓
  • 止损:使用以价格点计算的止损和止盈。

参数

  • MinVolume – 任一侧所需的最小成交量(默认 20000)
  • MinTraders – 任一侧所需的最小订单数量(默认 1000)
  • DiffVolumesEx – 入场所需的成交量比(默认 2.0)
  • DiffTradersEx – 入场所需的订单数量比(默认 1.5)
  • MinDiffVolumesEx – 开仓后用于监控的成交量比(默认 1.5)
  • MinDiffTradersEx – 开仓后用于监控的订单数量比(默认 1.3)
  • SleepMinutes – 盘口检查的间隔分钟数(默认 5)
  • TpPips – 止盈价差(默认 500)
  • SlPips – 止损价差(默认 500)

说明

本策略暂不提供 Python 版本。

using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy trading on volume sentiment using candle data.
/// Compares bullish vs bearish volume over a lookback period.
/// </summary>
public class SessionOrderSentimentStrategy : Strategy
{
	private readonly StrategyParam<decimal> _volumeRatio;
	private readonly StrategyParam<int> _lookback;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<DataType> _candleType;

	private readonly List<(decimal vol, bool isBull)> _volumeHistory = new();
	private decimal _entryPrice;

	/// <summary>
	/// Volume ratio required for entry.
	/// </summary>
	public decimal VolumeRatio
	{
		get => _volumeRatio.Value;
		set => _volumeRatio.Value = value;
	}

	/// <summary>
	/// Lookback period in candles.
	/// </summary>
	public int Lookback
	{
		get => _lookback.Value;
		set => _lookback.Value = value;
	}

	/// <summary>
	/// Stop loss percentage.
	/// </summary>
	public decimal StopLossPct
	{
		get => _stopLossPct.Value;
		set => _stopLossPct.Value = value;
	}

	/// <summary>
	/// Candle type for processing.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initialize <see cref="SessionOrderSentimentStrategy"/>.
	/// </summary>
	public SessionOrderSentimentStrategy()
	{
		_volumeRatio = Param(nameof(VolumeRatio), 1.5m)
			.SetDisplay("Volume Ratio", "Bull/bear volume ratio for entry", "General")
			.SetGreaterThanZero();

		_lookback = Param(nameof(Lookback), 10)
			.SetDisplay("Lookback", "Number of candles to look back", "General")
			.SetGreaterThanZero();

		_stopLossPct = Param(nameof(StopLossPct), 1m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
			.SetGreaterThanZero();

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

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

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

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

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

		var isBull = candle.ClosePrice >= candle.OpenPrice;
		_volumeHistory.Add((candle.TotalVolume, isBull));

		if (_volumeHistory.Count > Lookback)
			_volumeHistory.RemoveAt(0);

		if (_volumeHistory.Count < Lookback)
			return;

		var bullVolume = 0m;
		var bearVolume = 0m;

		foreach (var (vol, bull) in _volumeHistory)
		{
			if (bull)
				bullVolume += vol;
			else
				bearVolume += vol;
		}

		if (bearVolume == 0) bearVolume = 1;
		if (bullVolume == 0) bullVolume = 1;

		var bullBearRatio = bullVolume / bearVolume;
		var bearBullRatio = bearVolume / bullVolume;

		var close = candle.ClosePrice;

		// Check stop loss
		if (Position > 0 && close <= _entryPrice * (1m - StopLossPct / 100m))
		{
			SellMarket();
			return;
		}
		if (Position < 0 && close >= _entryPrice * (1m + StopLossPct / 100m))
		{
			BuyMarket();
			return;
		}

		// Bullish sentiment
		if (bullBearRatio >= VolumeRatio)
		{
			if (Position < 0)
			{
				BuyMarket();
			}
			if (Position <= 0)
			{
				_entryPrice = close;
				BuyMarket();
			}
		}
		// Bearish sentiment
		else if (bearBullRatio >= VolumeRatio)
		{
			if (Position > 0)
			{
				SellMarket();
			}
			if (Position >= 0)
			{
				_entryPrice = close;
				SellMarket();
			}
		}
	}

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