GitHub で見る

Levels With Trail Strategy

Converted from MQL script levels_with_trail.mq4. The strategy opens trades when price crosses a specified level and can trail the stop loss.

How it works

  • Subscribes to candles of the chosen timeframe.
  • When there is no open position and the closing price is above Level Price, it buys; if the price is below, it sells.
  • If Trail Stop is enabled, the stop loss follows the price when the position is profitable.
  • Positions are closed when the stop loss, take profit, or an opposite breakout signal is triggered.

Parameters

  • Stop Loss – stop loss size in price units.
  • Take Profit – take profit size in price units.
  • Level Price – breakout level to watch.
  • Trail Stop – enable or disable the trailing stop loss.
  • Candle Type – candle timeframe used for analysis.
using System;
using System.Collections.Generic;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy trading price level breakouts with trailing stop loss.
/// Uses SMA as dynamic level, enters on breakout, trails stop on winning positions.
/// </summary>
public class LevelsWithTrailStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<decimal> _trailPct;

	private decimal _entryPrice;
	private decimal _bestPrice;
	private decimal _prevPrice;
	private decimal _prevMa;
	private bool _hasPrev;

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

	public int MaPeriod
	{
		get => _maPeriod.Value;
		set => _maPeriod.Value = value;
	}

	public decimal TrailPct
	{
		get => _trailPct.Value;
		set => _trailPct.Value = value;
	}

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

		_maPeriod = Param(nameof(MaPeriod), 50)
			.SetDisplay("MA Period", "Moving average period for level", "Parameters");

		_trailPct = Param(nameof(TrailPct), 1m)
			.SetDisplay("Trail %", "Trailing stop percent", "Risk");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_entryPrice = 0;
		_bestPrice = 0;
		_prevPrice = 0;
		_prevMa = 0;
		_hasPrev = false;
	}

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

		_entryPrice = 0;
		_bestPrice = 0;
		_prevPrice = 0;
		_prevMa = 0;
		_hasPrev = false;

		var ma = new ExponentialMovingAverage { Length = MaPeriod };

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

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

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

		var price = candle.ClosePrice;

		// Trailing stop management
		if (Position > 0)
		{
			if (price > _bestPrice)
				_bestPrice = price;

			var stopLevel = _bestPrice * (1 - TrailPct / 100m);
			if (price <= stopLevel)
			{
				SellMarket();
				_entryPrice = 0;
			}
		}
		else if (Position < 0)
		{
			if (price < _bestPrice)
				_bestPrice = price;

			var stopLevel = _bestPrice * (1 + TrailPct / 100m);
			if (price >= stopLevel)
			{
				BuyMarket();
				_entryPrice = 0;
			}
		}

		if (!_hasPrev)
		{
			_prevPrice = price;
			_prevMa = maValue;
			_hasPrev = true;
			return;
		}

		// Entry: price crosses above MA
		if (_prevPrice < _prevMa && price >= maValue && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
			_entryPrice = price;
			_bestPrice = price;
		}
		// Entry: price crosses below MA
		else if (_prevPrice > _prevMa && price <= maValue && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
			_entryPrice = price;
			_bestPrice = price;
		}

		_prevPrice = price;
		_prevMa = maValue;
	}
}