Ver no GitHub

Estratégia de Último Preço

Esta estratégia coloca ordens limitadas na melhor oferta de compra ou venda quando o último preço negociado se afasta por um intervalo definido pelo usuário. Ela monitora atualizações do livro de ordens Level1 e negociações para decidir as entradas.

Detalhes

  • Critérios de entrada:
    • Comprado: Último preço ≥ melhor ask + intervalo.
    • Vendido: Último preço ≤ melhor bid - intervalo.
  • Comprado/Vendido: Ambas as direções.
  • Critérios de saída:
    • Sinal oposto ou fora das sessões de negociação permitidas.
  • Stops: Apenas stop loss.
  • Valores padrão:
    • Interval = 400
    • Min Volume = 1
    • Max Volume = 900000
    • Spread = 200
    • Volume = 1
    • Stop Loss = 400
  • Sessões de negociação:
    • 10:05:40 – 13:54:30
    • 14:08:30 – 15:44:30
    • 16:05:30 – 18:39:30
    • 19:15:10 – 23:44:30
  • Filtros:
    • Categoria: Rompimento
    • Direção: Ambos
    • Indicadores: Nenhum
    • Stops: Sim
    • Complexidade: Intermediário
    • Período: Intradiário
    • Sazonalidade: Não
    • Redes neurais: Não
    • Divergência: Não
    • Nível de risco: Médio
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 enters when price moves away from a moving average by
/// a configurable distance, expecting mean reversion.
/// </summary>
public class LastPriceStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<decimal> _distancePct;

	private decimal _entryPrice;

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

	public int MaPeriod
	{
		get => _maPeriod.Value;
		set => _maPeriod.Value = value;
	}

	public decimal DistancePct
	{
		get => _distancePct.Value;
		set => _distancePct.Value = value;
	}

	public LastPriceStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");

		_maPeriod = Param(nameof(MaPeriod), 20)
			.SetDisplay("MA Period", "Moving average period", "Parameters");

		_distancePct = Param(nameof(DistancePct), 0.5m)
			.SetDisplay("Distance %", "Percent distance from MA to trigger entry", "Parameters");
	}

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

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

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

		_entryPrice = 0;

		var ma = new ExponentialMovingAverage { Length = MaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ma, ProcessCandle)
			.Start();

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

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

		var price = candle.ClosePrice;
		var threshold = maValue * DistancePct / 100m;

		// Exit: price returned to MA
		if (Position > 0 && price >= maValue)
		{
			SellMarket();
			_entryPrice = 0;
		}
		else if (Position < 0 && price <= maValue)
		{
			BuyMarket();
			_entryPrice = 0;
		}

		// Entry: price moved away from MA
		if (Position == 0)
		{
			if (price < maValue - threshold)
			{
				BuyMarket();
				_entryPrice = price;
			}
			else if (price > maValue + threshold)
			{
				SellMarket();
				_entryPrice = price;
			}
		}
	}
}