GitHub で見る

Boilerplate Configurable Strategy

The Boilerplate Configurable strategy can switch between two modes: a simple moving average crossover or a Bollinger squeeze breakout. It features trading day and session filters, a date range, a news window, and risk management using ATR or static risk/reward.

Details

  • Entry Criteria:
    • In SmaCross mode, go long when the fast SMA crosses above the slow SMA and go short on the opposite cross.
    • In Squeeze mode, enter when price breaks the outer Bollinger band while remaining inside the narrower band.
  • Long/Short: Configurable for long, short, or both with optional inversion.
  • Exit Criteria:
    • Stop loss and take profit based on ATR or static percentages.
    • Daily exit period and news window close all positions.
  • Stops: Per-trade stop loss and take profit with drawdown protection.
  • Default Values:
    • Length = 20
    • WideMultiplier = 1.5
    • NarrowMultiplier = 2
    • MaxLossPerc = 0.02
    • AtrMultiplier = 1.5
    • StaticRr = 2
    • NewsWindow = 5
    • MaxDrawdown = 0.1
  • Filters:
    • Category: Modular
    • Direction: Long & Short
    • Indicators: SMA, Bollinger Bands, ATR
    • Stops: Yes
    • Complexity: High
    • Timeframe: Any
    • Seasonality: Yes
    • Neural networks: No
    • Divergence: No
    • Risk level: High
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>
/// Boilerplate configurable strategy using EMA crossover for trend timing.
/// Enters long on golden cross, short on death cross.
/// </summary>
public class BoilerplateConfigurableStrategy : Strategy
{
	private readonly StrategyParam<int> _fastEmaPeriod;
	private readonly StrategyParam<int> _slowEmaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFastEma;
	private decimal _prevSlowEma;

	public int FastEmaPeriod { get => _fastEmaPeriod.Value; set => _fastEmaPeriod.Value = value; }
	public int SlowEmaPeriod { get => _slowEmaPeriod.Value; set => _slowEmaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BoilerplateConfigurableStrategy()
	{
		_fastEmaPeriod = Param(nameof(FastEmaPeriod), 120)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowEmaPeriod = Param(nameof(SlowEmaPeriod), 450)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFastEma = 0m;
		_prevSlowEma = 0m;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var fastEma = new ExponentialMovingAverage { Length = FastEmaPeriod };
		var slowEma = new ExponentialMovingAverage { Length = SlowEmaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastEma, slowEma, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fastEma);
			DrawIndicator(area, slowEma);
			DrawOwnTrades(area);
		}
	}

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

		if (_prevFastEma == 0m || _prevSlowEma == 0m)
		{
			_prevFastEma = fastEmaValue;
			_prevSlowEma = slowEmaValue;
			return;
		}

		if (_prevFastEma <= _prevSlowEma && fastEmaValue > slowEmaValue && Position <= 0)
		{
			BuyMarket();
		}
		else if (_prevFastEma >= _prevSlowEma && fastEmaValue < slowEmaValue && Position >= 0)
		{
			SellMarket();
		}

		_prevFastEma = fastEmaValue;
		_prevSlowEma = slowEmaValue;
	}
}