在 GitHub 上查看

XPeriod Candle System TM Plus 策略

概述

该策略为 MetaTrader 专家顾问 Exp_XPeriodCandleSystem_Tm_Plus 的 StockSharp 移植版本。原始系统依赖自定义的 XPeriod Candle System 指标,通过平滑蜡烛数据并根据布林带突破对柱线着色。移植版本使用指数平滑重建合成 OHLC 数据、复制相同的价格模式,并根据颜色状态驱动交易,同时保留时间平仓与保护性订单。

交易逻辑

  1. 平滑蜡烛:使用可配置周期的指数移动平均生成合成的开盘价、最高价、最低价和收盘价。
  2. 价格模式:支持 12 种应用价格(收盘、开盘、中位、趋势跟随、Demark 等),然后送入布林带。
  3. 布林带分析:布林带长度与偏差可调,只有在指示器形成后才评估信号。
  4. 颜色状态
    • 多头柱线收盘高于上轨 → 颜色 0(向上突破)。
    • 空头柱线收盘低于下轨 → 颜色 4(向下突破)。
    • 其余多头柱线 → 颜色 1,其余空头柱线 → 颜色 3
    • 额外的突破偏移量(依据品种最小变动转换为价格单位)可减少误触发。
  5. 入场规则:观察 SignalBar 指定的柱以及更早一柱:
    • 若上一柱为颜色 0 而当前信号柱非 0,允许开多。
    • 若上一柱为颜色 4 而当前信号柱非 4,允许开空。
  6. 出场规则
    • 参考柱颜色大于 2 时平掉多单。
    • 参考柱颜色小于 2 时平掉空单。
    • 可选的持仓计时器在达到 HoldingMinutes 后强制离场。
  7. 风控:通过 StartProtection 启动的止盈止损以绝对价格距离表达,可随时设为 0 禁用。

参数

参数 说明 默认值
OrderVolume 每次下单的基础数量。 0.1
BuyPosOpen / SellPosOpen 是否允许多/空头开仓。 true
BuyPosClose / SellPosClose 是否允许多/空头平仓。 true
TimeTrade 是否启用持仓时间过滤。 true
HoldingMinutes 持仓超过该分钟数后强制离场。 960
CandleType 信号使用的蜡烛类型(时间框架)。 4 小时
Period 平滑指数移动平均的长度。 5
BollingerLength 布林带窗口内的平滑柱数。 20
BandsDeviation 布林带宽度倍数。 1.001
AppliedPriceMode 送入布林带的价格模式。 Close
SignalBar 用于信号判断的柱索引(1 表示上一根已完成柱)。 1
StopLoss / TakeProfit 保护性止损/止盈的绝对价格距离。 1000 / 2000
Deviation 判定突破时额外加入的价格偏移。 10

使用说明

  • 指数平滑用于逼近原始指标的 XPeriod 处理,可根据品种波动调整 Period
  • SignalBar 必须小于内部保存的历史深度(最多 14 根)。超出范围时策略会忽略信号。
  • 若品种提供 PriceStep,突破偏移会乘以最小变动,使其等效于 MetaTrader 中的点值偏移。
  • StopLossTakeProfit 为绝对价格距离,设为 0 即可关闭保护单功能。
  • 当前目录仅包含 C# 版本,暂未提供 Python 实现。
using System;
using System.Collections.Generic;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// XPeriod candle system with Bollinger Bands breakout.
/// Buys on close above upper band with bullish candle, sells on close below lower band with bearish candle.
/// </summary>
public class XPeriodCandleSystemTmPlusStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _bbPeriod;
	private readonly StrategyParam<decimal> _bbWidth;

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

	public int BbPeriod
	{
		get => _bbPeriod.Value;
		set => _bbPeriod.Value = value;
	}

	public decimal BbWidth
	{
		get => _bbWidth.Value;
		set => _bbWidth.Value = value;
	}

	public XPeriodCandleSystemTmPlusStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe", "General");

		_bbPeriod = Param(nameof(BbPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("BB Period", "Bollinger Bands period", "Indicators");

		_bbWidth = Param(nameof(BbWidth), 2m)
			.SetGreaterThanZero()
			.SetDisplay("BB Width", "Bollinger Bands width", "Indicators");
	}

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

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

		var bb = new BollingerBands { Length = BbPeriod, Width = BbWidth };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(bb, ProcessCandle)
			.Start();

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

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

		var bb = (BollingerBandsValue)value;
		if (bb.UpBand is not decimal upper ||
			bb.LowBand is not decimal lower ||
			bb.MovingAverage is not decimal middle)
			return;

		var close = candle.ClosePrice;
		var isBullish = candle.ClosePrice > candle.OpenPrice;
		var isBearish = candle.ClosePrice < candle.OpenPrice;

		// Buy: close above upper band with bullish candle
		if (close > upper && isBullish && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// Sell: close below lower band with bearish candle
		else if (close < lower && isBearish && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}
		// Exit long at middle band
		else if (Position > 0 && close < middle)
		{
			SellMarket();
		}
		// Exit short at middle band
		else if (Position < 0 && close > middle)
		{
			BuyMarket();
		}
	}
}