在 GitHub 上查看

Puria Method 策略

概述

Puria Method 是一套起源于 MetaTrader 的顺势策略,通过三条均线与 MACD 主线组合来识别动量突破。本转换版完整保留原始信号逻辑,同时提供止损、止盈、追踪止损以及分批止盈等风险控制功能,方便在 StockSharp 平台上直接使用。

交易逻辑

  • 计算三条可配置的均线(周期、平滑方式及价格来源均可调整)。
  • 使用前一根完成 K 线的数据比较快速均线与基准均线的差值:多头信号要求两条快速均线都至少高于基准均线 0.5 个最小价格单位;空头信号则要求基准均线领先快速均线。
  • 借助 MACD 主线确认趋势方向。多头必须满足上一根 MACD 值为正且最近若干根 MACD 不断抬升;空头条件相反。
  • 当出现进场信号时,如有反向持仓先行平仓,再以净头寸方式开立新仓位。

风险管理

  • 止损 / 止盈: 依据设置的点数从开仓价推算,并自动对齐交易品种的最小价位步长。
  • 追踪止损: 当浮盈超过“追踪距离 + 调整步长”后,止损价格随市价逐步上移/下移。
  • 分批止盈: 价格达到最小盈利距离后,可按比例平掉部分仓位以锁定收益。
  • 仓位跟踪: 策略记录多头的最高价与空头的最低价,用于触发止损或止盈判定。

参数说明

参数 含义
StopLossPips 止损点数。
TakeProfitPips 止盈点数。
TrailingStopPips 追踪止损距离。
TrailingStepPips 追踪止损更新所需的最小推进距离。
MinProfitStepPips 启动分批止盈的最小盈利点数。
MinProfitFraction 达到盈利条件时平仓的比例。
CandleType 使用的主 K 线类型。
Ma0Period, Ma1Period, Ma2Period 三条均线的周期。
Ma0Shift, Ma1Shift, Ma2Shift 均线的横向偏移量。
Ma0Method, Ma1Method, Ma2Method 均线平滑方式(简单、指数、平滑、加权)。
Ma0Price, Ma1Price, Ma2Price 均线的价格来源。
MacdFastPeriod, MacdSlowPeriod, MacdSignalPeriod MACD 参数。
MacdTrendBars 检查 MACD 单调趋势的历史柱数(最少 3)。
MacdPrice MACD 计算所用价格。

使用建议

  • 策略仅处理 CandleStates.Finished 的 K 线,建议选择与原策略相同周期的蜡烛序列。
  • 点值会根据品种的小数位自动换算,无需手动调整。
  • 若某项风险控制参数设置为零,则相应功能(如追踪止损或分批止盈)不会触发。
  • 可结合回测或优化器调整均线、MACD 及风险参数,以适配不同市场品种。
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;

public class PuriaMethodStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public PuriaMethodStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}