Ver en GitHub

Binario 31 Strategy

Breakout strategy converted from MetaTrader script binario_31. The algorithm builds two 144-period exponential moving averages calculated over the candle high and low prices, creating a dynamic channel. While the current price stays inside the channel the strategy prepares stop entries:

  • a buy stop placed above the EMA-high plus a configurable offset;
  • a sell stop placed below the EMA-low minus the same offset.

When price breaks through one of these levels a position is opened in the direction of the breakout. A protective stop is placed on the opposite side of the channel and a take profit target is calculated relative to the entry. An optional trailing stop can be enabled to protect profits.

Parameters

  • EMA Length – period for both high and low EMAs.
  • Pip Difference – distance from the EMA level to the breakout entry in price steps.
  • Take Profit – distance from entry to take profit in price steps.
  • Trailing Stop – trailing stop distance in price steps. Set to zero to disable.
  • Volume – order volume.
  • Candle Type – type of candles the strategy subscribes to.
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 channel breakout strategy.
/// Buys when price breaks above EMA + offset, sells when below EMA - offset.
/// Uses trailing stop and take profit for exits.
/// </summary>
public class Binario31Strategy : Strategy
{
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<decimal> _channelOffset;
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;

	public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
	public decimal ChannelOffset { get => _channelOffset.Value; set => _channelOffset.Value = value; }
	public decimal TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
	public decimal StopLossVal { get => _stopLoss.Value; set => _stopLoss.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Binario31Strategy()
	{
		_emaLength = Param(nameof(EmaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Length", "EMA period", "Indicator");

		_channelOffset = Param(nameof(ChannelOffset), 50m)
			.SetDisplay("Channel Offset", "Distance from EMA for channel", "Indicator");

		_takeProfit = Param(nameof(TakeProfit), 2000m)
			.SetDisplay("Take Profit", "Take profit distance", "Risk");

		_stopLoss = Param(nameof(StopLossVal), 1500m)
			.SetDisplay("Stop Loss", "Stop loss distance", "Risk");

		_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 ema = new ExponentialMovingAverage { Length = EmaLength };

		SubscribeCandles(CandleType)
			.Bind(ema, ProcessCandle)
			.Start();
	}

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

		var close = candle.ClosePrice;
		var upperBand = emaValue + ChannelOffset;
		var lowerBand = emaValue - ChannelOffset;

		// Exit management
		if (Position > 0)
		{
			var profit = close - _entryPrice;
			if ((TakeProfit > 0 && profit >= TakeProfit) || (StopLossVal > 0 && -profit >= StopLossVal))
			{
				SellMarket();
				return;
			}
		}
		else if (Position < 0)
		{
			var profit = _entryPrice - close;
			if ((TakeProfit > 0 && profit >= TakeProfit) || (StopLossVal > 0 && -profit >= StopLossVal))
			{
				BuyMarket();
				return;
			}
		}

		// Entry: channel breakout
		if (Position == 0)
		{
			if (close > upperBand)
			{
				BuyMarket();
				_entryPrice = close;
			}
			else if (close < lowerBand)
			{
				SellMarket();
				_entryPrice = close;
			}
		}
	}
}