在 GitHub 上查看

随机移动止损策略

随机移动止损策略根据简单移动平均线的偏向打开随机交易,并使用追踪止损进行管理。

详情

  • 入场条件:基于SMA偏向的随机方向
  • 多空方向:双向
  • 出场条件:追踪止损
  • 止损:是
  • 默认值:
    • MinStopLevel = 0.00036
    • TrailingStep = 0.00001
    • SleepMinutes = 5
    • SmaPeriod = 100
    • Volume = 0.1
  • 过滤器:
    • 类别: 实验性
    • 方向: 双向
    • 指标: SMA
    • 止损: 是
    • 复杂度: 基础
    • 时间框架: 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>
/// Random entry strategy with trailing stop management.
/// Biases trade direction using SMA trend filter.
/// </summary>
public class RandomTrailingStopStrategy : Strategy
{
	private readonly StrategyParam<decimal> _minStopLevel;
	private readonly StrategyParam<decimal> _trailingStep;
	private readonly StrategyParam<int> _sleepBars;
	private readonly StrategyParam<int> _smaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private int _barsSinceLastTrade;
	private decimal? _stopPrice;

	public decimal MinStopLevel { get => _minStopLevel.Value; set => _minStopLevel.Value = value; }
	public decimal TrailingStep { get => _trailingStep.Value; set => _trailingStep.Value = value; }
	public int SleepBars { get => _sleepBars.Value; set => _sleepBars.Value = value; }
	public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public RandomTrailingStopStrategy()
	{
		_minStopLevel = Param(nameof(MinStopLevel), 0.5m)
			.SetGreaterThanZero()
			.SetDisplay("Min Stop %", "Minimal stop distance percent", "Trading");

		_trailingStep = Param(nameof(TrailingStep), 0.1m)
			.SetGreaterThanZero()
			.SetDisplay("Trailing Step %", "Trailing stop adjustment step percent", "Trading");

		_sleepBars = Param(nameof(SleepBars), 20)
			.SetGreaterThanZero()
			.SetDisplay("Sleep Bars", "Pause before next trade in bars", "General");

		_smaPeriod = Param(nameof(SmaPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("SMA Period", "Simple moving average period", "Indicators");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_barsSinceLastTrade = 0;
		_stopPrice = null;
	}

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

		var sma = new SimpleMovingAverage { Length = SmaPeriod };
		var subscription = SubscribeCandles(CandleType);

		subscription.Bind(sma, ProcessCandle).Start();

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

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

		_barsSinceLastTrade++;

		if (Position == 0)
		{
			if (_barsSinceLastTrade < SleepBars)
				return;

			_stopPrice = null;

			var side = GetRandomSide(candle, smaValue);

			if (side == Sides.Buy)
				BuyMarket();
			else
				SellMarket();

			_barsSinceLastTrade = 0;
			return;
		}

		var stopDist = candle.ClosePrice * MinStopLevel / 100m;
		var trailDist = candle.ClosePrice * TrailingStep / 100m;

		if (_stopPrice == null)
		{
			if (Position > 0)
				_stopPrice = candle.ClosePrice - stopDist;
			else
				_stopPrice = candle.ClosePrice + stopDist;

			return;
		}

		if (Position > 0)
		{
			var newStop = candle.ClosePrice - stopDist;
			if (newStop - _stopPrice >= trailDist)
				_stopPrice = newStop;

			if (candle.LowPrice <= _stopPrice)
			{
				SellMarket();
				_barsSinceLastTrade = 0;
			}
		}
		else if (Position < 0)
		{
			var newStop = candle.ClosePrice + stopDist;
			if (_stopPrice - newStop >= trailDist)
				_stopPrice = newStop;

			if (candle.HighPrice >= _stopPrice)
			{
				BuyMarket();
				_barsSinceLastTrade = 0;
			}
		}
	}

	private Sides GetRandomSide(ICandleMessage candle, decimal smaValue)
	{
		var rnd = (int)(Math.Abs(candle.OpenTime.Ticks) % 5);
		if (candle.ClosePrice > smaValue)
			return rnd == 0 ? Sides.Sell : Sides.Buy;
		else
			return rnd == 1 ? Sides.Buy : Sides.Sell;
	}
}