在 GitHub 上查看

Binario 31 策略

从 MetaTrader 脚本 binario_31 转换而来的突破策略。算法使用 144 周期的指数移动平均分别应用于每根 K 线的最高价和最低价,从而形成一个动态通道。当价格位于通道内部时,策略会预先设置两个止损单:

  • 在 EMA 高值上方加上偏移量的买入止损;
  • 在 EMA 低值下方减去同样偏移量的卖出止损。

当价格突破其中一个水平时,在突破方向开仓。保护性止损放在通道另一侧,止盈目标根据入场价计算。还可以启用追踪止损以保护盈利。

参数

  • EMA Length – 高低价 EMA 的周期。
  • Pip Difference – 从 EMA 到突破入场的距离(价格步长)。
  • Take Profit – 从入场到止盈的距离(价格步长)。
  • Trailing Stop – 追踪止损距离(价格步长),0 表示禁用。
  • Volume – 交易数量。
  • Candle Type – 使用的蜡烛类型。
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;
			}
		}
	}
}