在 GitHub 上查看

IU 4 Bar UP Strategy

IU 4 Bar UP Strategy 是一种仅做多策略,当价格位于 SuperTrend 上方并出现连续四根上涨 K 线时买入。

细节

  • 数据:价格K线。
  • 入场条件
    • 多头:连续四根上涨 K 线并且收盘价在 SuperTrend 之上。
  • 出场条件:收盘价跌破 SuperTrend。
  • 止损:无。
  • 默认值
    • SupertrendLength = 14
    • SupertrendMultiplier = 1
  • 过滤器
    • 类别:趋势跟随
    • 方向:多头
    • 指标:SuperTrend
    • 复杂度:低
    • 风险等级:中等
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>
/// Long-only strategy that buys after four consecutive bullish candles when the price is above the SuperTrend.
/// </summary>
public class Iu4BarUpStrategy : Strategy
{
	private readonly StrategyParam<int> _supertrendLength;
	private readonly StrategyParam<decimal> _supertrendMultiplier;
	private readonly StrategyParam<DataType> _candleType;

	private bool _prevBull1;
	private bool _prevBull2;
	private bool _prevBull3;

	public Iu4BarUpStrategy()
	{
		_supertrendLength = Param(nameof(SupertrendLength), 14)
			.SetDisplay("SuperTrend ATR Period", "ATR period for SuperTrend", "General")
			;

		_supertrendMultiplier = Param(nameof(SupertrendMultiplier), 1m)
			.SetDisplay("SuperTrend ATR Factor", "ATR factor for SuperTrend", "General")
			;

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

	public int SupertrendLength
	{
		get => _supertrendLength.Value;
		set => _supertrendLength.Value = value;
	}

	public decimal SupertrendMultiplier
	{
		get => _supertrendMultiplier.Value;
		set => _supertrendMultiplier.Value = value;
	}

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_prevBull1 = false;
		_prevBull2 = false;
		_prevBull3 = false;
	}

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

		var supertrend = new SuperTrend
		{
			Length = SupertrendLength,
			Multiplier = SupertrendMultiplier
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
		.BindEx(supertrend, ProcessCandle)
		.Start();

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

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

		if (!stValue.IsFinal)
		return;

		if (stValue.IsEmpty) return;
		var st = stValue.ToDecimal();

		var bullish = candle.ClosePrice > candle.OpenPrice;
		var fourBull = bullish && _prevBull1 && _prevBull2 && _prevBull3;

		if (Position <= 0 && fourBull && candle.ClosePrice > st)
		BuyMarket();

		if (Position > 0 && candle.ClosePrice < st)
		SellMarket();

		_prevBull3 = _prevBull2;
		_prevBull2 = _prevBull1;
		_prevBull1 = bullish;
	}
}