在 GitHub 上查看

Line Order Dual Level 策略

该策略是 MetaTrader 智能交易系统 "MyLineOrder" 的 StockSharp 版本。交易者可以设置水平价格线,当价格触及这些水平时自动执行市价单。止损、止盈和跟踪止损以点数表示,交易量也可配置。

当市场价格触及 BuyPrice 时策略做多;触及 SellPrice 时策略做空。开仓后策略根据止损、止盈或跟踪止损条件监控并退出仓位。

细节

  • 入场条件
    • 多头:价格触及或突破 BuyPrice
    • 空头:价格触及或跌破 SellPrice
  • 方向:双向。
  • 出场条件
    • 止损、止盈或跟踪止损。
  • 止损
    • StopLossPipsTakeProfitPipsTrailingStopPips
  • 过滤器
    • 无。
  • 参数
    • BuyPrice – 做多入场水平。
    • SellPrice – 做空入场水平。
    • StopLossPips – 止损点数。
    • TakeProfitPips – 止盈点数。
    • TrailingStopPips – 跟踪止损点数。
    • TradeVolume – 交易量。
    • CandleType – 用于监控价格的蜡烛时间框。
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>
/// Dual-level breakout strategy using SMA and ATR for dynamic support/resistance levels.
/// Buys when price breaks above SMA + ATR, sells when breaks below SMA - ATR.
/// Exits on mean reversion to SMA.
/// </summary>
public class LineOrderDualLevelStrategy : Strategy
{
	private readonly StrategyParam<int> _smaPeriod;
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _atrMult;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;

	public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
	public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
	public decimal AtrMult { get => _atrMult.Value; set => _atrMult.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public LineOrderDualLevelStrategy()
	{
		_smaPeriod = Param(nameof(SmaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("SMA Period", "SMA period", "Indicators");
		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "ATR period", "Indicators");
		_atrMult = Param(nameof(AtrMult), 1.0m)
			.SetDisplay("ATR Mult", "ATR multiplier for levels", "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 OnReseted()
	{
		base.OnReseted();
		_entryPrice = 0;
	}

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

		var sma = new SimpleMovingAverage { Length = SmaPeriod };
		var atr = new StandardDeviation { Length = AtrPeriod };

		SubscribeCandles(CandleType)
			.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 upperLevel = smaVal + atrVal * AtrMult;
		var lowerLevel = smaVal - atrVal * AtrMult;

		// Breakout above upper level => long
		if (close > upperLevel && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
			_entryPrice = close;
		}
		// Breakout below lower level => short
		else if (close < lowerLevel && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
			_entryPrice = close;
		}
		// Exit long at SMA or if loss > 2*ATR
		else if (Position > 0)
		{
			if (close <= smaVal || (_entryPrice > 0 && close <= _entryPrice - atrVal * 2))
			{
				SellMarket();
				_entryPrice = 0;
			}
		}
		// Exit short at SMA or if loss > 2*ATR
		else if (Position < 0)
		{
			if (close >= smaVal || (_entryPrice > 0 && close >= _entryPrice + atrVal * 2))
			{
				BuyMarket();
				_entryPrice = 0;
			}
		}
	}
}