Ver no GitHub

Estratégia do Oscilador de Ondas de Elliott

Esta estratégia aplica o Oscilador de Ondas de Elliott (EWO) nos fechamentos das velas. O EWO é calculado como a diferença entre uma Média Móvel Simples rápida e uma lenta (5 e 35 períodos por padrão). A lógica de trading busca pontos de inflexão no oscilador para capturar possíveis reversões de tendência.

Uma posição comprada é aberta quando o oscilador forma um fundo local e começa a subir. Uma posição vendida é aberta quando o oscilador forma um topo local e começa a cair. As posições existentes são invertidas de acordo. Take‑profit e stop‑loss opcionais baseados em percentual são suportados por meio de StartProtection.

Detalhes

  • Indicador: Oscilador de Ondas de Elliott = SMA(rápida) − SMA(lenta).
  • Critérios de entrada:
    • Comprado: o valor do oscilador estava caindo e depois vira para cima.
    • Vendido: o valor do oscilador estava subindo e depois vira para baixo.
  • Comprado/Vendido: Ambos.
  • Critérios de saída: A posição se inverte no sinal oposto ou sai via stop ou take‑profit.
  • Stops: Stop‑loss e take‑profit percentuais.
  • Filtros: Nenhum.
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>
/// Elliott Wave Oscillator based strategy.
/// Buys when the oscillator turns upward.
/// Sells when the oscillator turns downward.
/// </summary>
public class ElliottWaveOscillatorStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevEwo;
	private decimal _prevPrevEwo;
	private bool _isFirstValue;

	/// <summary>
	/// Fast moving average length.
	/// </summary>
	public int FastLength
	{
		get => _fastLength.Value;
		set => _fastLength.Value = value;
	}

	/// <summary>
	/// Slow moving average length.
	/// </summary>
	public int SlowLength
	{
		get => _slowLength.Value;
		set => _slowLength.Value = value;
	}

	/// <summary>
	/// Take profit percentage.
	/// </summary>
	public decimal TakeProfitPct
	{
		get => _takeProfitPct.Value;
		set => _takeProfitPct.Value = value;
	}

	/// <summary>
	/// Stop loss percentage.
	/// </summary>
	public decimal StopLossPct
	{
		get => _stopLossPct.Value;
		set => _stopLossPct.Value = value;
	}

	/// <summary>
	/// Candle type to use.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the strategy.
	/// </summary>
	public ElliottWaveOscillatorStrategy()
	{
		_fastLength = Param(nameof(FastLength), 5)
			.SetGreaterThanZero()
			.SetDisplay("Fast Length", "Length of the fast SMA", "Indicator")
			;

		_slowLength = Param(nameof(SlowLength), 35)
			.SetGreaterThanZero()
			.SetDisplay("Slow Length", "Length of the slow SMA", "Indicator")
			;

		_takeProfitPct = Param(nameof(TakeProfitPct), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Take Profit %", "Percentage take profit", "Risk")
			;

		_stopLossPct = Param(nameof(StopLossPct), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss %", "Percentage stop loss", "Risk")
			;

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevEwo = 0m;
		_prevPrevEwo = 0m;
		_isFirstValue = true;
	}

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

		var fastMa = new ExponentialMovingAverage { Length = FastLength };
		var slowMa = new ExponentialMovingAverage { Length = SlowLength };

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

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

		StartProtection(new Unit(TakeProfitPct / 100m, UnitTypes.Percent), new Unit(StopLossPct / 100m, UnitTypes.Percent));
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var ewoValue = fastValue - slowValue;

		if (_isFirstValue)
		{
			_prevEwo = ewoValue;
			_prevPrevEwo = ewoValue;
			_isFirstValue = false;
			return;
		}

		if (_prevEwo < _prevPrevEwo && ewoValue > _prevEwo)
		{
			// Oscillator turns upward - open long
			if (Position <= 0)
				BuyMarket();
		}
		else if (_prevEwo > _prevPrevEwo && ewoValue < _prevEwo)
		{
			// Oscillator turns downward - open short
			if (Position >= 0)
				SellMarket();
		}

		_prevPrevEwo = _prevEwo;
		_prevEwo = ewoValue;
	}
}