GitHub で見る

Visible Chart Strategy

This sample shows how to work with a visible range of candles. The strategy tracks the highest high and lowest low over a configurable number of recent bars. A breakout above the visible high triggers a long entry, while a break below the visible low triggers a short entry.

Details

  • Entry: Breakout of the visible high/low.
  • Exit: Opposite breakout.
  • Indicators: Highest, Lowest.
using System;
using System.Collections.Generic;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy that trades breakouts of the visible chart range defined by a number of recent bars.
/// </summary>
public class VisibleChartStrategy : Strategy
{
	private readonly StrategyParam<int> _visibleBars;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _highVal;
	private decimal _lowVal;
	private int _cooldown;

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

	public VisibleChartStrategy()
	{
		_visibleBars = Param(nameof(VisibleBars), 40)
			.SetDisplay("Visible Bars", "Number of bars considered visible", "General")
			.SetOptimize(20, 200, 20);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to analyze", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_highVal = 0;
		_lowVal = 0;
		_cooldown = 0;
	}

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

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

		_highVal = 0;
		_lowVal = 0;
		_cooldown = 0;

		var subscription = SubscribeCandles(CandleType);

		subscription
			.Bind(highest, lowest, ProcessCandle)
			.Start();

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

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

		if (_cooldown > 0)
		{
			_cooldown--;
			_highVal = high;
			_lowVal = low;
			return;
		}

		if (_highVal == 0)
		{
			_highVal = high;
			_lowVal = low;
			return;
		}

		var breakoutUp = candle.ClosePrice >= _highVal && Position <= 0;
		var breakoutDown = candle.ClosePrice <= _lowVal && Position >= 0;

		if (breakoutUp)
		{
			BuyMarket();
			_cooldown = 30;
		}
		else if (breakoutDown)
		{
			SellMarket();
			_cooldown = 30;
		}

		_highVal = high;
		_lowVal = low;
	}
}