Открыть на GitHub

Стратегия OsMaSter V0

Общее описание

  • Конвертирована из эксперта MetaTrader 5 «OsMaSter v0» (файл MQL OsMaSter v0.mq5).
  • Использует паттерн индикатора MACD Histogram (OsMA) для поиска разворотов импульса после локальной консолидации.
  • Работает с одним инструментом и таймфреймом, который задаётся параметром CandleType.
  • Автоматически преобразует стоп-лосс и тейк-профит, заданные в пунктах, в абсолютные ценовые значения с учётом шага цены и количества знаков инструмента.

Параметры

Имя Значение по умолчанию Описание
FastPeriod 9 Длина быстрой EMA для расчёта гистограммы MACD.
SlowPeriod 26 Длина медленной EMA для расчёта гистограммы MACD.
SignalPeriod 5 Период сигнальной EMA, сглаживающей гистограмму.
StopLossPips 30 Размер защитного стопа в пунктах. Значение 0 отключает стоп.
TakeProfitPips 50 Размер тейк-профита в пунктах. Значение 0 отключает цель.
TradeVolume 1 Объём заявки (лотов), используемый при входе по рынку.
CandleType Свечи 15 минут Таймфрейм, по которому рассчитываются индикаторы.

Логика сигналов

  1. Стратегия хранит четыре последние величины гистограммы MACD (hist0 — текущая, hist1 — предыдущая, ..., hist3 — три свечи назад).
  2. Покупка выполняется, когда hist3 > hist2, hist2 < hist1 и hist1 < hist0, что соответствует формированию локального минимума и росту гистограммы.
  3. Продажа выполняется, когда hist3 < hist2, hist2 > hist1 и hist1 > hist0, что соответствует локальному максимуму и снижению гистограммы.
  4. Одновременно может быть открыта только одна позиция, новые сигналы игнорируются до закрытия текущей сделки.

Управление позицией

  • Ордера выставляются методами BuyMarket() и SellMarket() с объёмом TradeVolume.
  • Метод StartProtection прикрепляет стоп-лосс и тейк-профит, рассчитывая ценовое смещение из заданных пунктов. Для инструментов с 3 или 5 знаками используется правило «шаг цены × 10», иначе берётся сам шаг цены.
  • Дополнительных правил выхода нет: позиции закрываются защитными ордерами или вручную.

Примечания

  • Убедитесь, что у инструмента корректно заданы PriceStep и Decimals, иначе пересчёт пунктов может отличаться от спецификации брокера.
  • Подберите подходящий таймфрейм и объём в зависимости от цели торговли.
  • Пока открыта позиция, повторные сигналы в ту же сторону пропускаются, так как стратегия ожидает исполнения стопа или тейк-профита.
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// OsMA histogram pattern strategy converted from the "OsMaSter v0" MQL expert.
/// Enters on four-bar momentum reversals identified by the MACD histogram.
/// </summary>
public class OsMaSterV0Strategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _signalPeriod;
	private readonly StrategyParam<int> _stopLossPips;
	private readonly StrategyParam<int> _takeProfitPips;
	private readonly StrategyParam<decimal> _tradeVolume;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _histCurrent;
	private decimal? _histPrev1;
	private decimal? _histPrev2;
	private decimal? _histPrev3;

	/// <summary>
	/// Initializes a new instance of the strategy.
	/// </summary>
	public OsMaSterV0Strategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 9)
			.SetDisplay("Fast EMA", "Fast EMA period for MACD histogram", "Indicators")
			.SetGreaterThanZero();

		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetDisplay("Slow EMA", "Slow EMA period for MACD histogram", "Indicators")
			.SetGreaterThanZero();

		_signalPeriod = Param(nameof(SignalPeriod), 5)
			.SetDisplay("Signal Smoothing", "Signal moving average period", "Indicators")
			.SetGreaterThanZero();

		_stopLossPips = Param(nameof(StopLossPips), 500)
			.SetDisplay("Stop Loss (pips)", "Stop loss distance in pips", "Risk");

		_takeProfitPips = Param(nameof(TakeProfitPips), 1000)
			.SetDisplay("Take Profit (pips)", "Take profit distance in pips", "Risk");

		_tradeVolume = Param(nameof(TradeVolume), 1m)
			.SetDisplay("Trade Volume", "Order volume in lots", "Trading")
			.SetGreaterThanZero();

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for calculations", "General");

		Volume = TradeVolume;
	}

	/// <summary>
	/// Fast EMA period used in MACD histogram calculation.
	/// </summary>
	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	/// <summary>
	/// Slow EMA period used in MACD histogram calculation.
	/// </summary>
	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}

	/// <summary>
	/// Signal moving average period.
	/// </summary>
	public int SignalPeriod
	{
		get => _signalPeriod.Value;
		set => _signalPeriod.Value = value;
	}

	/// <summary>
	/// Stop loss size expressed in pips.
	/// </summary>
	public int StopLossPips
	{
		get => _stopLossPips.Value;
		set => _stopLossPips.Value = value;
	}

	/// <summary>
	/// Take profit size expressed in pips.
	/// </summary>
	public int TakeProfitPips
	{
		get => _takeProfitPips.Value;
		set => _takeProfitPips.Value = value;
	}

	/// <summary>
	/// Trading volume used for market orders.
	/// </summary>
	public decimal TradeVolume
	{
		get => _tradeVolume.Value;
		set
		{
			_tradeVolume.Value = value;
			Volume = value;
		}
	}

	/// <summary>
	/// Candle type used for indicator calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_histCurrent = null;
		_histPrev1 = null;
		_histPrev2 = null;
		_histPrev3 = null;

		Volume = TradeVolume;
	}

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

		Volume = TradeVolume;

		var macd = new MovingAverageConvergenceDivergenceHistogram
		{
			Macd =
			{
				ShortMa = { Length = FastPeriod },
				LongMa = { Length = SlowPeriod }
			},
			SignalMa = { Length = SignalPeriod }
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(macd, ProcessCandle)
			.Start();

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

		var step = Security?.PriceStep ?? 1m;
		var decimals = Security?.Decimals ?? 0;
		var pipSize = (decimals == 3 || decimals == 5) ? step * 10m : step;

		// Convert pip-based risk controls into absolute price offsets.
		StartProtection(
			takeProfit: TakeProfitPips > 0 ? new Unit(TakeProfitPips * pipSize, UnitTypes.Absolute) : null,
			stopLoss: StopLossPips > 0 ? new Unit(StopLossPips * pipSize, UnitTypes.Absolute) : null);
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue macdValue)
	{
		// Ignore incomplete candles because signals rely on closed data.
		if (candle.State != CandleStates.Finished)
			return;

		// Ensure the strategy is synchronized with the market and permitted to trade.
		if (!macdValue.IsFormed)
			return;

		var macdTyped = (MovingAverageConvergenceDivergenceHistogramValue)macdValue;
		if (macdTyped.Macd is not decimal histogram)
			return;

		// Shift stored histogram values to maintain the last four observations.
		_histPrev3 = _histPrev2;
		_histPrev2 = _histPrev1;
		_histPrev1 = _histCurrent;
		_histCurrent = histogram;

		if (_histCurrent is not decimal hist0 ||
			_histPrev1 is not decimal hist1 ||
			_histPrev2 is not decimal hist2 ||
			_histPrev3 is not decimal hist3)
		{
			return;
		}

		// Detect the specific four-bar rising pattern used for long entries.
		var bullishPattern = hist3 > hist2 && hist2 < hist1 && hist1 < hist0;

		// Detect the mirrored falling pattern used for short entries.
		var bearishPattern = hist3 < hist2 && hist2 > hist1 && hist1 > hist0;

		// The original expert opens a position only when no trades are active.
		if (Position != 0)
			return;

		if (bullishPattern)
		{
			// Enter long when the histogram forms a higher-low sequence.
			BuyMarket();
		}
		else if (bearishPattern)
		{
			// Enter short when the histogram forms a lower-high sequence.
			SellMarket();
		}
	}
}