Ver no GitHub

Xbug Free V4 Strategy

This strategy opens positions when a moving average of the median price crosses the median price itself. A symmetric take profit and stop loss are placed at a fixed distance from the entry price.

Details

  • Entry Criteria:
    • Long: the moving average is above the median price and was below it two candles ago
    • Short: the moving average is below the median price and was above it two candles ago
  • Long/Short: Both
  • Exit Criteria:
    • Take profit at StopPoints distance above/below entry
    • Stop loss at StopPoints distance opposite side
  • Stops: Yes
  • Default Values:
    • MaPeriod = 19
    • StopPoints = 270
    • Volume = 0.1m
    • CandleType = TimeSpan.FromHours(4).TimeFrame()
  • Filters:
    • Category: Crossover
    • Direction: Both
    • Indicators: SMA
    • Stops: Yes
    • Complexity: Basic
    • Timeframe: Long-term
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • Risk Level: Medium
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>
/// Xbug Free v4 strategy based on SMA crossing median price.
/// </summary>
public class XbugFreeV4Strategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevSma;
	private decimal _prevPrice;
	private decimal _prev2Sma;
	private decimal _prev2Price;
	private int _barCount;

	public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public XbugFreeV4Strategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Moving average length", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevSma = 0; _prevPrice = 0;
		_prev2Sma = 0; _prev2Price = 0;
		_barCount = 0;
	}

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

		var sma = new SimpleMovingAverage { Length = MaPeriod };
		var stdev = new StandardDeviation { Length = 14 };

		SubscribeCandles(CandleType).Bind(sma, stdev, ProcessCandle).Start();
	}

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

		var median = (candle.HighPrice + candle.LowPrice) / 2m;
		_barCount++;

		if (_barCount >= 3)
		{
			var buySignal = smaValue > median && _prevSma > _prevPrice && _prev2Sma < _prev2Price;
			var sellSignal = smaValue < median && _prevSma < _prevPrice && _prev2Sma > _prev2Price;

			if (buySignal && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			else if (sellSignal && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}

		_prev2Sma = _prevSma;
		_prev2Price = _prevPrice;
		_prevSma = smaValue;
		_prevPrice = median;
	}
}