Binary Wave Strategy combines several classic technical indicators into a single "binary" wave. Each indicator contributes either +1 or -1 depending on its bullish or bearish state. The weighted sum of all signals forms the final wave used for trading decisions.
Parameters
Mode – entry algorithm: Breakdown reacts to zero cross; Twist reacts to wave direction changes.
Candle Type – timeframe of candles for all calculations.
Indicator Periods – lengths for MA, MACD (fast, slow, signal), CCI, Momentum, RSI and ADX.
Weights – contribution of each indicator to the wave. Setting a weight to 0 disables the indicator.
Trading Permissions – enable or disable long/short entries and exits separately.
Risk – stop-loss and take-profit in percent of entry price.
How It Works
Subscribe to the specified candle series and calculate all indicators.
For each finished candle, evaluate the state of every indicator and convert it to a binary value (+1 / -1).
Sum weighted values to obtain the current wave.
Generate trading signals:
Breakdown: enter long when the wave crosses above zero, enter short when it crosses below zero.
Twist: enter long when the wave changes direction upwards, enter short when it turns downwards.
Optional protective stop-loss and take-profit are managed by the built-in position protection.
This approach allows flexible combination of multiple indicators while keeping the trading logic straightforward.
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>
/// Binary Wave strategy using EMA crossover.
/// </summary>
public class BinaryWaveStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private decimal _prevFast;
private decimal _prevSlow;
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 SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public BinaryWaveStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe", "General");
_fastPeriod = Param(nameof(FastPeriod), 12)
.SetGreaterThanZero()
.SetDisplay("Fast Period", "Fast EMA period", "Indicators");
_slowPeriod = Param(nameof(SlowPeriod), 26)
.SetGreaterThanZero()
.SetDisplay("Slow Period", "Slow EMA period", "Indicators");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevFast = 0;
_prevSlow = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fast = new ExponentialMovingAverage { Length = FastPeriod };
var slow = new ExponentialMovingAverage { Length = SlowPeriod };
SubscribeCandles(CandleType)
.Bind(fast, slow, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastVal, decimal slowVal)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevFast = fastVal;
_prevSlow = slowVal;
_hasPrev = true;
return;
}
var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;
if (crossUp && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (crossDown && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevFast = fastVal;
_prevSlow = slowVal;
}
}