在 GitHub 上查看

EA Vishal EURGBP H4 策略

概述

EA Vishal EURGBP H4 策略 复刻原始 MetaTrader 智能交易系统:通过随机指标交叉入场,并结合包络线突破退出。默认基于 4 小时蜡烛,仅在蜡烛收盘后做出决策,同时使用以点(pip)为单位的虚拟风控工具(止损、止盈与可选的移动止损),最大限度还原 MT4 的表现。

交易逻辑

  • 入场 – 在最近两根已完成蜡烛上计算随机指标的 %K 与 %D。当 %K 从上向下穿越 %D(在 n-2n-1 之间)时做多;反向穿越则做空。策略同一时间仅持有一个方向的仓位。
  • 出场 – 仓位按以下层次管理:
    1. 包络线突破 – 若下一根蜡烛开盘价突破上一根包络线上/下轨,而再前一根仍位于包络线内部,则立即平仓。
    2. 虚拟止损/止盈 – 依据入场价与设定的点差距离计算止损和止盈,当蜡烛的高低点触及水平时平仓。
    3. 可选移动止损 – 当启用且设置了止损距离时,止损跟踪上一根蜡烛的最高价(多头)或最低价(空头),再减/加相应的点差距离。

参数

名称 默认值 说明
Volume 0.5 每次下单的手数。
StopLossPips 0 止损距离(点),0 表示不设止损。
TakeProfitPips 22 止盈距离(点),0 表示不设止盈。
UseTrailingStop false 启用虚拟移动止损(需 StopLossPips > 0)。
StochasticKPeriod 6 随机指标 %K 的回看周期。
StochasticDPeriod 3 %D 平滑周期。
StochasticSlowing 1 %K 的减速系数。
EnvelopePeriod 32 用作包络线基准的简单移动平均周期。
EnvelopeDeviationPercent 0.3 包络线上下偏移百分比。
CandleType 4 小时 驱动策略的蜡烛类型(默认四小时)。

说明

  • 所有参数均可在 StockSharp Studio 中优化。
  • 止损/止盈在内部追踪,并在蜡烛区间触及时通过市价单执行,符合原始 EA 的“新蜡烛检查”方式。
  • 策略只处理已完成的蜡烛,便于回测与实盘结果保持一致。
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 EaVishalEurgbpH4Strategy : 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 EaVishalEurgbpH4Strategy()
	{
		_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;
	}
}