Ver no GitHub

Estratégia Brandy v1.2 (C#)

Visão geral

A Estratégia Brandy v1.2 é uma conversão direta do consultor especialista MetaTrader 4 "Brandy_v1_2.mq4" na estrutura de estratégia de alto nível StockSharp. O sistema avalia um par de médias móveis simples deslocadas (SMAs) calculadas sobre o preço de fechamento da série de velas configurada. Novas posições são abertas apenas quando os SMAs de longo e curto prazo mostram impulso sincronizado na mesma direção, enquanto as negociações existentes são gerenciadas usando reversões de inclinação, níveis de stop-loss fixos e um módulo opcional de trailing stop.

O script MQL original foi executado exatamente uma vez por barra concluída. Esta porta processa StockSharp velas finalizadas da mesma maneira, garantindo que todas as decisões de negociação sejam baseadas em dados fechados, sem depender de barras parcialmente formadas.

Lógica de negociação

  1. Preparação de indicadores
    • Dois SMAs são calculados: uma linha de base mais longa (LongPeriod) e uma linha de confirmação mais curta (ShortPeriod).
    • Cada média é acessada duas vezes: o valor da barra anterior (shift = 1) e outro valor deslocado por LongShift/ShortShift barras respectivamente. Isso reproduz as chamadas iMA(..., shift) presentes no EA original.
  2. Regras de entrada
    • Compre quando o valor da barra anterior de ambos os SMAs for maior do que suas contrapartes deslocadas (ambas as inclinações apontando para cima) e nenhuma posição estiver aberta.
    • Venda quando o valor da barra anterior de ambos os SMAs for inferior aos seus homólogos deslocados (ambas as inclinações apontando para baixo) e nenhuma posição estiver aberta.
    • Apenas uma posição pode estar ativa a qualquer momento, espelhando a verificação k == 0 na fonte MQL.
  3. Regras de saída
    • Reversão de inclinação: uma posição longa aberta é liquidada se a posição longa SMA cair (longPrev < longShifted), enquanto uma posição curta é coberta quando a posição longa SMA subir (longPrev > longShifted).
    • Stop-loss fixo: ao entrar, a estratégia armazena um nível de stop inicial compensado em StopLossPoints × PriceStep do preço de entrada. O stop é verificado em relação à faixa máxima/mínima da vela, aproximando-se do gerenciamento do nível de tick do consultor original.
    • Trailing stop: se TrailingStopPoints ≥ 100, a estratégia replica a lógica final (parâmetro ts). Quando o lucro flutuante excede a distância final, o stop é puxado para currentPrice ± trailingDistance, desde que o novo nível esteja mais próximo do preço do que o stop existente. Este comportamento corresponde às chamadas OrderModify no especialista MQL.

Parâmetros

Parâmetro Padrão Descrição
LongPeriod 70 Comprimento do SMA primário (p1 em MQL). Deve ser > 0.
LongShift 5 Deslocamento para trás aplicado à comparação longa SMA (s1). Pode ser zero.
ShortPeriod 20 Comprimento da confirmação SMA (p2). Deve ser > 0.
ShortShift 5 Mudança para trás para o SMA curto (s2). Pode ser zero.
StopLossPoints 50 Distância de parada fixa em etapas de preço (sl). Defina como 0 para desativar a parada brusca.
TrailingStopPoints 150 Distância final em etapas de preço (ts). O rastreamento é ativado somente quando o valor é ≥ 100, espelhando o limite original.
Volume 0,1 Volume do pedido usado para entradas (lots).
CandleType Período de 15 minutos Série de velas processadas pela estratégia (configurável pelo usuário).

Dependência da etapa de preço

Ambos os parâmetros de parada operam em pontos do instrumento. O método auxiliar os converte em deltas de preços absolutos por meio de Security.PriceStep. Se a fonte de dados não fornecer PriceStep, a estratégia volta para 0.0001 para que a lógica continue funcionando, embora com uma conversão aproximada. Sempre verifique os metadados do símbolo em StockSharp antes do uso ao vivo.

Gestão de risco

  • Parada brusca: armazenada internamente e validada em cada vela finalizada. Quando o preço viola o stop, a chamada SellMarket/BuyMarket correspondente fecha toda a posição.
  • Trailing stop: segue as condições exatas do EA original, movendo o stop somente quando o lucro atual excede a distância final e o stop existente ainda está mais longe que essa distância.
  • Posição única: o algoritmo nunca faz pirâmides; ele tem uma única posição longa, uma única posição curta ou é plano.

Notas de implementação

  • O estado (preço de entrada, nível de stop, históricos de SMA) é redefinido automaticamente em OnReseted(), garantindo backtests e reinicializações limpos.
  • Os históricos dos indicadores são armazenados em buffers rolantes curtos para reproduzir os deslocamentos iMA(..., shift) sem chamar GetValue().
  • Todos os comentários embutidos permanecem em inglês, conforme exigido pelas diretrizes do repositório.
  • Nenhuma contraparte Python é fornecida. Somente a implementação de alto nível C# é entregue em CS/BrandyV12Strategy.cs conforme solicitado.

Uso

  1. Coloque a estratégia em uma solução StockSharp, selecione o instrumento desejado e certifique-se de que os dados da vela correspondam ao período especificado por CandleType.
  2. Configure os parâmetros na UI ou por meio de código. Os padrões replicam os valores MT4 originais.
  3. Comece a estratégia. Ele assinará a série de velas, desenhará ambos os SMAs no gráfico e gerenciará as negociações automaticamente.

Isenção de responsabilidade: Esta porta destina-se a fins educacionais e de teste. Sempre valide o comportamento em sessões de negociação históricas e em papel antes de implantar em mercados reais.

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>
/// Trend-following strategy using displaced simple moving averages.
/// </summary>
public class BrandyV12Strategy : Strategy
{
	private readonly StrategyParam<int> _longPeriod;
	private readonly StrategyParam<int> _longShift;
	private readonly StrategyParam<int> _shortPeriod;
	private readonly StrategyParam<int> _shortShift;
	private readonly StrategyParam<decimal> _stopLossPoints;
	private readonly StrategyParam<decimal> _trailingStopPoints;
	private readonly StrategyParam<DataType> _candleType;

	private SimpleMovingAverage _longSma;
	private SimpleMovingAverage _shortSma;
	private readonly List<decimal> _longHistory = new();
	private readonly List<decimal> _shortHistory = new();
	private decimal? _entryPrice;
	private decimal? _stopPrice;

	/// <summary>
	/// Initializes a new instance of <see cref="BrandyV12Strategy"/>.
	/// </summary>
	public BrandyV12Strategy()
	{
		_longPeriod = Param(nameof(LongPeriod), 70)
			.SetGreaterThanZero()
			.SetDisplay("Long SMA Period", "Period for the longer moving average.", "Indicators")
			;

		_longShift = Param(nameof(LongShift), 5)
			.SetNotNegative()
			.SetDisplay("Long SMA Shift", "Backward shift applied to the longer SMA.", "Indicators")
			;

		_shortPeriod = Param(nameof(ShortPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Short SMA Period", "Period for the shorter moving average.", "Indicators")
			;

		_shortShift = Param(nameof(ShortShift), 5)
			.SetNotNegative()
			.SetDisplay("Short SMA Shift", "Backward shift applied to the shorter SMA.", "Indicators")
			;

		_stopLossPoints = Param(nameof(StopLossPoints), 50m)
			.SetNotNegative()
			.SetDisplay("Stop Loss (points)", "Initial stop-loss distance expressed in price steps.", "Risk")
			;

		_trailingStopPoints = Param(nameof(TrailingStopPoints), 150m)
			.SetNotNegative()
			.SetDisplay("Trailing Stop (points)", "Trailing stop distance in price steps. Activates when >= 100.", "Risk")
			;

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(2).TimeFrame())
			.SetDisplay("Candle Type", "Candle series processed by the strategy.", "General");
	}

	/// <summary>
	/// Period for the longer simple moving average.
	/// </summary>
	public int LongPeriod
	{
		get => _longPeriod.Value;
		set => _longPeriod.Value = value;
	}

	/// <summary>
	/// Backward shift used when evaluating the longer SMA.
	/// </summary>
	public int LongShift
	{
		get => _longShift.Value;
		set => _longShift.Value = value;
	}

	/// <summary>
	/// Period for the shorter simple moving average.
	/// </summary>
	public int ShortPeriod
	{
		get => _shortPeriod.Value;
		set => _shortPeriod.Value = value;
	}

	/// <summary>
	/// Backward shift used when evaluating the shorter SMA.
	/// </summary>
	public int ShortShift
	{
		get => _shortShift.Value;
		set => _shortShift.Value = value;
	}

	/// <summary>
	/// Stop-loss distance in points (price steps).
	/// </summary>
	public decimal StopLossPoints
	{
		get => _stopLossPoints.Value;
		set => _stopLossPoints.Value = value;
	}

	/// <summary>
	/// Trailing stop distance in points (price steps).
	/// Trailing activates only when the configured value is at least 100.
	/// </summary>
	public decimal TrailingStopPoints
	{
		get => _trailingStopPoints.Value;
		set => _trailingStopPoints.Value = value;
	}

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

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

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

		_longSma = null;
		_shortSma = null;
		_longHistory.Clear();
		_shortHistory.Clear();
		_entryPrice = null;
		_stopPrice = null;
	}

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

		_longSma = new SMA { Length = LongPeriod };
		_shortSma = new SMA { Length = ShortPeriod };

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

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

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

		if (_longSma?.IsFormed != true || _shortSma?.IsFormed != true)
			return;

		var longCapacity = Math.Max(LongShift, 1) + 2;
		var shortCapacity = Math.Max(ShortShift, 1) + 2;
		UpdateHistory(_longHistory, longValue, longCapacity);
		UpdateHistory(_shortHistory, shortValue, shortCapacity);

		if (!TryGetShiftedValue(_longHistory, 1, out var longPrev) ||
			!TryGetShiftedValue(_longHistory, LongShift, out var longShifted) ||
			!TryGetShiftedValue(_shortHistory, 1, out var shortPrev) ||
			!TryGetShiftedValue(_shortHistory, ShortShift, out var shortShifted))
		{
			return;
		}

		if (ManageExistingPosition(candle, longPrev, longShifted))
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (Position == 0)
		{
			var bullish = longPrev > longShifted && shortPrev > shortShifted;
			var bearish = longPrev < longShifted && shortPrev < shortShifted;

			if (bullish)
			{
				EnterLong(candle);
			}
			else if (bearish)
			{
				EnterShort(candle);
			}
		}
	}

	private bool ManageExistingPosition(ICandleMessage candle, decimal longPrev, decimal longShifted)
	{
		if (Position > 0)
		{
			if (longPrev < longShifted)
			{
				SellMarket(Position);
				ResetPositionState();
				return true;
			}

			if (UpdateLongStops(candle))
			{
				SellMarket(Position);
				ResetPositionState();
				return true;
			}
		}
		else if (Position < 0)
		{
			if (longPrev > longShifted)
			{
				BuyMarket(Math.Abs(Position));
				ResetPositionState();
				return true;
			}

			if (UpdateShortStops(candle))
			{
				BuyMarket(Math.Abs(Position));
				ResetPositionState();
				return true;
			}
		}

		return false;
	}

	private void EnterLong(ICandleMessage candle)
	{
		var volume = Volume;
		if (volume <= 0m)
			return;

		BuyMarket(volume);

		var step = GetPoint();
		var price = candle.ClosePrice;
		_entryPrice = price;

		_stopPrice = StopLossPoints > 0m ? price - StopLossPoints * step : null;
	}

	private void EnterShort(ICandleMessage candle)
	{
		var volume = Volume;
		if (volume <= 0m)
			return;

		SellMarket(volume);

		var step = GetPoint();
		var price = candle.ClosePrice;
		_entryPrice = price;

		_stopPrice = StopLossPoints > 0m ? price + StopLossPoints * step : null;
	}

	private bool UpdateLongStops(ICandleMessage candle)
	{
		if (_entryPrice is not decimal entry)
			return false;

		var step = GetPoint();
		if (step <= 0m)
			return false;

		if (_stopPrice is null && StopLossPoints > 0m)
		{
			_stopPrice = entry - StopLossPoints * step;
		}

		if (TrailingStopPoints >= 100m)
		{
			var trailingDistance = TrailingStopPoints * step;
			if (trailingDistance > 0m)
			{
				var currentPrice = candle.ClosePrice;
				if (currentPrice - entry > trailingDistance)
				{
					var newStop = currentPrice - trailingDistance;
					if (_stopPrice is not decimal existing || currentPrice - existing > trailingDistance)
					{
						_stopPrice = newStop;
					}
				}
			}
		}

		if (_stopPrice is not decimal stop)
			return false;

		return candle.LowPrice <= stop;
	}

	private bool UpdateShortStops(ICandleMessage candle)
	{
		if (_entryPrice is not decimal entry)
			return false;

		var step = GetPoint();
		if (step <= 0m)
			return false;

		if (_stopPrice is null && StopLossPoints > 0m)
		{
			_stopPrice = entry + StopLossPoints * step;
		}

		if (TrailingStopPoints >= 100m)
		{
			var trailingDistance = TrailingStopPoints * step;
			if (trailingDistance > 0m)
			{
				var currentPrice = candle.ClosePrice;
				if (entry - currentPrice > trailingDistance)
				{
					var newStop = currentPrice + trailingDistance;
					if (_stopPrice is not decimal existing || existing - currentPrice > trailingDistance)
					{
						_stopPrice = newStop;
					}
				}
			}
		}

		if (_stopPrice is not decimal stop)
			return false;

		return candle.HighPrice >= stop;
	}

	private void ResetPositionState()
	{
		_entryPrice = null;
		_stopPrice = null;
	}

	private static void UpdateHistory(List<decimal> history, decimal value, int capacity)
	{
		history.Add(value);
		if (history.Count > capacity)
		{
			history.RemoveAt(0);
		}
	}

	private static bool TryGetShiftedValue(List<decimal> history, int shift, out decimal value)
	{
		value = 0m;

		if (shift < 0)
			return false;

		var index = history.Count - 1 - shift;
		if (index < 0 || index >= history.Count)
			return false;

		value = history[index];
		return true;
	}

	private decimal GetPoint()
	{
		var step = Security?.PriceStep;
		if (step is decimal priceStep && priceStep > 0m)
			return priceStep;

		return 0.0001m;
	}
}