Ver no GitHub

Lucky Code Strategy

Lucky Code is a short-term breakout scalper converted from the original MetaTrader "Lucky_code" expert advisor. The strategy watches the spread extremes and reacts when the best ask jumps above or the best bid falls below the previous quote by a configurable distance. All trades are closed aggressively: profits are taken immediately once price ticks favorably, while losses are cut when an adverse excursion breaches a protective limit.

Data and execution

  • Market data: requires a steady stream of Level 1 quotes to read the latest best bid and ask values.
  • Order types: uses market orders for every entry and exit to mirror the tick-based execution of the MQL version.
  • Position mode: supports both netting and hedging accounts. Multiple fills accumulate into a single net position that is managed as a block.

Parameters

  • Shift points – minimum number of points (pips) between consecutive quotes that unlocks a new entry. Higher values reduce trade frequency and noise sensitivity.
  • Limit points – maximum adverse distance allowed before positions are force-closed. The value is converted into price units with the instrument tick size.

Trading logic

  1. Initialization
    • Converts point-based parameters into real price offsets using the security tick size.
    • Subscribes to Level 1 data and resets the internal buffers for the last seen bid and ask.
  2. Entry rules
    • When the best ask advances by at least the configured shift above the previous ask, the strategy opens a short position (matching the original EA behavior that sells after upward spikes).
    • When the best bid drops by at least the same shift under the previous bid, the strategy opens a long position to capture the rebound.
  3. Volume sizing
    • Starts from the strategy Volume property.
    • If the portfolio value is available, the size is increased to round(Equity / 10,000, 1) lots, emulating the MetaTrader margin-based sizing.
  4. Exit rules
    • Long exposure is closed immediately once the bid exceeds the average entry price or the ask moves down by the configured loss limit.
    • Short exposure is closed once the ask falls below the entry price or the bid rises above it by the loss limit.

Implementation notes

  • The strategy reacts on every quote update, so consider throttling noisy feeds or increasing the shift parameter in production environments.
  • Because market orders are used for both opening and closing trades, ensure sufficient liquidity to avoid slippage spikes during fast quote jumps.
  • Additional portfolio-level risk controls (daily stop, maximum drawdown, etc.) are recommended when running the strategy live.
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;

using StockSharp.Algo;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Momentum strategy that opens trades when candle price jumps reach a configurable distance and manages exits with profit and drawdown filters.
/// </summary>
public class LuckyCodeStrategy : Strategy
{
	private readonly StrategyParam<int> _shiftPoints;
	private readonly StrategyParam<int> _limitPoints;

	private decimal? _previousClose;
	private decimal _shiftThreshold;
	private decimal _limitThreshold;
	private decimal _entryPrice;
	private bool _thresholdsReady;
	private int _holdBars;

	/// <summary>
	/// Minimum price movement in points required before opening a new trade.
	/// </summary>
	public int ShiftPoints
	{
		get => _shiftPoints.Value;
		set => _shiftPoints.Value = value;
	}

	/// <summary>
	/// Maximum adverse excursion in points tolerated before forcing an exit.
	/// </summary>
	public int LimitPoints
	{
		get => _limitPoints.Value;
		set => _limitPoints.Value = value;
	}

	/// <summary>
	/// Initializes strategy parameters.
	/// </summary>
	public LuckyCodeStrategy()
	{
		_shiftPoints = Param(nameof(ShiftPoints), 3)
			.SetGreaterThanZero()
			.SetDisplay("Shift points", "Minimum price jump required to trigger entries", "Trading")

			.SetOptimize(1, 20, 1);

		_limitPoints = Param(nameof(LimitPoints), 18)
			.SetGreaterThanZero()
			.SetDisplay("Limit points", "Maximum number of points allowed against the position", "Risk management")

			.SetOptimize(5, 100, 5);
	}

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

		_previousClose = null;
		_shiftThreshold = 0m;
		_limitThreshold = 0m;
		_entryPrice = 0m;
		_thresholdsReady = false;
		_holdBars = 0;
	}

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

		// Subscribe to candles and process each finished candle.
		var tf = TimeSpan.FromMinutes(5).TimeFrame();

		SubscribeCandles(tf)
			.Bind(ProcessCandle)
			.Start();
	}

	private void EnsureThresholds(decimal price)
	{
		if (_thresholdsReady)
			return;

		if (price <= 0m)
			return;

		// Use percentage of price. ShiftPoints=3 means 3% shift, LimitPoints=18 means 18% limit.
		_shiftThreshold = price * ShiftPoints * 0.01m;
		_limitThreshold = price * LimitPoints * 0.01m;
		_thresholdsReady = true;
	}

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

		var close = candle.ClosePrice;

		EnsureThresholds(close);

		if (!_thresholdsReady)
			return;

		// Count hold bars for position management.
		if (Position != 0)
			_holdBars++;

		if (_previousClose is decimal prevClose)
		{
			var delta = close - prevClose;

			// Only enter if flat.
			if (Position == 0)
			{
				// Price dropped sharply -> buy on expected rebound.
				if (-delta >= _shiftThreshold)
				{
					BuyMarket();
					_entryPrice = close;
					_holdBars = 0;
					LogInfo($"Buy triggered by fast price drop. Price={close:0.#####}");
				}
				// Price rose sharply -> sell on expected reversal.
				else if (delta >= _shiftThreshold)
				{
					SellMarket();
					_entryPrice = close;
					_holdBars = 0;
					LogInfo($"Sell triggered by fast price rise. Price={close:0.#####}");
				}
			}
		}

		_previousClose = close;

		TryClosePosition(close);
	}

	private void TryClosePosition(decimal currentPrice)
	{
		if (Position == 0)
			return;

		var avgPrice = _entryPrice;

		if (avgPrice <= 0m)
			return;

		// Minimum hold of 3 bars before checking exit.
		if (_holdBars < 3)
			return;

		// Use half of shift threshold as profit target.
		var profitTarget = _shiftThreshold * 0.5m;

		if (Position > 0)
		{
			// Close long on profit target or drawdown limit.
			if (currentPrice - avgPrice >= profitTarget)
			{
				SellMarket();
				_holdBars = 0;
				LogInfo($"Closed long on profit. Price={currentPrice:0.#####}");
			}
			else if (_limitThreshold > 0m && avgPrice - currentPrice >= _limitThreshold)
			{
				SellMarket();
				_holdBars = 0;
				LogInfo($"Closed long on drawdown limit. Price={currentPrice:0.#####}");
			}
		}
		else if (Position < 0)
		{
			// Close short on profit target or drawdown limit.
			if (avgPrice - currentPrice >= profitTarget)
			{
				BuyMarket();
				_holdBars = 0;
				LogInfo($"Closed short on profit. Price={currentPrice:0.#####}");
			}
			else if (_limitThreshold > 0m && currentPrice - avgPrice >= _limitThreshold)
			{
				BuyMarket();
				_holdBars = 0;
				LogInfo($"Closed short on drawdown limit. Price={currentPrice:0.#####}");
			}
		}
	}
}