GitHub で見る

Morse Code Strategy

Overview

The Morse Code Strategy replicates the original MetaTrader 5 expert that treats every finished candle as either a "dash" or a "dot". A bullish candle (close price greater than or equal to the open) is encoded as 1, while a bearish candle (close price less than or equal to the open) is encoded as 0. The strategy scans the latest sequence of completed candles and compares it to a user-selected binary mask. When the last candles match the configured sequence exactly, the strategy opens a position in the chosen direction and immediately attaches both a take-profit and a stop-loss order expressed in pips.

The implementation relies exclusively on high-level StockSharp APIs: candle subscriptions provide data, binding handles event delivery, and the built-in protection module manages exits. No custom collections or direct indicator value access are required, keeping the logic concise and robust.

Pattern logic

  • Candles are evaluated only after they are fully closed (CandleStates.Finished).
  • Each candle becomes a binary digit:
    • 1 – the candle is bullish or neutral (Close >= Open).
    • 0 – the candle is bearish or neutral (Close <= Open). Doji candles match both digits, exactly as in the original expert.
  • The mask is selected from the MorsePatternMasks enumeration. It contains every binary sequence from length 1 to length 5 that appeared in the MT5 version (for example 000, 1011, 11111).
  • The strategy keeps a rolling window of the most recent candles. When the newest window matches the mask, the entry signal fires.

This behaviour mirrors the MT5 implementation that called CopyRates and compared each bar with the pattern string character by character.

Trading workflow

  1. Subscribe to the configured candle type and wait until enough bars are accumulated to cover the mask length.
  2. For every completed candle:
    • Update the internal masks that classify the candle as bullish, bearish or neutral.
    • Ignore further checks until at least as many candles were processed as the mask requires.
    • If the latest candles match the selected mask exactly, check the desired direction.
    • Submit a market order in the direction of the signal (BuyMarket or SellMarket). When an opposite position exists, the strategy first closes it by increasing the order volume, reproducing the behaviour of the original expert adviser.
  3. StartProtection immediately attaches a stop-loss and a take-profit offset expressed in price units. Protective orders are handled by the StockSharp engine using market exits to avoid missed fills.

Parameters

Name Default Description
CandleType 5-minute candles (TimeSpan.FromMinutes(5).TimeFrame()) Data type used to build the Morse sequence.
Pattern _0 ("0") Binary mask to match against the most recent candles. Values come directly from MorsePatternMasks.
Direction Sides.Buy Whether to open a long (Buy) or short (Sell) position when the pattern appears.
TakeProfitPips 50 Distance from entry to take profit in pips. The strategy automatically adapts to 3- and 5-decimal forex quotes by multiplying the price step by ten.
StopLossPips 50 Distance from entry to stop loss in pips, using the same pip calculation as above.
Volume (strategy property) user-defined Order size in lots/contracts, equivalent to InpLot in the MT5 expert.

All parameters support the StockSharp parameter window, can be optimised, and can be changed before the strategy is started.

Risk management

  • StartProtection attaches both targets using price-based offsets derived from the pip settings. Exits are executed with market orders so that the behaviour matches the MT5 trade class that set stop-loss and take-profit values on position entry.
  • Because the strategy does not pyramid, a new trade is ignored while an existing position in the same direction is open. When the pattern appears while holding the opposite direction, the volume is automatically increased to flip the position.
  • Standard StockSharp logging reports every entry to the strategy journal.

Usage notes

  • The binary masks are intentionally short (up to five candles) to keep the logic faithful to the original idea. Consider combining multiple pattern masks through portfolio orchestration if a richer vocabulary is needed.
  • Pip conversion relies on the instrument price step. For exotic symbols with non-standard increments you can adjust TakeProfitPips and StopLossPips manually.
  • The strategy does not filter by time of day or volatility. You can wrap it inside a parent strategy that handles sessions or additional indicators if required.
  • When testing, ensure the Volume property matches the expected lot size. The StockSharp tester will reuse the same protections and order flow as the live mode.

Pattern reference

Examples of enumeration values:

  • _0"0" (single bearish candle)
  • _5"11" (two bullish candles in a row)
  • _20"0110" (bearish-bullish sequence forming a zig-zag)
  • _33"00011" (three bearish candles followed by two bullish ones)
  • _61"11111" (five consecutive bullish candles)

Any of the 62 masks can be selected from the parameter panel to reproduce the exact Morse code signature required by the trading plan.

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

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy that trades when a selected Morse code candle pattern appears.
/// </summary>
public class MorseCodeStrategy : Strategy
{
	/// <summary>
	/// Available Morse code style patterns where '1' is bullish and '0' is bearish.
	/// </summary>
	public enum MorsePatternMasks
	{
		_0 = 0,
		_1 = 1,
		_2 = 2,
		_3 = 3,
		_4 = 4,
		_5 = 5,
		_6 = 6,
		_7 = 7,
		_8 = 8,
		_9 = 9,
		_10 = 10,
		_11 = 11,
		_12 = 12,
		_13 = 13,
		_14 = 14,
		_15 = 15,
		_16 = 16,
		_17 = 17,
		_18 = 18,
		_19 = 19,
		_20 = 20,
		_21 = 21,
		_22 = 22,
		_23 = 23,
		_24 = 24,
		_25 = 25,
		_26 = 26,
		_27 = 27,
		_28 = 28,
		_29 = 29,
		_30 = 30,
		_31 = 31,
		_32 = 32,
		_33 = 33,
		_34 = 34,
		_35 = 35,
		_36 = 36,
		_37 = 37,
		_38 = 38,
		_39 = 39,
		_40 = 40,
		_41 = 41,
		_42 = 42,
		_43 = 43,
		_44 = 44,
		_45 = 45,
		_46 = 46,
		_47 = 47,
		_48 = 48,
		_49 = 49,
		_50 = 50,
		_51 = 51,
		_52 = 52,
		_53 = 53,
		_54 = 54,
		_55 = 55,
		_56 = 56,
		_57 = 57,
		_58 = 58,
		_59 = 59,
		_60 = 60,
		_61 = 61
	}

	private static readonly string[] PatternValues = new[]
	{
		"0",
		"1",
		"00",
		"01",
		"10",
		"11",
		"000",
		"001",
		"010",
		"011",
		"100",
		"101",
		"110",
		"111",
		"0000",
		"0001",
		"0010",
		"0011",
		"0100",
		"0101",
		"0110",
		"0111",
		"1000",
		"1001",
		"1010",
		"1011",
		"1100",
		"1101",
		"1110",
		"1111",
		"00000",
		"00000",
		"00010",
		"00011",
		"00100",
		"00101",
		"00111",
		"00111",
		"01000",
		"01001",
		"01010",
		"01011",
		"01100",
		"01101",
		"01110",
		"01111",
		"10000",
		"10001",
		"10010",
		"10011",
		"10100",
		"10101",
		"10110",
		"10111",
		"11000",
		"11001",
		"11010",
		"11011",
		"11100",
		"11101",
		"11110",
		"11111"
	};

	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<MorsePatternMasks> _patternMask;
	private readonly StrategyParam<Sides> _direction;
	private readonly StrategyParam<decimal> _takeProfitPips;
	private readonly StrategyParam<decimal> _stopLossPips;

	private string _patternText = string.Empty;
	private int _patternLength;
	private int _maskLimit;
	private int _bullMask;
	private int _bearMask;
	private int _processedBars;
	private decimal _pipSize;
	private decimal _takeProfitDistance;
	private decimal _stopLossDistance;

	/// <summary>
	/// Initializes a new instance of the <see cref="MorseCodeStrategy"/> class.
	/// </summary>
	public MorseCodeStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe used for candle analysis", "General");

		_patternMask = Param(nameof(Pattern), MorsePatternMasks._14)
			.SetDisplay("Pattern", "Morse code pattern where 1= bullish and 0 = bearish", "Pattern");

		_direction = Param(nameof(Direction), Sides.Buy)
			.SetDisplay("Direction", "Side to trade when the pattern appears", "Trading");

		_takeProfitPips = Param(nameof(TakeProfitPips), 50m)
			.SetGreaterThanZero()
			.SetDisplay("Take Profit (pips)", "Distance from entry to take profit in pips", "Risk Management");

		_stopLossPips = Param(nameof(StopLossPips), 50m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss (pips)", "Distance from entry to stop loss in pips", "Risk Management");
	}

	/// <summary>
	/// Candle type used for the strategy.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Selected Morse code pattern.
	/// </summary>
	public MorsePatternMasks Pattern
	{
		get => _patternMask.Value;
		set => _patternMask.Value = value;
	}

	/// <summary>
	/// Trade direction used when the pattern is detected.
	/// </summary>
	public Sides Direction
	{
		get => _direction.Value;
		set => _direction.Value = value;
	}

	/// <summary>
	/// Take profit distance expressed in pips.
	/// </summary>
	public decimal TakeProfitPips
	{
		get => _takeProfitPips.Value;
		set => _takeProfitPips.Value = value;
	}

	/// <summary>
	/// Stop loss distance expressed in pips.
	/// </summary>
	public decimal StopLossPips
	{
		get => _stopLossPips.Value;
		set => _stopLossPips.Value = value;
	}

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

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

		_patternText = string.Empty;
		_patternLength = 0;
		_maskLimit = 0;
		_bullMask = 0;
		_bearMask = 0;
		_processedBars = 0;
		_pipSize = 0m;
		_takeProfitDistance = 0m;
		_stopLossDistance = 0m;
	}

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

		_patternText = GetPatternText(Pattern);
		_patternLength = _patternText.Length;
		if (_patternLength == 0)
			throw new InvalidOperationException("Pattern cannot be empty.");

		_maskLimit = (1 << _patternLength) - 1;
		_bullMask = 0;
		_bearMask = 0;
		_processedBars = 0;

		_pipSize = CalculatePipSize();
		_takeProfitDistance = TakeProfitPips * _pipSize;
		_stopLossDistance = StopLossPips * _pipSize;

		// Configure automatic take profit and stop loss handling
		StartProtection(
			takeProfit: new Unit(_takeProfitDistance, UnitTypes.Absolute),
			stopLoss: new Unit(_stopLossDistance, UnitTypes.Absolute),
			useMarketOrders: true);

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

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

	private void ProcessCandle(ICandleMessage candle)
	{
		// Only act on completed candles
		if (candle.State != CandleStates.Finished)
			return;

		UpdatePatternMasks(candle);

		// Wait until enough candles were processed to match the pattern
		if (_processedBars < _patternLength)
			return;

		if (!IsPatternMatched())
			return;

		var closePrice = candle.ClosePrice;

		if (Direction == Sides.Buy)
		{
			if (Position > 0m)
				return; // Already in a long position

			EnterLong(closePrice);
		}
		else
		{
			if (Position < 0m)
				return; // Already in a short position

			EnterShort(closePrice);
		}
	}

	private static string GetPatternText(MorsePatternMasks mask)
	{
		var index = (int)mask;
		if (index < 0 || index >= PatternValues.Length)
			throw new ArgumentOutOfRangeException(nameof(mask));

		return PatternValues[index];
	}

	private decimal CalculatePipSize()
	{
		var step = Security?.PriceStep ?? 1m;
		if (step <= 0m)
			return 1m;

		var value = step;
		var digits = 0;
		while (value < 1m && digits < 10)
		{
			value *= 10m;
			digits++;
		}

		if (digits == 3 || digits == 5)
			step *= 10m;

		return step;
	}

	private void UpdatePatternMasks(ICandleMessage candle)
	{
		if (_patternLength == 0)
			return;

		var strictBull = candle.ClosePrice > candle.OpenPrice;
		var strictBear = candle.ClosePrice < candle.OpenPrice;

		_bullMask = ((_bullMask << 1) | (strictBull ? 1 : 0)) & _maskLimit;
		_bearMask = ((_bearMask << 1) | (strictBear ? 1 : 0)) & _maskLimit;

		if (_processedBars < _patternLength)
			_processedBars++;
	}

	private bool IsPatternMatched()
	{
		for (var i = 0; i < _patternLength; i++)
		{
			var expected = _patternText[i];
			var isStrictBull = ((_bullMask >> i) & 1) == 1;
			var isStrictBear = ((_bearMask >> i) & 1) == 1;

			if (expected == '1')
			{
				if (isStrictBear)
					return false; // Pattern expects bullish or neutral candle
			}
			else
			{
				if (isStrictBull)
					return false; // Pattern expects bearish or neutral candle
			}
		}

		return true;
	}

	private void EnterLong(decimal price)
	{
		BuyMarket();
		LogInfo($"Entered long position at price {price}.");
	}

	private void EnterShort(decimal price)
	{
		SellMarket();
		LogInfo($"Entered short position at price {price}.");
	}
}