GitHub で見る

EMA WPRトレンド戦略

EMAトレンドフィルターとWilliams %Rシグナルを組み合わせた戦略。売られすぎレベルで買い、買われすぎレベルで売ります。押し目しきい値によって連続したエントリーを防ぎます。オプションのエグジットは、Williams %Rの反対の極値で、または複数の不採算バー後にトレードを決済します。

詳細

  • エントリー条件:
    • ロング: Williams %R <= -100 かつ EMAトレンドが上向き
    • ショート: Williams %R >= 0 かつ EMAトレンドが下向き
  • ロング/ショート: 両方
  • エグジット条件:
    • UseWprExitが有効な場合、Williams %Rが反対の極値を突破
    • UseUnprofitExitが有効な場合、ポジションがMaxUnprofitBars本のバーにわたって不採算
  • ストップ: いいえ
  • デフォルト値:
    • WprPeriod = 46
    • WprRetracement = 30
    • EmaPeriod = 144
    • BarsInTrend = 1
    • MaxUnprofitBars = 5
  • フィルター:
    • カテゴリ: 平均回帰
    • 方向: 両方
    • インジケーター: EMA, Williams %R
    • ストップ: いいえ
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Strategy combining EMA trend filter with Williams %R signals.
/// Buys at oversold levels and sells at overbought levels.
/// </summary>
public class EmaWprTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<int> _wprPeriod;
	private readonly StrategyParam<decimal> _wprRetracement;
	private readonly StrategyParam<DataType> _candleType;

	private bool _buyAllowed;
	private bool _sellAllowed;
	private decimal _entryPrice;
	private decimal _prevEma;
	private bool _hasPrevEma;
	private int _trendCounter;

	public int EmaPeriod
	{
		get => _emaPeriod.Value;
		set => _emaPeriod.Value = value;
	}

	public int WprPeriod
	{
		get => _wprPeriod.Value;
		set => _wprPeriod.Value = value;
	}

	public decimal WprRetracement
	{
		get => _wprRetracement.Value;
		set => _wprRetracement.Value = value;
	}

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

	public EmaWprTrendStrategy()
	{
		_emaPeriod = Param(nameof(EmaPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "Period for EMA", "Indicators");

		_wprPeriod = Param(nameof(WprPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("WPR Period", "%R length", "Indicators");

		_wprRetracement = Param(nameof(WprRetracement), 30m)
			.SetNotNegative()
			.SetDisplay("WPR Retracement", "Retracement for next trade", "Signals");

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

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_buyAllowed = true;
		_sellAllowed = true;
		_entryPrice = 0;
		_prevEma = 0;
		_hasPrevEma = false;
		_trendCounter = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var ema = new ExponentialMovingAverage { Length = EmaPeriod };
		var wpr = new WilliamsR { Length = WprPeriod };

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

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

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

		// Update trend
		if (_hasPrevEma)
		{
			if (emaValue > _prevEma)
				_trendCounter = Math.Min(_trendCounter + 1, 1);
			else if (emaValue < _prevEma)
				_trendCounter = Math.Max(_trendCounter - 1, -1);
			else
				_trendCounter = 0;
		}
		_prevEma = emaValue;
		_hasPrevEma = true;

		var price = candle.ClosePrice;

		// Exit: WPR at opposite extreme
		if (Position > 0 && wprValue >= -20)
		{
			SellMarket();
			_entryPrice = 0;
		}
		else if (Position < 0 && wprValue <= -80)
		{
			BuyMarket();
			_entryPrice = 0;
		}

		// Retracement flags
		if (wprValue > -100 + WprRetracement)
			_buyAllowed = true;

		if (wprValue < 0 - WprRetracement)
			_sellAllowed = true;

		var trendUp = _trendCounter >= 1;
		var trendDown = _trendCounter <= -1;

		if (Position <= 0 && _buyAllowed && wprValue <= -80 && trendUp)
		{
			BuyMarket();
			_entryPrice = price;
			_buyAllowed = false;
		}
		else if (Position >= 0 && _sellAllowed && wprValue >= -20 && trendDown)
		{
			SellMarket();
			_entryPrice = price;
			_sellAllowed = false;
		}
	}
}