View on GitHub

Farhad Crab Strategy

Overview

The Farhad Crab Strategy is a StockSharp high-level port of the MetaTrader expert advisor FarhadCrab1.mq4. The original EA is a fast scalping system designed for the M1 timeframe on GBP/JPY, GBP/USD, and EUR/USD. This conversion recreates the trading logic in C# by combining intraday moving-average filters with a daily trend safety net and automated exit management.

The strategy analyses the current timeframe through a 9-period EMA calculated on the typical price and a 9-period SMA calculated on the candle open. At the same time it keeps track of a 55-period smoothed moving average (SMMA) built from daily candles. Whenever the short-term filters show sufficient momentum to the upside while no position is open, a long trade is triggered. Conversely, when the intraday high remains under the SMA of opens, a short trade is opened. The daily SMMA acts as a protective overlay: crossing the price from below forces all long trades to exit, and crossing from above closes short positions.

Exit management reproduces the original EA's behaviour with configurable take-profit levels in pips and independent trailing stops for long and short positions. The trailing logic follows the MetaTrader implementation by moving the stop only after the market advances by the configured distance. The strategy closes positions via market orders rather than pending stop orders, making it compatible with the high-level API event flow.

Key Features

  • Indicator set identical to the EA – 9-period EMA on typical price, 9-period SMA on opens, and a daily 55-period SMMA for trend direction.
  • Multi-timeframe data handling – subscribes to the trading timeframe and to daily candles simultaneously, letting StockSharp compute the required indicators without manual buffering.
  • Configurable exits – symmetric take-profit distances (long/short) and trailing stops expressed in pips, just like the original external inputs.
  • Daily safety switch – replicates the EA's rule that closes longs when the daily SMMA moves above the daily close and shorts when it moves below.
  • Built-in protection – calls StartProtection() once at startup to guard positions according to framework best practices.

Parameters

Parameter Description Default
OrderVolume Trade volume applied to new market orders. 0.1
LongTakeProfitPips Take-profit distance for long positions, measured in pips. 10
ShortTakeProfitPips Take-profit distance for short positions, measured in pips. 10
LongTrailingStopPips Trailing stop distance for long trades. Trailing is disabled when set to zero. 8
ShortTrailingStopPips Trailing stop distance for short trades. Trailing is disabled when set to zero. 8
DailyMaPeriod Length of the daily smoothed moving average used for protective exits. 55
CandleType Primary timeframe that drives the strategy calculations. Defaults to 1 minute candles. 1m

All parameters are exposed through StrategyParam<T> and marked as optimisable where it makes sense, so they can be tuned via the StockSharp optimiser.

Trading Rules

  1. Long entries: When the current candle low stays above the 9-period EMA of the typical price and no position is active, open a long trade.
  2. Short entries: When the current candle high remains below the 9-period SMA of the open price and no position is active, open a short trade.
  3. Daily protective exit (long): Close any long position if the daily SMMA moves above the daily close while it was previously below the previous close.
  4. Daily protective exit (short): Close any short position if the daily SMMA moves below the daily close while it was previously above the previous close.
  5. Take-profit: Close the position once the configured pip target is reached.
  6. Trailing stop: After a position gains the trailing distance, lock in profits by monitoring the retracement distance and exit when the price moves back by that amount.

Implementation Notes

  • The code relies exclusively on high-level SubscribeCandles().Bind(...) calls, eliminating any manual indicator buffers and staying within the project's guidelines.
  • Pips are calculated from the instrument's PriceStep with the usual MetaTrader-style adjustment for 3- and 5-digit quotes. This keeps the behaviour consistent with the EA's point-based parameters.
  • Stop-loss and take-profit management is performed internally by closing positions when conditions are met, rather than registering limit/stop orders. This approach matches the instantaneous exits found in the original script while remaining compatible with asynchronous order execution in StockSharp.
  • The strategy resets its state inside OnReseted, ensuring that optimisation runs and repeated launches start from a clean slate.

Usage Tips

  • The original EA was tailored for highly volatile GBP and EUR pairs on the M1 timeframe. Results may differ significantly even when the timeframe and instruments are the same; parameters are provided to explore different volatility profiles.
  • Because the system keeps only one position at a time, it is suitable for straightforward backtesting and live execution without complex position pyramiding.
  • Trailing stops become more effective on instruments with smooth trends. On ranging markets consider reducing the trailing distance or relying solely on take-profit exits.
  • The daily SMMA exit serves as the primary risk control. For swing-oriented setups you can increase DailyMaPeriod to make the long-term filter less reactive.

Testable indicator and exit rules: For each finished M1 candle the typical price is (high+low+close)/3. The 9-period EMA of typical prices is seeded with their first nine-value SMA and thereafter updated by EMA=(8*EMA+2*typical)/10. The separate 9-period SMA uses candle opens. With no position or pending order, buy if low > EMA, otherwise sell if high < SMA(Open). The daily SMMA(55) is seeded from 55 completed daily closes and updated as (54*SMMA+close)/55; its crossing of the daily close closes the adverse position. Take-profit and trailing-stop exits use finished M1 candle extremes and the actual entry fill price, not the signal close. Trailing levels become active only after the position moves by the configured trailing distance and are checked against subsequent bars, without inferring the unknown intrabar path. A reduced DailyMaPeriod is a separate mechanics test, not evidence that the 55-day default was covered. Backtests do not demonstrate future returns.

namespace StockSharp.Samples.Strategies;

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

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

/// <summary>
/// Farhad Crab uses EMA(9) on typical M1 prices, SMA(9) of M1 opens,
/// and daily SMMA protection with direction-specific take/trailing exits.
/// </summary>
public class FarhadCrabStrategy : Strategy
{
	private readonly StrategyParam<int> _dailyMaPeriod;
	private readonly StrategyParam<decimal> _orderVolume;
	private readonly StrategyParam<decimal> _longTakeProfitPips;
	private readonly StrategyParam<decimal> _shortTakeProfitPips;
	private readonly StrategyParam<decimal> _longTrailingStopPips;
	private readonly StrategyParam<decimal> _shortTrailingStopPips;
	private readonly StrategyParam<DataType> _candleType;

	private readonly Queue<decimal> _typicalSeed = new();
	private readonly Queue<decimal> _opens = new();
	private readonly Queue<decimal> _dailySeed = new();
	private decimal? _emaTypical;
	private decimal? _dailySmma;
	private decimal? _priorSmma;
	private decimal? _priorDailyClose;
	private decimal? _entryPrice;
	private decimal? _trailingLevel;
	private decimal _bestPrice;
	private Order _pendingOrder;
	private Order _entryOrder;

	public int DailyMaPeriod { get => _dailyMaPeriod.Value; set => _dailyMaPeriod.Value = value; }
	public decimal OrderVolume { get => _orderVolume.Value; set => _orderVolume.Value = value; }
	public decimal LongTakeProfitPips { get => _longTakeProfitPips.Value; set => _longTakeProfitPips.Value = value; }
	public decimal ShortTakeProfitPips { get => _shortTakeProfitPips.Value; set => _shortTakeProfitPips.Value = value; }
	public decimal LongTrailingStopPips { get => _longTrailingStopPips.Value; set => _longTrailingStopPips.Value = value; }
	public decimal ShortTrailingStopPips { get => _shortTrailingStopPips.Value; set => _shortTrailingStopPips.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public FarhadCrabStrategy()
	{
		_dailyMaPeriod = Param(nameof(DailyMaPeriod), 55).SetGreaterThanZero();
		_orderVolume = Param(nameof(OrderVolume), 0.1m).SetGreaterThanZero();
		_longTakeProfitPips = Param(nameof(LongTakeProfitPips), 10m).SetNotNegative();
		_shortTakeProfitPips = Param(nameof(ShortTakeProfitPips), 10m).SetNotNegative();
		_longTrailingStopPips = Param(nameof(LongTrailingStopPips), 8m).SetNotNegative();
		_shortTrailingStopPips = Param(nameof(ShortTrailingStopPips), 8m).SetNotNegative();
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame());
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType), (Security, TimeSpan.FromDays(1).TimeFrame())];

	protected override void OnReseted()
	{
		base.OnReseted();
		ClearState();
	}

	private void ClearState()
	{
		_typicalSeed.Clear();
		_opens.Clear();
		_dailySeed.Clear();
		_emaTypical = null;
		_dailySmma = null;
		_priorSmma = null;
		_priorDailyClose = null;
		_entryPrice = null;
		_trailingLevel = null;
		_bestPrice = 0m;
		_pendingOrder = null;
		_entryOrder = null;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		ClearState();
		if ((Security?.PriceStep ?? 0m) <= 0m)
			throw new InvalidOperationException("Farhad Crab requires a positive instrument PriceStep.");
		SubscribeCandles(TimeSpan.FromDays(1).TimeFrame()).Bind(ProcessDailyCandle).Start();
		SubscribeCandles(CandleType).Bind(ProcessCandle).Start();
	}

	private decimal PipSize()
	{
		var step = Security?.PriceStep ?? 0m;
		return Security?.Decimals is 3 or 5 ? step * 10m : step;
	}

	private bool Pending => _pendingOrder is not null &&
		_pendingOrder.State is not (OrderStates.Done or OrderStates.Failed);

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

		var priorSmma = _dailySmma;
		var priorClose = _priorDailyClose;
		if (_dailySmma is null)
		{
			_dailySeed.Enqueue(candle.ClosePrice);
			if (_dailySeed.Count == DailyMaPeriod)
				_dailySmma = _dailySeed.Average();
		}
		else
			_dailySmma = (_dailySmma.Value * (DailyMaPeriod - 1) + candle.ClosePrice) / DailyMaPeriod;

		_priorSmma = priorSmma;
		_priorDailyClose = candle.ClosePrice;

		if (Pending || Position == 0m || priorSmma is null || priorClose is null || _dailySmma is null)
			return;

		if (Position > 0m && priorSmma < priorClose && _dailySmma > candle.ClosePrice ||
			Position < 0m && priorSmma > priorClose && _dailySmma < candle.ClosePrice)
			Exit("Farhad Crab daily safety exit");
	}

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

		var typical = (candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 3m;
		if (_emaTypical is null)
		{
			_typicalSeed.Enqueue(typical);
			if (_typicalSeed.Count == 9)
				_emaTypical = _typicalSeed.Average();
		}
		else
			_emaTypical = (_emaTypical.Value * 8m + typical * 2m) / 10m;

		_opens.Enqueue(candle.OpenPrice);
		while (_opens.Count > 9)
			_opens.Dequeue();

		if (Pending)
			return;
		if (Position != 0m)
		{
			HandleRisk(candle);
			return;
		}
		if (_emaTypical is null || _opens.Count < 9)
			return;

		if (candle.LowPrice > _emaTypical.Value)
			Enter(Sides.Buy);
		else if (candle.HighPrice < _opens.Average())
			Enter(Sides.Sell);
	}

	private void Enter(Sides side)
	{
		var volume = OrderVolume;
		if (Security?.VolumeStep is decimal step && step > 0m)
			volume = decimal.Floor(volume / step) * step;
		if (Security?.MinVolume is decimal min && min > 0m)
			volume = Math.Max(min, volume);
		if (Security?.MaxVolume is decimal max && max > 0m)
			volume = Math.Min(max, volume);
		if (volume > 0m)
			Submit(side, volume, "Farhad Crab entry");
	}

	private void HandleRisk(ICandleMessage candle)
	{
		if (_entryPrice is not decimal entry)
			return;
		var positive = Position > 0m;
		var pip = PipSize();
		var target = (positive ? LongTakeProfitPips : ShortTakeProfitPips) * pip;
		if (target > 0m && (positive ? candle.HighPrice >= entry + target :
			candle.LowPrice <= entry - target))
		{
			Exit("Farhad Crab take profit");
			return;
		}

		if (_trailingLevel is decimal level && (positive ? candle.LowPrice <= level :
			candle.HighPrice >= level))
		{
			Exit("Farhad Crab trailing stop");
			return;
		}

		var distance = (positive ? LongTrailingStopPips : ShortTrailingStopPips) * pip;
		if (distance <= 0m)
			return;

		_bestPrice = positive ? Math.Max(_bestPrice, candle.HighPrice) :
			Math.Min(_bestPrice, candle.LowPrice);
		var move = positive ? _bestPrice - entry : entry - _bestPrice;
		if (move < distance)
			return;
		var next = positive ? _bestPrice - distance : _bestPrice + distance;
		if (_trailingLevel is null || (positive ? next > _trailingLevel.Value : next < _trailingLevel.Value))
			_trailingLevel = next;
	}

	private void Exit(string reason)
	{
		if (!Pending && Position != 0m)
			Submit(Position > 0m ? Sides.Sell : Sides.Buy, Math.Abs(Position), reason);
	}

	private void Submit(Sides side, decimal volume, string comment)
	{
		_pendingOrder = new Order
		{
			Security = Security,
			Portfolio = Portfolio,
			Type = OrderTypes.Market,
			Side = side,
			Volume = volume,
			Comment = comment,
		};
		if (comment == "Farhad Crab entry")
			_entryOrder = _pendingOrder;
		RegisterOrder(_pendingOrder);
	}

	protected override void OnOwnTradeReceived(MyTrade trade)
	{
		base.OnOwnTradeReceived(trade);
		if (trade?.Order == _entryOrder && trade.Trade is not null)
		{
			_entryPrice = trade.Trade.Price;
			_bestPrice = _entryPrice.Value;
			_trailingLevel = null;
		}
		else if (Position == 0m)
		{
			_entryPrice = null;
			_trailingLevel = null;
			_bestPrice = 0m;
		}
	}
}