在 GitHub 上查看

MA平滑蜡烛策略

概述

该策略来源于MQL5专家顾问“MA Rounding Candle”。它对每根K线的开盘价和收盘价分别计算平滑移动平均线,并根据两条平均线的相对位置生成一根“合成蜡烛”。当平滑收盘价高于平滑开盘价时视为上涨蜡烛,反之则为下跌蜡烛,相等时为中性。上一根蜡烛的颜色与当前蜡烛不同则触发交易信号。

算法

  1. 对每根完成的K线的开盘价和收盘价分别计算给定周期的简单移动平均。
  2. 根据平滑值判断蜡烛颜色:
    • 上涨蜡烛 – 平滑收盘价高于平滑开盘价。
    • 下跌蜡烛 – 平滑收盘价低于平滑开盘价。
    • 中性 – 两者相等。
  3. 若上一根蜡烛为上涨而当前蜡烛不是上涨,则开多单并平掉空单。
  4. 若上一根蜡烛为下跌而当前蜡烛不是下跌,则开空单并平掉多单。

参数

  • MaLength – 平滑移动平均的周期,默认12。
  • CandleType – 处理的K线时间框架。

说明

该策略展示了如何仅使用StockSharp内置工具复现自定义指标信号。策略没有设置止损或止盈,当出现反向信号时立即反向持仓。

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>
/// MA Rounding Candle strategy.
/// Opens a long position when a smoothed candle is bullish and a short position when it is bearish.
/// </summary>
public class MaRoundingCandleStrategy : Strategy
{
	private readonly StrategyParam<int> _maLength;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _openMa;
	private ExponentialMovingAverage _closeMa;
	private int _prevColor = 1;

	public MaRoundingCandleStrategy()
	{
		_maLength = Param(nameof(MaLength), 12)
			.SetDisplay("MA Length", "Moving average length", "Parameters");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame());
	}

	public int MaLength { get => _maLength.Value; set => _maLength.Value = value; }

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_openMa = default;
		_closeMa = default;
		_prevColor = 1;
	}

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

		_openMa = new ExponentialMovingAverage { Length = MaLength };
		_closeMa = new ExponentialMovingAverage { Length = MaLength };

		Indicators.Add(_openMa);
		Indicators.Add(_closeMa);

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

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

		StartProtection(null, null);
	}

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

		var openVal = _openMa.Process(candle.OpenPrice, candle.OpenTime, true).ToDecimal();
		var closeVal = _closeMa.Process(candle.ClosePrice, candle.OpenTime, true).ToDecimal();

		if (!_openMa.IsFormed || !_closeMa.IsFormed)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var color = openVal < closeVal ? 2 : openVal > closeVal ? 0 : 1;

		if (_prevColor == 2 && color != 2 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (_prevColor == 0 && color != 0 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevColor = color;
	}
}