Ver no GitHub

Amstell SL Averaging Strategy

Conversion of the MetaTrader expert advisor exp_Amstell-SL. The system opens both a long and a short immediately, adds new orders every time price moves against the most recent entry by a fixed number of points, and relies on virtual (software-managed) take-profit and stop-loss levels to exit each ticket individually.

Strategy Logic

  • Initial Entries: When the strategy starts and there are no open trades, it submits one market buy (at the ask) and one market sell (at the bid).
  • Pyramiding on Drawdown:
    • Long side: whenever the current ask is ReentryPoints (default 10 points) below the last long entry price, a new buy order of the same volume is sent.
    • Short side: whenever the current bid is ReentryPoints above the last short entry price, a new sell order of the same volume is opened.
  • Exit Rules (virtual management):
    • For every long ticket the strategy monitors the best bid and best ask. If the bid rises by TakeProfitPoints from the order price or the ask drops by StopLossPoints, the position is closed at market.
    • For every short ticket it checks whether the ask is lower by TakeProfitPoints or the bid is higher by StopLossPoints; in either case the sell order is covered at market.
  • Processing Order: Exits are evaluated before any new entries, replicating the MetaTrader script that stops further actions after closing a position on the current tick.

Parameters

  • TakeProfitPoints – distance (in price steps) used to close profitable positions. Default: 30.
  • StopLossPoints – distance (in price steps) for protective exits. Default: 30.
  • Volume – lot size for each newly opened order. Default: 0.01.
  • ReentryPoints – adverse movement (in price steps) required to stack an additional order on the corresponding side. Default: 10.

Additional Notes

  • The point value is derived from Security.PriceStep; if it is not provided by the exchange, a value of 1 is used.
  • The strategy can be simultaneously long and short because it tracks buy and sell tickets independently, matching the hedging-style behaviour of the original expert advisor.
  • Take-profit and stop-loss levels are executed virtually by market orders; they are not placed on the exchange order book.
  • Risk increases quickly when price trends strongly in one direction because additional orders are opened without reducing previous exposure.
  • Works best on symbols where the notion of "point" equals a minimal price increment, for example major forex pairs on MetaTrader-style pricing.
using System;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Amstell SL: Grid averaging strategy with ATR-based take profit and stop loss.
/// Adds positions on adverse moves and exits on profit/stop targets.
/// </summary>
public class AmstellSlStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _atrLength;
	private readonly StrategyParam<int> _emaLength;

	private decimal _entryPrice;
	private decimal _prevEma;
	private int _gridCount;
	private int _cooldown;

	public AmstellSlStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe.", "General");

		_atrLength = Param(nameof(AtrLength), 14)
			.SetDisplay("ATR Length", "ATR period.", "Indicators");

		_emaLength = Param(nameof(EmaLength), 50)
			.SetDisplay("EMA Length", "EMA trend filter.", "Indicators");
	}

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

	public int AtrLength
	{
		get => _atrLength.Value;
		set => _atrLength.Value = value;
	}

	public int EmaLength
	{
		get => _emaLength.Value;
		set => _emaLength.Value = value;
	}

	/// <inheritdoc />
	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_entryPrice = 0;
		_prevEma = 0;
		_gridCount = 0;
		_cooldown = 0;
	}

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

		_entryPrice = 0;
		_prevEma = 0;
		_gridCount = 0;
		_cooldown = 0;

		var atr = new AverageTrueRange { Length = AtrLength };
		var ema = new ExponentialMovingAverage { Length = EmaLength };

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

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

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

		if (atrVal <= 0 || _prevEma == 0)
		{
			_prevEma = emaVal;
			return;
		}

		if (_cooldown > 0)
		{
			_cooldown--;
			_prevEma = emaVal;
			if (Position == 0) return;
		}

		var close = candle.ClosePrice;

		// Position management with grid and stops
		if (Position > 0)
		{
			if (close >= _entryPrice + atrVal * 2.5m)
			{
				SellMarket();
				_entryPrice = 0;
				_gridCount = 0;
				_cooldown = 10;
			}
			else if (close <= _entryPrice - atrVal * 4m)
			{
				SellMarket();
				_entryPrice = 0;
				_gridCount = 0;
				_cooldown = 10;
			}
			else if (_gridCount < 1 && close <= _entryPrice - atrVal * 2m)
			{
				_entryPrice = (_entryPrice + close) / 2m;
				_gridCount++;
				BuyMarket();
			}
		}
		else if (Position < 0)
		{
			if (close <= _entryPrice - atrVal * 2.5m)
			{
				BuyMarket();
				_entryPrice = 0;
				_gridCount = 0;
				_cooldown = 10;
			}
			else if (close >= _entryPrice + atrVal * 4m)
			{
				BuyMarket();
				_entryPrice = 0;
				_gridCount = 0;
				_cooldown = 10;
			}
			else if (_gridCount < 1 && close >= _entryPrice + atrVal * 2m)
			{
				_entryPrice = (_entryPrice + close) / 2m;
				_gridCount++;
				SellMarket();
			}
		}

		// Entry on EMA trend
		if (Position == 0 && _cooldown == 0)
		{
			if (close > emaVal && emaVal > _prevEma)
			{
				_entryPrice = close;
				_gridCount = 0;
				BuyMarket();
			}
			else if (close < emaVal && emaVal < _prevEma)
			{
				_entryPrice = close;
				_gridCount = 0;
				SellMarket();
			}
		}

		_prevEma = emaVal;
	}
}