在 GitHub 上查看

N Candles 策略

N Candles 策略复刻原始的 MQL 智能交易程序:当最近 N 根已完成的蜡烛线方向一致时入场。若窗口内所有蜡烛都收阳,则提交市价买单;若全部收阴,则提交市价卖单。策略本身不包含止盈、止损或离场逻辑,持仓需要人工或其他风控模块来处理。

概览

  • 适用市场:更适合出现短期动量爆发的行情。
  • 标的范围:外汇、期货、加密货币等连续交易的资产。
  • 周期设置:可配置,默认使用 1 小时蜡烛。
  • 下单方式:单纯的市价单,无保护性挂单。

工作流程

  1. 每当一根蜡烛收盘,策略检查最近的 N 根蜡烛。
  2. 若窗口内全部收阳,则按设定手数买入。
  3. 若全部收阴,则按设定手数卖出。
  4. 若出现十字星(开盘价等于收盘价),计数会被清零,直到形成新的连续趋势。
  5. 策略不会主动平仓;在净额账户上,重复信号会不断加仓同一方向。

参数说明

  • Consecutive Candles:触发交易所需的连续同向蜡烛数量。
  • Volume:每次信号提交的市价单数量。
  • Candle Type:用于判定序列的蜡烛类型或周期。

使用建议

  • 由于缺乏退出逻辑,应与手动管理或额外的风险控制策略搭配使用。
  • 对于波动性较大的市场,可以降低所需蜡烛数量或缩短周期以捕捉更快的走势。
  • 持续的信号可能导致仓位迅速增加,请关注杠杆、保证金及账户风险限制。
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Trades in the direction of consecutive candles of the same color.
/// </summary>
public class NCandlesStrategy : Strategy
{
	private readonly StrategyParam<int> _consecutiveCandles;
	private readonly StrategyParam<DataType> _candleType;

	private int _currentDirection;
	private int _streakLength;

	/// <summary>
	/// Number of identical candles that must appear in a row to trigger an order.
	/// </summary>
	public int ConsecutiveCandles
	{
		get => _consecutiveCandles.Value;
		set => _consecutiveCandles.Value = value;
	}


	/// <summary>
	/// The type of candles used for analysis.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the strategy.
	/// </summary>
	public NCandlesStrategy()
	{
		_consecutiveCandles = Param(nameof(ConsecutiveCandles), 4)
			.SetGreaterThanZero()
			.SetDisplay("Consecutive Candles", "Number of identical candles required", "General")
			
			.SetOptimize(2, 6, 1);


		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candles to analyze", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_currentDirection = 0;
		_streakLength = 0;
	}

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

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

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

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

		var direction = 0;

		if (candle.ClosePrice > candle.OpenPrice)
		{
			direction = 1;
		}
		else if (candle.ClosePrice < candle.OpenPrice)
		{
			direction = -1;
		}
		else
		{
			// Doji candle breaks the streak just like in the original expert.
			_currentDirection = 0;
			_streakLength = 0;
			return;
		}

		if (direction == _currentDirection)
		{
			_streakLength = Math.Min(_streakLength + 1, ConsecutiveCandles);
		}
		else
		{
			_currentDirection = direction;
			_streakLength = 1;
		}

		if (_streakLength < ConsecutiveCandles)
			return;

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