在 GitHub 上查看

Anands策略

该策略通过上一交易日的日线确定方向。 若前一日收盘价高于其最高价,则考虑做多;若低于最低价,则考虑做空。 在15分钟级别上监测最近的两根已完成K线。 当上一根K线收于两根之前的最高价之上时开多, 当上一根收盘低于两根之前的最低价时开空。

细节

  • 入场条件
    • 前一日收盘价超出其日线范围决定多空偏向。
    • 多头:上一根15分K线收盘价 > 两根之前的最高价。
    • 空头:上一根15分K线收盘价 < 两根之前的最低价。
  • 方向:双向。
  • 出场条件:无,反向信号平仓。
  • 止损:建议放在突破K线的相反端。
  • 默认参数
    • CandleType = 15分钟
  • 过滤器
    • 类型:突破
    • 方向:双向
    • 指标:K线
    • 止损:可选
    • 复杂度:低
    • 时间框架:日内
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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>
/// Anand's breakout strategy based on short-term trend and price level breakouts.
/// Uses EMA for trend and breakout of previous candle high/low for entry.
/// </summary>
public class AnandsStrategy : Strategy
{
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _cooldownBars;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevHigh;
	private decimal _prevLow;
	private int _barIndex;
	private int _lastTradeBar;

	/// <summary>
	/// EMA period for trend filter.
	/// </summary>
	public int EmaLength
	{
		get => _emaLength.Value;
		set => _emaLength.Value = value;
	}

	/// <summary>
	/// Cooldown bars between trades.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public AnandsStrategy()
	{
		_emaLength = Param(nameof(EmaLength), 5)
			.SetDisplay("EMA Length", "EMA trend filter period", "Indicator");

		_cooldownBars = Param(nameof(CooldownBars), 10)
			.SetDisplay("Cooldown Bars", "Bars between trades", "Trading");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Trading timeframe", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHigh = 0;
		_prevLow = 0;
		_barIndex = 0;
		_lastTradeBar = -100;
	}

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

		var ema = new ExponentialMovingAverage { Length = EmaLength };

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

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

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

		_barIndex++;

		if (_prevHigh == 0 || _prevLow == 0)
		{
			_prevHigh = candle.HighPrice;
			_prevLow = candle.LowPrice;
			return;
		}

		var cooldownOk = _barIndex - _lastTradeBar > CooldownBars;
		var upTrend = candle.ClosePrice > emaValue;
		var downTrend = candle.ClosePrice < emaValue;

		// Breakout above previous candle high in uptrend
		if (upTrend && candle.ClosePrice > _prevHigh && Position <= 0 && cooldownOk)
		{
			BuyMarket();
			_lastTradeBar = _barIndex;
		}
		// Breakout below previous candle low in downtrend
		else if (downTrend && candle.ClosePrice < _prevLow && Position >= 0 && cooldownOk)
		{
			SellMarket();
			_lastTradeBar = _barIndex;
		}

		_prevHigh = candle.HighPrice;
		_prevLow = candle.LowPrice;
	}
}