Ver no GitHub

Arrows & Curves Strategy

Overview

This strategy is a conversion of the MQL5 expert advisor Exp_Arrows_Curves. It builds a dynamic price channel using recent highs and lows and reacts to breakouts. The strategy can open or close positions depending on user permissions and trend direction.

Strategy Logic

  • Calculate highest high and lowest low over the configured period.
  • Expand the range by a percentage to form outer channel lines.
  • Create inner stop lines using an additional percentage.
  • When price breaks above the upper channel, go long; when it breaks below the lower channel, go short.
  • Inner stop lines trigger position exits when the opposite side of the channel is crossed.

Parameters

  • SspPeriod – lookback period for highs and lows.
  • Channel – expansion percentage for main channel lines.
  • StopChannel – additional percent used for inner stop lines.
  • CandleType – candle timeframe.
  • BuyPosOpen / SellPosOpen – allow opening long/short positions.
  • BuyPosClose / SellPosClose – allow closing long/short positions.

Indicators

  • Highest
  • Lowest

Notes

The strategy operates on finished candles only. Stop loss and take profit management are not included; exits rely on channel crossings.

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>
/// Channel breakout strategy using Highest/Lowest midpoint crossing.
/// </summary>
public class ArrowsCurvesStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;

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

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

	public ArrowsCurvesStrategy()
	{
		_period = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Channel period", "Parameters");

		_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 = Period };
		var lowest = new Lowest { Length = Period };

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

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

		if (!_hasPrev)
		{
			_prevHigh = high;
			_prevLow = low;
			_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 = high;
		_prevLow = low;
	}
}