在 GitHub 上查看

Straddle News 策略

该策略用于重大新闻时的高波动行情。它在当前价格上下放置对称的 Buy Stop 和 Sell Stop 挂单,以捕捉突破。当其中一单被触发后,另一单会被取消,并使用追踪止损保护头寸。

细节

  • 入场条件: 当点差低于 SpreadOperation 时,分别在 Ask + PipsAway 点和 Bid - PipsAway 点放置 Buy Stop 和 Sell Stop
  • 多空方向: 双向
  • 出场条件: 保护性止损、止盈或价格回撤 TrailingStop 点触发的追踪止损
  • 止损: 初始止损和止盈通过 StartProtection 设置;追踪止损在代码中实现
  • 默认值:
    • StopLoss = 100
    • TakeProfit = 300
    • TrailingStop = 50
    • PipsAway = 50
    • BalanceUsed = 0.01
    • SpreadOperation = 25
    • Leverage = 400
  • 过滤器:
    • 分类: Breakout
    • 方向: 双向
    • 指标: 无
    • 止损: 有
    • 复杂度: 基础
    • 时间框架: Level1 / Tick
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 高

工作流程

  1. 订阅 Level1 行情以获取最新 Bid 和 Ask。
  2. 当点差满足条件时,依据账户价值、LeverageBalanceUsed 计算交易量。
  3. PipsAway 的偏移放置 Buy Stop 和 Sell Stop。
  4. 当持仓打开后,取消未触发的另一张挂单。
  5. 根据 StopLossTakeProfit 附加止损与止盈。
  6. 跟踪进场后的最高/最低价,若价格回撤超过 TrailingStop 点则平仓。
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;

/// <summary>
/// EMA crossover strategy.
/// </summary>
public class StraddleNewsStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public StraddleNewsStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Indicators");
		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}