在 GitHub 上查看

MMA Breakout Volume I 策略

该策略在价格突破长期平滑移动平均线(SMMA)时进行交易。 当收盘价向上突破 SMMA(200) 时开多仓,向下突破时开空仓。 当价格与持仓方向相反并穿越指数移动平均线(EMA)时退出仓位。

详情

  • 入场条件
    • 多头:收盘价向上穿越 SMMA(200)。
    • 空头:收盘价向下穿越 SMMA(200)。
  • 出场条件
    • 多头:收盘价跌破 EMA(5)。
    • 空头:收盘价突破 EMA(5)。
  • 多/空:双向。
  • 止损:无固定止损,退出由 EMA 信号决定。
  • 默认值
    • SMMA 周期 = 200
    • EMA 周期 = 5
    • 蜡烛类型 = 5 分钟
  • 过滤器
    • 类型:趋势跟随
    • 方向:双向
    • 指标:移动平均线
    • 止损:否
    • 复杂度:简单
    • 时间框架:短期
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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>
/// Strategy based on smoothed moving average breakout with EMA exit.
/// </summary>
public class MmaBreakoutVolumeIStrategy : Strategy
{
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _exitPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevPrice;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int ExitPeriod { get => _exitPeriod.Value; set => _exitPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MmaBreakoutVolumeIStrategy()
	{
		_slowPeriod = Param(nameof(SlowPeriod), 50)
			.SetDisplay("Slow EMA Period", "Period for long moving average", "Indicators");

		_exitPeriod = Param(nameof(ExitPeriod), 10)
			.SetDisplay("Exit EMA Period", "Period for exit EMA", "Indicators");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevPrice = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

		var slowEma = new ExponentialMovingAverage { Length = SlowPeriod };
		var exitEma = new ExponentialMovingAverage { Length = ExitPeriod };

		SubscribeCandles(CandleType)
			.Bind(slowEma, exitEma, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevPrice = candle.ClosePrice;
			_prevSlow = slowValue;
			_hasPrev = true;
			return;
		}

		var isCrossAbove = _prevPrice <= _prevSlow && candle.ClosePrice > slowValue;
		var isCrossBelow = _prevPrice >= _prevSlow && candle.ClosePrice < slowValue;

		if (isCrossAbove && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (isCrossBelow && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
		else if (Position > 0 && candle.ClosePrice < exitValue)
			SellMarket();
		else if (Position < 0 && candle.ClosePrice > exitValue)
			BuyMarket();

		_prevPrice = candle.ClosePrice;
		_prevSlow = slowValue;
	}
}