Ver en GitHub

Limits Martin Strategy

This strategy places paired limit orders above and below the current market price. Each trade uses a configurable step distance and optional martingale position sizing to recover previous losses.

Parameters

  • Step – distance in pips between the market price and pending limit orders.
  • Stop Loss – protective stop size in pips for open positions.
  • Take Profit – target profit size in pips for open positions.
  • Use Martingale – enables volume increase after a losing trade.
  • Loss Limit – maximum number of consecutive losing trades before resetting volume.
  • Volume – initial order volume.
  • Use MegaLot – doubles the volume instead of adding the base volume when martingale is active.
  • Candle Type – candle data type used for processing.

Trading Logic

  1. When there is no open position or active order, the strategy places a buy limit order below the last close and a sell limit order above it, both at the specified Step distance.
  2. Upon execution of a position, the opposite pending order remains, allowing only one active position at a time.
  3. The position is closed when either the stop loss or take profit level is reached.
  4. After a losing trade, the position volume can be increased according to the martingale settings.

Notes

  • The strategy uses the high-level StockSharp API with the Bind approach for handling candle data.
  • All comments within the code are written in English to meet repository conventions.
using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Simple martingale strategy with RSI-based entries.
/// Buys when RSI is oversold, sells when overbought.
/// </summary>
public class LimitsMartinStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _oversold;
	private readonly StrategyParam<decimal> _overbought;
	private readonly StrategyParam<DataType> _candleType;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public decimal Oversold { get => _oversold.Value; set => _oversold.Value = value; }
	public decimal Overbought { get => _overbought.Value; set => _overbought.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public LimitsMartinStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "Period for RSI", "Indicators");

		_oversold = Param(nameof(Oversold), 30m)
			.SetDisplay("Oversold", "RSI oversold level", "Indicators");

		_overbought = Param(nameof(Overbought), 70m)
			.SetDisplay("Overbought", "RSI overbought level", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (rsiValue < Oversold && Position <= 0)
			BuyMarket();
		else if (rsiValue > Overbought && Position >= 0)
			SellMarket();
	}
}