在 GitHub 上查看

Pipsover

概述

Pipsover 是一套动量反转策略,针对 Chaikin 振荡器的极值做出反应。原始的 MetaTrader 5 智能交易系统会在振荡器出现显著尖峰、且上一根 K 线回撤到 20 周期简单移动平均线时开仓。本 C# 版本复刻了同样的思想,通过累积/派发指标配合两条指数移动平均线重建 Chaikin 振荡器。同时沿用脚本中设定的止损与止盈距离,以保持风险控制与原版一致。

指标与工具

  • 简单移动平均线(SMA 20):提供均值回归参考。只有当上一根 K 线触及或跌破该均线后,交易条件才可能成立。
  • Chaikin 振荡器(ADL 的 3 周期 EMA 减去 10 周期 EMA):衡量价格与成交量之间的压力。极低读数触发做多机会,极高读数触发做空机会。
  • 累积/派发线(ADL):作为 Chaikin 振荡器的输入。快慢两条 EMA 以此数据流运行,从而模拟 MQL5 中的 iChaikin 指标。

交易逻辑

做多条件

  1. 等待一根完整的 K 线,确保所有指标数值已经最终确定。
  2. 上一根 K 线必须收阳(Close > Open)。
  3. 上一根最低价需要跌破 SMA20,说明出现回踩。
  4. 读取上一根的 Chaikin 值,必须低于 -OpenLevel,表明振荡器过度超卖。
  5. 在没有持仓的情况下,满足以上条件即可市价买入。

做空条件

  1. 等待完整 K 线。
  2. 上一根 K 线必须收阴(Close < Open)。
  3. 上一根最高价需要突破 SMA20。
  4. 上一根 Chaikin 值需大于 OpenLevel
  5. 在没有持仓时触发市价卖出。

平仓逻辑

  • 多单:当下一根 K 线呈现空头结构(收盘低于开盘)、最高价仍位于 SMA20 之上,且 Chaikin 值突破 CloseLevel 时平仓。
  • 空单:当下一根 K 线呈现多头结构、最低价跌破 SMA20,且 Chaikin 值跌破 -CloseLevel 时平仓。
  • 防护退出:每根完整 K 线都会检查是否触发止损或止盈。多单若价格达到止损线以下或止盈线以上则立即平仓;空单的比较方向相反。

仓位管理

  • 任意时刻仅允许存在一个净仓位。开新仓前会取消所有挂单,以符合原版 EA 只持有单一仓位的行为。
  • 止损与止盈根据当前品种的最小报价步长计算。多单的止损设置在入场价下方 StopLossPoints * PriceStep,止盈则在上方同等距离;空单采用对称但方向相反的距离。

参数

名称 默认值 说明
TradeVolume 0.1 每次市价单的交易量。
MaLength 20 均线回撤过滤的周期。
StopLossPoints 65 入场价下方(或上方)的止损点数,以报价步长计。
TakeProfitPoints 100 入场价上方(或下方)的止盈点数。
OpenLevel 100 触发新开仓的 Chaikin 阈值。
CloseLevel 125 强制平仓的 Chaikin 阈值。
ChaikinFastLength 3 Chaikin 快 EMA 的周期。
ChaikinSlowLength 10 Chaikin 慢 EMA 的周期。
CandleType 1 小时 订阅的 K 线周期,可根据交易时段调整。

实现细节

  • 策略通过 SubscribeCandles().Bind(...) 将 ADL 与 SMA 绑定到 K 线订阅,保证每根完成的 K 线都会同步更新指标。
  • ProcessCandle 中手动重建 Chaikin 值,避免触及转换指南禁止的低级缓存操作。
  • 算法缓存最近一根完成 K 线的 OHLC、SMA 与 Chaikin 数值,以复刻 MQL5 中 iClose(...,1)iLow(...,1)iChaikin(...,1) 的“上一根”逻辑。
  • 止损与止盈在策略内部追踪,而非依赖经纪商托管,确保在回测与实盘中表现一致。
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>
/// Pipsover strategy rebuilt on the high level StockSharp API.
/// The strategy opens positions when a strong Chaikin oscillator spike aligns with a pullback to the 20-period SMA on the previous candle.
/// Protective stop-loss and take-profit levels are recreated using price step distances defined in the original Expert Advisor.
/// </summary>
public class PipsoverStrategy : Strategy
{
	private readonly StrategyParam<decimal> _tradeVolume;
	private readonly StrategyParam<int> _maLength;
	private readonly StrategyParam<decimal> _stopLossPoints;
	private readonly StrategyParam<decimal> _takeProfitPoints;
	private readonly StrategyParam<decimal> _openLevel;
	private readonly StrategyParam<decimal> _closeLevel;
	private readonly StrategyParam<int> _chaikinFastLength;
	private readonly StrategyParam<int> _chaikinSlowLength;
	private readonly StrategyParam<DataType> _candleType;

	private SimpleMovingAverage _sma;
	private AccumulationDistributionLine _accumulationDistribution;
	private ExponentialMovingAverage _chaikinFast;
	private ExponentialMovingAverage _chaikinSlow;

	private decimal _prevOpen;
	private decimal _prevHigh;
	private decimal _prevLow;
	private decimal _prevClose;
	private decimal _prevSma;
	private decimal _prevChaikin;
	private bool _hasPrevCandle;

	private decimal _stopPrice;
	private decimal _takeProfitPrice;
	private bool _hasTargets;

	/// <summary>
	/// Trading volume used for market orders.
	/// </summary>
	public decimal TradeVolume
	{
		get => _tradeVolume.Value;
		set => _tradeVolume.Value = value;
	}

	/// <summary>
	/// Period of the simple moving average that acts as a pullback filter.
	/// </summary>
	public int MaLength
	{
		get => _maLength.Value;
		set => _maLength.Value = value;
	}

	/// <summary>
	/// Stop-loss distance expressed in price steps.
	/// </summary>
	public decimal StopLossPoints
	{
		get => _stopLossPoints.Value;
		set => _stopLossPoints.Value = value;
	}

	/// <summary>
	/// Take-profit distance expressed in price steps.
	/// </summary>
	public decimal TakeProfitPoints
	{
		get => _takeProfitPoints.Value;
		set => _takeProfitPoints.Value = value;
	}

	/// <summary>
	/// Chaikin oscillator level required to allow new entries.
	/// </summary>
	public decimal OpenLevel
	{
		get => _openLevel.Value;
		set => _openLevel.Value = value;
	}

	/// <summary>
	/// Chaikin oscillator level that closes existing positions.
	/// </summary>
	public decimal CloseLevel
	{
		get => _closeLevel.Value;
		set => _closeLevel.Value = value;
	}

	/// <summary>
	/// Fast EMA period for Chaikin oscillator reconstruction.
	/// </summary>
	public int ChaikinFastLength
	{
		get => _chaikinFastLength.Value;
		set => _chaikinFastLength.Value = value;
	}

	/// <summary>
	/// Slow EMA period for Chaikin oscillator reconstruction.
	/// </summary>
	public int ChaikinSlowLength
	{
		get => _chaikinSlowLength.Value;
		set => _chaikinSlowLength.Value = value;
	}

	/// <summary>
	/// Candle type used for indicator calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="PipsoverStrategy"/> class.
	/// </summary>
	public PipsoverStrategy()
	{
		_tradeVolume = Param(nameof(TradeVolume), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Trade Volume", "Order size used for market entries", "Trading");

		_maLength = Param(nameof(MaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("SMA Length", "Simple moving average length", "Indicators");

		_stopLossPoints = Param(nameof(StopLossPoints), 65m)
			.SetGreaterThanZero()
			.SetDisplay("Stop-Loss Points", "Stop-loss distance expressed in price steps", "Risk");

		_takeProfitPoints = Param(nameof(TakeProfitPoints), 100m)
			.SetGreaterThanZero()
			.SetDisplay("Take-Profit Points", "Take-profit distance expressed in price steps", "Risk");

		_openLevel = Param(nameof(OpenLevel), 20m)
			.SetGreaterThanZero()
			.SetDisplay("Open Level", "Chaikin oscillator threshold for entries", "Chaikin");

		_closeLevel = Param(nameof(CloseLevel), 30m)
			.SetGreaterThanZero()
			.SetDisplay("Close Level", "Chaikin oscillator threshold for exits", "Chaikin");

		_chaikinFastLength = Param(nameof(ChaikinFastLength), 3)
			.SetGreaterThanZero()
			.SetDisplay("Chaikin Fast Length", "Fast EMA length for Chaikin oscillator", "Chaikin");

		_chaikinSlowLength = Param(nameof(ChaikinSlowLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("Chaikin Slow Length", "Slow EMA length for Chaikin oscillator", "Chaikin");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe used for calculations", "Data");
	}

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

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

		// Reset cached candle and indicator state.
		_hasPrevCandle = false;
		_prevOpen = 0m;
		_prevHigh = 0m;
		_prevLow = 0m;
		_prevClose = 0m;
		_prevSma = 0m;
		_prevChaikin = 0m;

		// Reset protective price levels.
		_stopPrice = 0m;
		_takeProfitPrice = 0m;
		_hasTargets = false;
	}

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

		// Apply configured trading volume to base strategy.
		Volume = TradeVolume;

		// Prepare indicators that replicate the MQL Expert Advisor logic.
		_sma = new SMA { Length = MaLength };
		_accumulationDistribution = new AccumulationDistributionLine();
		_chaikinFast = new EMA { Length = ChaikinFastLength };
		_chaikinSlow = new EMA { Length = ChaikinSlowLength };

		// Subscribe to candle data and bind indicators.
		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_accumulationDistribution, _sma, ProcessCandle)
			.Start();

		// Optional charting for visual validation.
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _sma);
			DrawIndicator(area, _accumulationDistribution);
			DrawOwnTrades(area);
		}
	}

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

		// Rebuild Chaikin oscillator values via EMA of the ADL indicator.
		var fastResult = _chaikinFast.Process(new DecimalIndicatorValue(_chaikinFast, adlValue, candle.ServerTime) { IsFinal = true });
		var slowResult = _chaikinSlow.Process(new DecimalIndicatorValue(_chaikinSlow, adlValue, candle.ServerTime) { IsFinal = true });
		var chaikinValue = fastResult.ToDecimal() - slowResult.ToDecimal();

		// Wait for all indicators to be fully formed before trading.
		if (!_chaikinFast.IsFormed || !_chaikinSlow.IsFormed || !_sma.IsFormed)
		{
			UpdateState(candle, chaikinValue, smaValue);
			return;
		}

		if (!_hasPrevCandle)
		{
			UpdateState(candle, chaikinValue, smaValue);
			return;
		}

		var step = Security?.PriceStep ?? 1m;
		var stopLossDistance = StopLossPoints * step;
		var takeProfitDistance = TakeProfitPoints * step;

		// Check protective stop-loss and take-profit targets before new decisions.
		if (_hasTargets)
		{
			if (Position > 0)
			{
				if (candle.LowPrice <= _stopPrice || candle.HighPrice >= _takeProfitPrice)
				{
					SellMarket(Position);
					ResetTargets();
				}
			}
			else if (Position < 0)
			{
				if (candle.HighPrice >= _stopPrice || candle.LowPrice <= _takeProfitPrice)
				{
					BuyMarket(Math.Abs(Position));
					ResetTargets();
				}
			}
			else
			{
				ResetTargets();
			}
		}

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			UpdateState(candle, chaikinValue, smaValue);
			return;
		}

		var prevBullish = _prevClose > _prevOpen;
		var prevBearish = _prevClose < _prevOpen;

		if (Position > 0)
		{
			var shouldExitLong = prevBearish && _prevHigh > _prevSma && _prevChaikin > CloseLevel;
			if (shouldExitLong)
			{
				SellMarket(Position);
				ResetTargets();
			}
		}
		else if (Position < 0)
		{
			var shouldExitShort = prevBullish && _prevLow < _prevSma && _prevChaikin < -CloseLevel;
			if (shouldExitShort)
			{
				BuyMarket(Math.Abs(Position));
				ResetTargets();
			}
		}
		else
		{
			// No position is open, evaluate entry signals.

			var allowLong = prevBullish && _prevLow < _prevSma && _prevChaikin < -OpenLevel;
			var allowShort = prevBearish && _prevHigh > _prevSma && _prevChaikin > OpenLevel;

			if (allowLong)
			{
				BuyMarket();

				var entryPrice = candle.ClosePrice;
				_stopPrice = entryPrice - stopLossDistance;
				_takeProfitPrice = entryPrice + takeProfitDistance;
				_hasTargets = true;
			}
			else if (allowShort)
			{
				SellMarket();

				var entryPrice = candle.ClosePrice;
				_stopPrice = entryPrice + stopLossDistance;
				_takeProfitPrice = entryPrice - takeProfitDistance;
				_hasTargets = true;
			}
		}

		UpdateState(candle, chaikinValue, smaValue);
	}

	private void UpdateState(ICandleMessage candle, decimal chaikinValue, decimal smaValue)
	{
		// Store previous candle data for next iteration checks.
		_prevOpen = candle.OpenPrice;
		_prevHigh = candle.HighPrice;
		_prevLow = candle.LowPrice;
		_prevClose = candle.ClosePrice;
		_prevSma = smaValue;
		_prevChaikin = chaikinValue;
		_hasPrevCandle = true;
	}

	private void ResetTargets()
	{
		// Clear stop-loss and take-profit levels once position is closed.
		_stopPrice = 0m;
		_takeProfitPrice = 0m;
		_hasTargets = false;
	}
}