在 GitHub 上查看

VR Overturn 策略

VR Overturn 实现了简单的马丁格尔与反马丁格尔逻辑。 策略始终只持有一个仓位,仓位平仓后会立即根据上一笔交易 的结果开立新的仓位。

策略逻辑

  1. 按照 StartSide 方向以 StartVolume 手数开仓。
  2. 以点数偏移设置止损与止盈。
  3. 仓位平仓后:
    • 计算上一笔交易的盈亏。
    • Martingale 模式:
      • 盈利 → 手数重置为 StartVolume,方向保持不变。
      • 亏损 → 手数乘以 Multiplier,方向反转。
    • AntiMartingale 模式:
      • 盈利 → 手数乘以 Multiplier,方向保持不变。
      • 亏损 → 手数重置为 StartVolume,方向反转。
  4. 使用计算出的方向和手数开立下一笔仓位。

该过程在策略运行期间持续重复。

参数

名称 描述
Mode 交易模式:MartingaleAntiMartingale
StartSide 首笔交易方向(BuySell)。
TakeProfit 距离入场价的止盈点数。
StopLoss 距离入场价的止损点数。
StartVolume 首笔交易使用的初始手数。
Multiplier 盈利或亏损后应用的手数倍数。

备注

  • 保护性订单以止损和限价订单形式发送。
  • 任意时刻只有一个仓位。
  • 策略不使用任何市场指标。
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>
/// Overturn strategy using EMA crossover.
/// </summary>
public class VROverturnStrategy : 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 VROverturnStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "General");
		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "General");
		_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;
	}
}