在 GitHub 上查看

Xbug Free V4 策略

该策略在移动平均线穿越自身的价格中位数时开仓。入场后在固定距离处设置对称的止盈和止损。

细节

  • 入场条件
    • 多头:移动平均线高于价格中位数且两根K线前在其下方
    • 空头:移动平均线低于价格中位数且两根K线前在其上方
  • 方向:双向
  • 出场条件
    • 在距入场价 StopPoints 的位置止盈
    • 在相同距离的相反方向止损
  • 止损:是
  • 默认值
    • MaPeriod = 19
    • StopPoints = 270
    • Volume = 0.1m
    • CandleType = TimeSpan.FromHours(4).TimeFrame()
  • 筛选
    • 类型:Crossover
    • 方向:双向
    • 指标:SMA
    • 止损:是
    • 复杂度:基础
    • 时间框架:长期
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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>
/// Xbug Free v4 strategy based on SMA crossing median price.
/// </summary>
public class XbugFreeV4Strategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevSma;
	private decimal _prevPrice;
	private decimal _prev2Sma;
	private decimal _prev2Price;
	private int _barCount;

	public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public XbugFreeV4Strategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Moving average length", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevSma = 0; _prevPrice = 0;
		_prev2Sma = 0; _prev2Price = 0;
		_barCount = 0;
	}

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

		var sma = new SimpleMovingAverage { Length = MaPeriod };
		var stdev = new StandardDeviation { Length = 14 };

		SubscribeCandles(CandleType).Bind(sma, stdev, ProcessCandle).Start();
	}

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

		var median = (candle.HighPrice + candle.LowPrice) / 2m;
		_barCount++;

		if (_barCount >= 3)
		{
			var buySignal = smaValue > median && _prevSma > _prevPrice && _prev2Sma < _prev2Price;
			var sellSignal = smaValue < median && _prevSma < _prevPrice && _prev2Sma > _prev2Price;

			if (buySignal && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			else if (sellSignal && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}

		_prev2Sma = _prevSma;
		_prev2Price = _prevPrice;
		_prevSma = smaValue;
		_prevPrice = median;
	}
}