Auf GitHub ansehen

Anubis CCI MACD Strategy

Summary

  • Converts the MetaTrader 4 expert advisor "Anubis" to the StockSharp high level API.
  • Uses a 4-hour Commodity Channel Index (CCI) filter together with a 15-minute MACD crossover.
  • Applies adaptive position sizing, stop-loss, breakeven protection, ATR driven exits, and a standard deviation based take-profit.

Strategy logic

  1. Data
    • Primary timeframe: 15-minute candles (SignalCandleType), used for MACD and ATR calculations.
    • Higher timeframe: 4-hour candles (TrendCandleType), used for CCI filtering and standard deviation measurement.
  2. Indicators
    • CommodityChannelIndex with configurable period on the 4H series.
    • StandardDeviation (length 30) on 4H closes to estimate the take-profit distance.
    • MovingAverageConvergenceDivergenceSignal (fast/slow/signal configurable) on 15M candles.
    • AverageTrueRange (length 12) on 15M candles for volatility based exits.
  3. Entries
    • Short: when 4H CCI is above CciThreshold, the previous two MACD values show a bearish crossover (MACD crossing below its signal), MACD was positive, there are no open longs, and the price has moved at least PriceFilterPoints since the last short entry.
    • Long: symmetric condition with CCI below -CciThreshold, MACD crossing upwards while negative, no open shorts, and the minimum distance filter satisfied.
  4. Risk management
    • Base volume is defined by VolumeValue and is scaled by account equity (2× above 14k, 3.2× above 22k) and by LossFactor after a losing trade.
    • Maximum simultaneous trades per direction are limited by MaxLongTrades and MaxShortTrades.
    • Hard stop-loss placed virtually at StopLossPoints * PriceStep from the average entry price.
    • Breakeven activates once price advances by BreakevenPoints and immediately closes the position if price returns to the entry.
  5. Exits
    • Standard deviation take-profit closes the position once price moves StdDevMultiplier * StdDev in favor.
    • Aggressive exits trigger when the prior candle range exceeds CloseAtrMultiplier * ATR.
    • MACD deceleration exits require both sufficient profit (ProfitThresholdPoints) and a reversal in MACD slope (previous MACD less than or greater than two bars ago, depending on direction).
    • Protective stop closes the trade if price pierces the stop-loss distance or falls back to entry after breakeven activation.

Parameters

Name Description
VolumeValue Base order volume.
CciThreshold Absolute threshold for the 4H CCI filter.
CciPeriod Period of the 4H CCI indicator.
StopLossPoints Stop-loss distance in points.
BreakevenPoints Profit in points required to arm breakeven.
MacdFastPeriod Fast EMA period for MACD.
MacdSlowPeriod Slow EMA period for MACD.
MacdSignalPeriod Signal EMA period for MACD.
LossFactor Volume multiplier applied after a losing trade.
MaxShortTrades Maximum number of concurrent short entries.
MaxLongTrades Maximum number of concurrent long entries.
CloseAtrMultiplier ATR multiplier for early exits.
ProfitThresholdPoints Additional profit buffer (points) before MACD exits.
StdDevMultiplier Standard deviation multiplier for the take-profit.
PriceFilterPoints Minimum price movement between consecutive entries.
SignalCandleType Primary timeframe for MACD and ATR.
TrendCandleType Higher timeframe for CCI and standard deviation.

Notes

  • The strategy relies on valid Security.PriceStep metadata to translate point-based parameters to price distances.
  • Protective logic is implemented via explicit checks instead of pending stop/limit orders, mirroring the original EA behavior with virtual stops.
  • Python version is intentionally omitted per task instructions.
using System;
using System.Collections.Generic;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Anubis CCI + MACD strategy.
/// Buys when CCI crosses above 0 and MACD histogram is positive.
/// Sells when CCI crosses below 0 and MACD histogram is negative.
/// </summary>
public class AnubisCciMacdStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevCci;
	private bool _hasPrev;

	public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public AnubisCciMacdStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 14)
			.SetDisplay("CCI Period", "CCI lookback period", "Indicators");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevCci = 0m;
		_hasPrev = false;
	}

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

		_hasPrev = false;

		var cci = new CommodityChannelIndex { Length = CciPeriod };
		var macd = new MovingAverageConvergenceDivergenceSignal();

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(cci, macd, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue cciValue, IIndicatorValue macdValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!cciValue.IsFinal || !macdValue.IsFinal)
			return;

		var cci = cciValue.ToDecimal();
		var macdVal = (MovingAverageConvergenceDivergenceSignalValue)macdValue;

		if (macdVal.Macd is not decimal macd || macdVal.Signal is not decimal signal)
			return;

		var histogram = macd - signal;

		if (!_hasPrev)
		{
			_prevCci = cci;
			_hasPrev = true;
			return;
		}

		// CCI crosses above 0 with bullish MACD
		if (_prevCci <= 0 && cci > 0 && histogram > 0 && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// CCI crosses below 0 with bearish MACD
		else if (_prevCci >= 0 && cci < 0 && histogram < 0 && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}

		_prevCci = cci;
	}
}