在 GitHub 上查看

Boilerplate Configurable 策略

Boilerplate Configurable 策略可以在两种模式下运行:简单移动平均线交叉或布林带挤压突破。它包含交易日和交易时段过滤、日期范围、新闻窗口以及基于 ATR 或固定风险回报的风险管理。

详情

  • 入场条件
    • SmaCross 模式下,当快速 SMA 上穿慢速 SMA 时做多,反向交叉时做空。
    • Squeeze 模式下,当价格突破外侧布林带且位于窄布林带内时入场。
  • 多空方向:可配置仅做多、仅做空或双向,并支持反转。
  • 出场条件
    • 基于 ATR 或固定百分比的止损和止盈。
    • 每日退出时间段和新闻窗口会关闭所有持仓。
  • 止损:每笔交易的止损和止盈并带有回撤保护。
  • 默认参数
    • Length = 20
    • WideMultiplier = 1.5
    • NarrowMultiplier = 2
    • MaxLossPerc = 0.02
    • AtrMultiplier = 1.5
    • StaticRr = 2
    • NewsWindow = 5
    • MaxDrawdown = 0.1
  • 过滤器
    • 类别:模块化
    • 方向:多头和空头
    • 指标:SMA、布林带、ATR
    • 止损:是
    • 复杂度:高
    • 时间框架:任意
    • 季节性:是
    • 神经网络:否
    • 背离:否
    • 风险级别:高
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;
	}
}