Ver en GitHub

Stop Loss Take Profit Strategy

This port replicates the MetaTrader "Stop Loss Take Profit" expert advisor. The strategy flips a coin whenever the account is flat and opens a market order in the chosen direction. Each position immediately receives pip-based stop-loss and take-profit orders. If the stop is hit the next trade doubles its size (capped by the security's volume limits). A take-profit resets the volume back to the initial amount. The behaviour mirrors the original martingale-style position sizing while using StockSharp's high-level API.

Trading Logic

  • Market Data: Uses the CandleType parameter (default 1-minute time frame) to drive decision points.
  • Entry Rules:
    • When Position == 0 and no entry order is pending, the strategy generates a pseudo-random boolean.
    • true opens a long position with BuyMarket(volume); false opens a short with SellMarket(volume).
  • Exit Rules:
    • Protective stop-loss and take-profit orders are placed as soon as the entry fill is received.
    • A stop exit doubles the size for the next trade, while a take-profit resets it.
    • If either stop or take-profit distance is set to 0, the respective protective order is skipped.
  • Money Management:
    • InitialVolume defines the base order size.
    • After a losing trade the size is doubled but clipped to Security.MaxVolume when that value is available.
    • Volume is normalised to the instrument's VolumeStep, MinVolume and MaxVolume so orders remain valid.
  • Pip Handling:
    • By default the strategy infers a pip from the instrument's PriceStep and Decimals (5-digit FX symbols map to 0.0001).
    • Set PipSize to a positive value to override the automatic pip size detection.

Parameters

Name Default Description
CandleType 1-minute candles Time frame used to trigger coin flips and entries.
StopLossPips 1 Stop-loss distance expressed in pips. 0 disables the stop.
TakeProfitPips 1 Take-profit distance expressed in pips. 0 disables the take-profit.
InitialVolume 0.01 Starting trade volume. Doubled after stop-loss events and reset after wins.
PipSize 0 (auto) Optional pip size override in absolute price units.

Usage Notes

  • Works on both long and short sides and is intentionally direction-neutral.
  • Protective orders are cancelled whenever the position is closed to avoid stale orders.
  • The random generator is seeded with Environment.TickCount, meaning each session produces different trade sequences.
  • Suitable for demonstrating risk layering and martingale behaviour rather than for production trading without further risk controls.
using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Random direction strategy with pip-based stop loss and take profit that doubles the volume after losses.
/// </summary>
public class StopLossTakeProfitStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<decimal> _stopLossDistance;
	private readonly StrategyParam<decimal> _takeProfitDistance;
	private readonly StrategyParam<decimal> _initialVolume;

	private decimal _currentVolume;
	private decimal _entryPrice;
	private int _tradeCount;

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public decimal StopLossDistance
	{
		get => _stopLossDistance.Value;
		set => _stopLossDistance.Value = value;
	}

	public decimal TakeProfitDistance
	{
		get => _takeProfitDistance.Value;
		set => _takeProfitDistance.Value = value;
	}

	public decimal InitialVolume
	{
		get => _initialVolume.Value;
		set => _initialVolume.Value = value;
	}

	public StopLossTakeProfitStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for evaluating entries", "General");

		_stopLossDistance = Param(nameof(StopLossDistance), 5m)
			.SetDisplay("Stop Loss Distance", "Stop loss distance in price units", "Risk");

		_takeProfitDistance = Param(nameof(TakeProfitDistance), 5m)
			.SetDisplay("Take Profit Distance", "Take profit distance in price units", "Risk");

		_initialVolume = Param(nameof(InitialVolume), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Initial Volume", "Starting order volume", "Risk");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_currentVolume = 0;
		_entryPrice = 0;
		_tradeCount = 0;
	}

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

		_currentVolume = InitialVolume;
		_entryPrice = 0m;

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

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

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

		// Check SL/TP for open position
		if (Position != 0 && _entryPrice > 0)
		{
			if (Position > 0)
			{
				var hitStop = StopLossDistance > 0 && candle.LowPrice <= _entryPrice - StopLossDistance;
				var hitTake = TakeProfitDistance > 0 && candle.HighPrice >= _entryPrice + TakeProfitDistance;

				if (hitStop)
				{
					SellMarket();
					HandleStopLoss();
					return;
				}

				if (hitTake)
				{
					SellMarket();
					HandleTakeProfit();
					return;
				}
			}
			else if (Position < 0)
			{
				var hitStop = StopLossDistance > 0 && candle.HighPrice >= _entryPrice + StopLossDistance;
				var hitTake = TakeProfitDistance > 0 && candle.LowPrice <= _entryPrice - TakeProfitDistance;

				if (hitStop)
				{
					BuyMarket();
					HandleStopLoss();
					return;
				}

				if (hitTake)
				{
					BuyMarket();
					HandleTakeProfit();
					return;
				}
			}
		}

		// Enter new position when flat
		if (Position == 0)
		{
			_tradeCount++;

			// Use candle direction as a deterministic signal
			if (candle.ClosePrice < candle.OpenPrice)
			{
				SellMarket();
			}
			else
			{
				BuyMarket();
			}

			_entryPrice = candle.ClosePrice;
		}
	}

	private void HandleStopLoss()
	{
		// Double volume on loss (martingale)
		_currentVolume *= 2m;

		var maxVol = Security?.MaxVolume;
		if (maxVol.HasValue && maxVol.Value > 0 && _currentVolume > maxVol.Value)
			_currentVolume = maxVol.Value;

		Volume = _currentVolume;
		_entryPrice = 0m;
	}

	private void HandleTakeProfit()
	{
		// Reset volume on profit
		_currentVolume = InitialVolume;
		Volume = _currentVolume;
		_entryPrice = 0m;
	}
}