Ver en GitHub

Reco RSI Grid Strategy

Overview

This strategy reproduces the behaviour of the original MetaTrader "Reco" expert advisor using StockSharp's high level API. The algorithm opens an initial position based on the Relative Strength Index (RSI) and then places counter positions forming a grid. Distance between grid orders and their volume grow geometrically. All open positions are closed together when cumulative profit or loss reaches predefined thresholds.

Trading Logic

  • Initial signal – RSI exceeds the configured overbought or oversold zones. A short position is opened when RSI is above the sell level and a long position when it is below the buy level.
  • Grid expansion – after the first order the strategy watches price movement against the last trade. When price moves by a calculated distance, an opposite market order is sent. The distance increases by Distance Multiplier on each new step and can be limited by Max Distance and Min Distance.
  • Volume scaling – the size of every new order equals the initial Lot multiplied by Lot Multiplier raised to the count of already opened orders. Maximum and minimum volume limits are also supported.
  • Exit rules – if Use Close Profit is enabled, all positions are closed when the aggregated profit is greater than Profit First Order multiplied by Profit Multiplier for each additional order. If Use Close Lose is enabled, the same logic is applied to losses using Lose First Order and Lose Multiplier.

Parameters

Name Description
RsiPeriod RSI indicator period.
RsiSellZone RSI level that triggers a sell signal.
RsiBuyZone RSI level that triggers a buy signal.
StartDistance Initial distance from the last order expressed in points.
DistanceMultiplier Multiplier applied to the distance for each additional order.
MaxDistance Upper limit for distance growth (0 disables).
MinDistance Lower limit for distance growth (0 disables).
MaxOrders Maximum number of simultaneous open orders (0 means no limit).
Lot Base order volume.
LotMultiplier Multiplier for volume scaling.
MaxLot Maximum allowed volume per order (0 disables).
MinLot Minimum allowed volume per order (0 disables).
UseCloseProfit Enable closing all positions by profit target.
ProfitFirstOrder Profit target for the first order.
ProfitMultiplier Profit multiplier for subsequent orders.
UseCloseLose Enable closing all positions by loss threshold.
LoseFirstOrder Loss threshold for the first order.
LoseMultiplier Loss multiplier for subsequent orders.
PointMultiplier Multiplier applied to the security price step to calculate one point.
CandleType Type of candles used for indicator calculations.

Notes

  • The strategy works with market orders and assumes immediate execution.
  • Positions are netted: opening an opposite order may reduce or reverse the current position.
  • The strategy uses tabs for indentation and English comments as required by project 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>
/// RSI based grid strategy.
/// Opens the first trade when RSI reaches overbought/oversold zones
/// and then adds counter trades as price moves by configurable steps.
/// All positions are closed together on defined profit target.
/// </summary>
public class RecoRsiGridStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _rsiSellZone;
	private readonly StrategyParam<decimal> _rsiBuyZone;
	private readonly StrategyParam<decimal> _gridStep;
	private readonly StrategyParam<int> _maxOrders;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _lastOrderPrice;
	private bool _lastOrderIsBuy;
	private int _ordersTotal;
	private decimal _entryPrice;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public decimal RsiSellZone { get => _rsiSellZone.Value; set => _rsiSellZone.Value = value; }
	public decimal RsiBuyZone { get => _rsiBuyZone.Value; set => _rsiBuyZone.Value = value; }
	public decimal GridStep { get => _gridStep.Value; set => _gridStep.Value = value; }
	public int MaxOrders { get => _maxOrders.Value; set => _maxOrders.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public RecoRsiGridStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetDisplay("RSI Period", "RSI indicator period", "Signal");

		_rsiSellZone = Param(nameof(RsiSellZone), 70m)
			.SetDisplay("RSI Sell Zone", "RSI level to sell", "Signal");

		_rsiBuyZone = Param(nameof(RsiBuyZone), 30m)
			.SetDisplay("RSI Buy Zone", "RSI level to buy", "Signal");

		_gridStep = Param(nameof(GridStep), 200m)
			.SetDisplay("Grid Step", "Distance between grid orders", "Grid");

		_maxOrders = Param(nameof(MaxOrders), 5)
			.SetDisplay("Max Orders", "Maximum number of grid orders", "Grid");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_lastOrderPrice = 0;
		_lastOrderIsBuy = false;
		_ordersTotal = 0;
		_entryPrice = 0;
	}

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

		_lastOrderPrice = 0;
		_lastOrderIsBuy = false;
		_ordersTotal = 0;
		_entryPrice = 0;

		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;

		var price = candle.ClosePrice;

		// Check if we should close all on profit
		if (_ordersTotal > 0 && _entryPrice > 0)
		{
			var unrealized = Position > 0
				? price - _entryPrice
				: _entryPrice - price;

			if (unrealized > GridStep * 0.5m)
			{
				// Close all
				if (Position > 0)
					SellMarket();
				else if (Position < 0)
					BuyMarket();

				_ordersTotal = 0;
				_lastOrderPrice = 0;
				_entryPrice = 0;
				return;
			}
		}

		var signal = GetSignal(price, rsiValue);

		if (signal > 0 && Position <= 0)
		{
			BuyMarket();
			_lastOrderIsBuy = true;
			_lastOrderPrice = price;
			_entryPrice = price;
			_ordersTotal++;
		}
		else if (signal < 0 && Position >= 0)
		{
			SellMarket();
			_lastOrderIsBuy = false;
			_lastOrderPrice = price;
			_entryPrice = price;
			_ordersTotal++;
		}
	}

	private int GetSignal(decimal price, decimal rsiValue)
	{
		if (_ordersTotal == 0)
		{
			if (rsiValue >= RsiSellZone)
				return -1;
			if (rsiValue <= RsiBuyZone)
				return 1;
			return 0;
		}

		if (MaxOrders > 0 && _ordersTotal >= MaxOrders)
		{
			// Reset grid when max orders reached
			_ordersTotal = 0;
			_lastOrderPrice = 0;
			return 0;
		}

		// Add counter-trend orders at grid steps
		if (_lastOrderIsBuy && price <= _lastOrderPrice - GridStep)
			return 1;
		else if (!_lastOrderIsBuy && price >= _lastOrderPrice + GridStep)
			return -1;

		return 0;
	}
}