在 GitHub 上查看

Breakout 04 策略 (中文)

该策略交易前一日区间的突破。 当价格突破前一天最高价时做多,跌破前一天最低价时做空。 使用追踪止损和固定止盈,并可根据账户资金调整下单量。 在周一设定时间之前和周五设定时间之后停止交易。

细节

  • 入场条件:
    • 多头: 价格 > 前高
    • 空头: 价格 < 前低
  • 多空: 双向
  • 出场条件: 追踪止损或止盈
  • 止损: 追踪止损与固定止损
  • 默认参数:
    • MondayHour = 18
    • FridayHour = 14
    • TrailingStop = 21
    • TakeProfit = 550
    • StopLoss = 124
    • UseMoneyManagement = false
    • PercentMM = 8m
    • Volume = 0.1m
  • 过滤器:
    • 分类: Breakout
    • 方向: 双向
    • 指标: 无
    • 止损: 有
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险等级: 中等
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>
/// Breakout strategy using Highest/Lowest channels.
/// </summary>
public class Breakout04Strategy : Strategy
{
	private readonly StrategyParam<int> _lookback;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevHigh;
	private decimal _prevLow;
	private bool _hasPrev;

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

	public Breakout04Strategy()
	{
		_lookback = Param(nameof(Lookback), 30)
			.SetDisplay("Lookback", "Channel lookback period", "General");

		_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();
		_prevHigh = 0;
		_prevLow = 0;
		_hasPrev = false;
	}

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

		var highest = new Highest { Length = Lookback };
		var lowest = new Lowest { Length = Lookback };

		SubscribeCandles(CandleType).Bind(highest, lowest, ProcessCandle).Start();
	}

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

		if (!_hasPrev)
		{
			_prevHigh = highest;
			_prevLow = lowest;
			_hasPrev = true;
			return;
		}

		var close = candle.ClosePrice;

		if (close > _prevHigh && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (close < _prevLow && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevHigh = highest;
		_prevLow = lowest;
	}
}