在 GitHub 上查看

JB策略

摘要

JB策略来自一个fxDreema专家顾问,结合长期趋势过滤、动量确认和波动性突破:

  • 趋势过滤: 要求前一根K线的收盘价位于100周期简单移动平均线之上(做多)或之下(做空)。
  • 动量确认: 使用100周期Force Index确认方向(正值做多,负值做空)。
  • 波动触发: 当前一根K线的收盘价突破对应的布林带(20周期,2.0标准差)时触发入场。
  • 仓位管理: 在亏损周期后按照马丁风格提高下次下单量,盈利周期后恢复至基础仓位。
  • 退出规则: 当所有持仓的单合约平均浮盈达到设定目标时全部平仓。

参数

名称 描述
SmaPeriod SMA趋势过滤周期,默认100。
ForcePeriod Force Index指标周期,默认100。
BollingerPeriod 布林带周期,默认20。
BollingerDeviation 布林带标准差倍数,默认2.0。
BaseVolume 马丁调整前的基础下单量,默认0.1。
LossMultiplier 亏损后下一次下单量乘数,默认1.55。
AverageProfitTarget 触发全部平仓的单合约平均浮盈目标,默认2.8。
CandleType 用于计算信号的K线类型,默认1分钟。

信号

多头入场

  1. 前一根K线收盘价≤下轨布林带。
  2. 前一根收盘价高于100周期SMA。
  3. Force Index为正值。

空头入场

  1. 前一根K线收盘价≥上轨布林带。
  2. 前一根收盘价低于100周期SMA。
  3. Force Index为负值。

退出

  • 当所有持仓的单合约平均浮盈达到AverageProfitTarget时,立即市价平仓。
  • 每次仓位归零后,根据上一交易周期的实盈亏调整下次下单量:亏损则按LossMultiplier放大,盈利则恢复到BaseVolume

说明

  • 马丁增仓依赖实盘损益判断亏损周期,使用前请确认标的适合提高仓位。
  • StockSharp策略使用净头寸表示仓位,因此原始MQL版本允许的对冲在此实现中通过净仓位近似处理。
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 JbStrategy : 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 JbStrategy()
	{
		_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;
	}
}