在 GitHub 上查看

Enhanced Doji Candle 策略

该策略基于十字星形态配合简单的确认规则和固定的风险收益比。出现十字星时,如果本根或上一根K线收盘突破开盘价且影线较短,则顺势开仓。止损以点数设定,止盈按风险收益系数计算。

详情

  • 入场条件: 十字星(实体 <= 区间的30%)。若为阳线且下影线<=1%或上一根阳线,则做多;若为阴线且上影线<=1%或上一根阴线,则做空。
  • 多空方向: 双向。
  • 出场条件: 止盈、止损或任意新的十字星。
  • 止损: 有。
  • 默认值:
    • RiskRewardRatio = 2.0m
    • StopLossPips = 5
    • SmaPeriod = 20
    • CandleType = TimeSpan.FromMinutes(1)
  • 过滤器:
    • 类别: Candlestick
    • 方向: Both
    • 指标: 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>
/// Enhanced doji candle strategy.
/// A doji has a body of at most 30% of its range. When flat, a doji goes long if it is bullish with a lower wick of at most 1% of
/// its range or the previous candle was bullish, and short if it is bearish with an upper wick of at most 1% or the previous
/// candle was bearish. While a position is open any new doji closes it. The stop lies StopLossPips price steps from the entry and
/// the take profit RiskRewardRatio times that distance. The SMA is drawn for reference.
/// </summary>
public class EnhancedDojiCandleStrategy : Strategy
{
	private const decimal _dojiBodyPercent = 30m;
	private const decimal _wickPercent = 1m;

	private readonly StrategyParam<decimal> _riskRewardRatio;
	private readonly StrategyParam<int> _stopLossPips;
	private readonly StrategyParam<int> _smaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private ICandleMessage _prevCandle;

	/// <summary>
	/// Take profit distance in multiples of the stop distance.
	/// </summary>
	public decimal RiskRewardRatio
	{
		get => _riskRewardRatio.Value;
		set => _riskRewardRatio.Value = value;
	}

	/// <summary>
	/// Stop loss in price steps.
	/// </summary>
	public int StopLossPips
	{
		get => _stopLossPips.Value;
		set => _stopLossPips.Value = value;
	}

	/// <summary>
	/// SMA period.
	/// </summary>
	public int SmaPeriod
	{
		get => _smaPeriod.Value;
		set => _smaPeriod.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public EnhancedDojiCandleStrategy()
	{
		_riskRewardRatio = Param(nameof(RiskRewardRatio), 2.0m)
			.SetGreaterThanZero()
			.SetDisplay("Risk Reward", "Take profit in multiples of the stop distance", "Risk");

		_stopLossPips = Param(nameof(StopLossPips), 5)
			.SetNotNegative()
			.SetDisplay("Stop Loss Pips", "Stop loss in price steps", "Risk");

		_smaPeriod = Param(nameof(SmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("SMA Period", "SMA period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevCandle = null;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_prevCandle = null;

		var sma = new SimpleMovingAverage { Length = SmaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(sma, ProcessCandle)
			.Start();

		var stopDistance = StopLossPips * (Security?.PriceStep ?? 1m);
		StartProtection(
			stopDistance > 0m ? new Unit(stopDistance * RiskRewardRatio, UnitTypes.Absolute) : new Unit(),
			stopDistance > 0m ? new Unit(stopDistance, UnitTypes.Absolute) : new Unit(),
			useMarketOrders: true);

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, sma);
			DrawOwnTrades(area);
		}
	}

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

		var prev = _prevCandle;
		_prevCandle = candle;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var range = candle.HighPrice - candle.LowPrice;
		if (range <= 0m)
			return;

		var open = candle.OpenPrice;
		var close = candle.ClosePrice;
		var isDoji = Math.Abs(close - open) / range * 100m <= _dojiBodyPercent;

		if (!isDoji)
			return;

		if (Position > 0)
		{
			SellMarket(Position);
			return;
		}

		if (Position < 0)
		{
			BuyMarket(-Position);
			return;
		}

		var lowerWick = (Math.Min(open, close) - candle.LowPrice) / range * 100m;
		var upperWick = (candle.HighPrice - Math.Max(open, close)) / range * 100m;
		var prevBullish = prev != null && prev.ClosePrice > prev.OpenPrice;
		var prevBearish = prev != null && prev.ClosePrice < prev.OpenPrice;

		if ((close > open && lowerWick <= _wickPercent) || prevBullish)
			BuyMarket(Volume);
		else if ((close < open && upperWick <= _wickPercent) || prevBearish)
			SellMarket(Volume);
	}
}