Ver no GitHub

Estratégia Fracture

Fracture combina rompimentos de fractais com médias móveis suavizadas e ADX para operar tanto em mercados laterais quanto em tendência.

Detalhes

  • Critérios de entrada: Se o ADX estiver abaixo do limiar, entrar comprado acima do último fractal de alta ou vendido abaixo do último fractal de baixa quando o preço também estiver acima/abaixo da SMMA rápida. No regime de tendência (SMMA rápida acima/abaixo das mais lentas), entrar na direção da tendência quando o preço cruzar a SMMA rápida.
  • Comprado/Vendido: Ambos.
  • Critérios de saída: Fechar posição quando o lucro superar o ATR multiplicado por MinProfit.
  • Stops: Alvo de lucro baseado em ATR.
  • Valores padrão:
    • CandleType = TimeSpan.FromMinutes(1)
    • AtrPeriod = 14
    • AdxPeriod = 22
    • AdxLine = 40
    • Ma1Period = 5
    • Ma2Period = 9
    • Ma3Period = 22
    • RangingMultiplier = 0.5
    • MinProfit = 1
  • Filtros:
    • Categoria: Rompimento
    • Direção: Comprado & Vendido
    • Indicadores: Fractal, SMMA, ATR, ADX
    • Stops: Sim
    • Complexidade: Intermediário
    • Período: Qualquer
    • 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>
/// EMA triple crossover strategy.
/// </summary>
public class FractureStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _midPeriod;
	private readonly StrategyParam<int> _slowPeriod;

	private decimal _prevFast;
	private decimal _prevMid;
	private bool _hasPrev;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int MidPeriod { get => _midPeriod.Value; set => _midPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }

	public FractureStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle", "Candle type", "General");
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast", "Fast EMA period", "EMA");
		_midPeriod = Param(nameof(MidPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Mid", "Mid EMA period", "EMA");
		_slowPeriod = Param(nameof(SlowPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("Slow", "Slow EMA period", "EMA");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevMid = 0;
		_hasPrev = false;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var mid = new ExponentialMovingAverage { Length = MidPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, mid, slow, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevMid = midVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevMid && fastVal > midVal;
		var crossDown = _prevFast >= _prevMid && fastVal < midVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevMid = midVal;
	}
}