Ver no GitHub

Estratégia Fxscalper

Estratégia de scalping de rompimento de Bandas de Bollinger traduzida do especialista MQL4 "fxscalper". A estratégia se inscreve em dados de velas e Bandas de Bollinger. Quando o preço de fechamento rompe acima da banda superior abre uma posição comprada; quando o preço de fechamento rompe abaixo da banda inferior abre uma posição vendida. As posições são protegidas por níveis de stop loss e take profit.

Detalhes

  • Critérios de entrada:
    • Comprado: Close > Upper Band
    • Vendido: Close < Lower Band
  • Comprado/Vendido: Ambos
  • Critérios de saída: Sinal oposto ou stops protetores
  • Stops: Stop loss e take profit
  • Valores padrão:
    • CandleType = TimeSpan.FromMinutes(5).TimeFrame()
    • BollingerPeriod = 20
    • BollingerDeviation = 2
    • StopLoss = 200m
    • TakeProfit = 150m
  • Filtros:
    • Categoria: Bollinger Bands
    • Direção: Ambos
    • Indicadores: Bollinger Bands
    • Stops: Sim
    • Complexidade: Básico
    • Período: Intradiário
    • Sazonalidade: Não
    • Redes neurais: Não
    • Divergência: Não
    • Nível de risco: Médio
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>
/// Bollinger Band breakout scalping strategy.
/// </summary>
public class FxscalperStrategy : Strategy
{
	private readonly StrategyParam<int> _bollingerPeriod;
	private readonly StrategyParam<decimal> _bollingerDeviation;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;

	public int BollingerPeriod { get => _bollingerPeriod.Value; set => _bollingerPeriod.Value = value; }
	public decimal BollingerDeviation { get => _bollingerDeviation.Value; set => _bollingerDeviation.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public FxscalperStrategy()
	{
		_bollingerPeriod = Param(nameof(BollingerPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("BB Period", "Bollinger Bands length", "Indicators");

		_bollingerDeviation = Param(nameof(BollingerDeviation), 2m)
			.SetGreaterThanZero()
			.SetDisplay("BB Width", "Bollinger Bands width", "Indicators");

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

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

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

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

		var bollinger = new BollingerBands { Length = BollingerPeriod, Width = BollingerDeviation };

		var subscription = SubscribeCandles(CandleType);
		subscription.BindEx(bollinger, ProcessCandle).Start();

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

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

		var bb = (BollingerBandsValue)bbValue;
		if (bb.UpBand is not decimal upper ||
			bb.LowBand is not decimal lower ||
			bb.MovingAverage is not decimal middle)
			return;

		if (candle.ClosePrice > upper && Position <= 0)
		{
			BuyMarket();
			_entryPrice = candle.ClosePrice;
		}
		else if (candle.ClosePrice < lower && Position >= 0)
		{
			SellMarket();
			_entryPrice = candle.ClosePrice;
		}

		// Mean reversion exit: close when price returns to middle band
		if (Position > 0 && candle.ClosePrice <= middle)
			SellMarket();
		else if (Position < 0 && candle.ClosePrice >= middle)
			BuyMarket();
	}
}