在 GitHub 上查看

SHE Kanskigor 策略

该日内策略每天只交易一次,依据前一日蜡烛的方向。在设定时间,若前一日收盘价低于开盘价则买入;若收盘价高于开盘价则卖出。使用固定点数的止盈和止损来控制风险,每天仅允许一笔交易。

细节

  • 入场条件:在 StartTime 比较前一日的开盘与收盘;open > close 时买入,open < close 时卖出
  • 多/空:双向
  • 离场条件:止盈或止损
  • 止损:是
  • 默认值
    • Volume = 0.1
    • StartTime = 00:05
    • TakeProfit = 350
    • StopLoss = 550
  • 过滤器
    • 类别:反转
    • 方向:双向
    • 指标:无
    • 止损:有
    • 复杂度:基础
    • 时间框架:日线
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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>
/// Strategy that trades based on consecutive same-direction candles with EMA filter.
/// </summary>
public class SheKanskigorStrategy : Strategy
{
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevOpen;
	private decimal _prevClose;
	private decimal _prevPrevOpen;
	private decimal _prevPrevClose;
	private int _candleCount;

	public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SheKanskigorStrategy()
	{
		_emaPeriod = Param(nameof(EmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "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();
		_prevOpen = 0;
		_prevClose = 0;
		_prevPrevOpen = 0;
		_prevPrevClose = 0;
		_candleCount = 0;
	}

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

		var ema = new ExponentialMovingAverage { Length = EmaPeriod };
		SubscribeCandles(CandleType).Bind(ema, ProcessCandle).Start();
	}

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

		_candleCount++;

		if (_candleCount < 3)
		{
			_prevPrevOpen = _prevOpen;
			_prevPrevClose = _prevClose;
			_prevOpen = candle.OpenPrice;
			_prevClose = candle.ClosePrice;
			return;
		}

		// Two consecutive bearish candles -> buy reversal (with EMA confirmation)
		var twoBearish = _prevPrevOpen > _prevPrevClose && _prevOpen > _prevClose;
		// Two consecutive bullish candles -> sell reversal
		var twoBullish = _prevPrevOpen < _prevPrevClose && _prevOpen < _prevClose;

		if (twoBearish && candle.ClosePrice > emaValue && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (twoBullish && candle.ClosePrice < emaValue && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevPrevOpen = _prevOpen;
		_prevPrevClose = _prevClose;
		_prevOpen = candle.OpenPrice;
		_prevClose = candle.ClosePrice;
	}
}