在 GitHub 上查看

外部柱策略

该策略交易外部柱突破。当当前蜡烛的最高点高于前一根且最低点低于前一根时出现外部柱。多头在柱内设置买入止损,空头设置卖出止损。支持部分止盈和保本移动。

详情

  • 入场条件:外部柱形态并判断方向。
  • 多空:双向。
  • 出场条件:根据柱范围计算的止损或止盈。
  • 止损:有。
  • 默认值
    • CandleType = 5 分钟
    • EntryPercentage = 0.5
    • TpPercentage = 1
    • PartialRR = 1
    • PartialExitPercent = 0.5
    • StopLossOffset = 10
  • 过滤器
    • 类别:形态
    • 方向:双向
    • 指标:K线
    • 止损:有
    • 复杂度:中等
    • 时间框架:日内
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险级别:中等
using System;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

public class OutsideBarStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

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

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

	public OutsideBarStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHigh = 0;
		_prevLow = 0;
		_hasPrev = false;
	}

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

		_prevHigh = 0;
		_prevLow = 0;
		_hasPrev = false;

		var sma = new SimpleMovingAverage { Length = 20 };
		var lastSignal = DateTimeOffset.MinValue;
		var cooldown = TimeSpan.FromMinutes(360);

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(sma, (candle, smaVal) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				if (!sma.IsFormed)
					return;

				if (!_hasPrev)
				{
					_prevHigh = candle.HighPrice;
					_prevLow = candle.LowPrice;
					_hasPrev = true;
					return;
				}

				var isOutsideBar = candle.HighPrice > _prevHigh && candle.LowPrice < _prevLow;

				if (isOutsideBar && candle.OpenTime - lastSignal >= cooldown)
				{
					var isBullish = candle.ClosePrice > candle.OpenPrice;

					if (isBullish && candle.ClosePrice > smaVal && Position <= 0)
					{
						BuyMarket();
						lastSignal = candle.OpenTime;
					}
					else if (!isBullish && candle.ClosePrice < smaVal && Position >= 0)
					{
						SellMarket();
						lastSignal = candle.OpenTime;
					}
				}

				_prevHigh = candle.HighPrice;
				_prevLow = candle.LowPrice;
			})
			.Start();

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