在 GitHub 上查看

JS Signal Baes 策略

概述

本策略将 MetaTrader 专家顾问 "JS Signal Baes" 移植到 StockSharp 平台。系统同时监控六个周期(默认 M1、M5、M15、M30、H1、H4),只有当所有指标都指向相同方向时才开仓。通过 Reverse 参数可以反向交易,在趋势判断为多头时做空,或在空头信号时做多。

指标与判定

每个周期都会计算以下指标:

  • 双均线:采用可选的平滑方式(简单、指数、平滑或线性加权)。
  • MACD:可配置的快线、慢线以及信号线周期。
  • RSI:拥有独立的周期参数。
  • CCI:拥有独立的观测窗口。
  • 随机指标(Stochastic):由 K 周期、D 周期与平滑参数组成。

当满足下列全部条件时,该周期被视为多头

  1. 快速均线高于慢速均线;
  2. MACD 主线高于信号线;
  3. RSI 高于 50;
  4. CCI 大于 0;
  5. 随机指标 %K 高于 40。

当满足下列全部条件时,该周期被视为空头

  1. 快速均线低于慢速均线;
  2. MACD 主线低于信号线;
  3. RSI 低于 50;
  4. CCI 小于 0;
  5. 随机指标 %K 低于 60。

交易逻辑

以主周期(默认 M1)收盘价为触发点,在确认全部六个周期都同时处于多头或空头后执行交易:

  • 开多仓:六个周期全部为多头。如果启用 Reverse,则改为开空仓。
  • 开空仓:六个周期全部为空头。如启用 Reverse,则改为开多仓。

策略不做加仓或网格处理,当前持仓未平仓之前新的信号会被忽略。原版中的止损、止盈以及移动止损没有移植,需要用户自行在组合层面控制风险。

参数

参数 默认值 说明
CciPeriod 13 CCI 指标的计算窗口。
FastMaPeriod 5 快速均线的长度。
SlowMaPeriod 9 慢速均线的长度。
MaMethod LinearWeighted 两条均线所使用的平滑方式。
MacdFastPeriod 8 MACD 快线的周期。
MacdSlowPeriod 17 MACD 慢线的周期。
MacdSignalPeriod 9 MACD 信号线的周期。
StochasticKPeriod 5 随机指标 K 线周期。
StochasticDPeriod 3 随机指标 D 线周期。
StochasticSmoothing 3 随机指标的平滑参数。
RsiPeriod 9 RSI 的观察周期。
ReverseSignals false 是否反向执行信号。
TimeFrame1..6 M1、M5、M15、M30、H1、H4 对应六个时间周期的数据源。

说明

  • 默认参数复制自原始 MQL5 版本。
  • 本策略未实现原程序中的资金管理和止损/止盈/移动止损模块。
  • 请确保所选周期都具备足够的历史数据,以便指标在交易前完成预热。
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 JsSignalBaesStrategy : 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 JsSignalBaesStrategy()
	{
		_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;
	}
}