View on GitHub

SilverTrend V3 JTPO Strategy

Overview

SilverTrend V3 is a trend-following strategy translated from the original MetaTrader 4 implementation. It evaluates the SilverTrend indicator together with the J_TPO statistical filter to identify new directional swings. The strategy trades a single instrument at a time and enforces a Friday evening flat rule to avoid holding risk over the weekend.

Trading Logic

  1. Indicator Processing
    • The strategy keeps a rolling buffer of recent candles and recalculates the SilverTrend direction on every completed bar.
    • SilverTrend uses a 9-bar window and a risk factor of 3 to determine the adaptive channel limits. If the closing price pierces above the upper limit the signal flips to bullish; crossing below the lower limit flips the signal to bearish.
    • The J_TPO calculation (length 14) measures price distribution skewness. Only positive J_TPO values confirm long entries, while negative readings are required for shorts.
  2. Entry Conditions
    • A long trade is opened when the SilverTrend signal changes from bearish to bullish and J_TPO is above zero.
    • A short trade is opened when the SilverTrend signal changes from bullish to bearish and J_TPO is below zero.
    • New positions are blocked on Fridays once the market hour exceeds the configured cutoff.
  3. Exit Management
    • Opposite SilverTrend signals close open trades immediately.
    • Optional initial stop loss and take profit levels are placed at fixed distances (expressed in points).
    • An optional trailing stop follows the price once it moves beyond the configured profit buffer.

Parameters

Name Description Default
Volume Order size in lots. 1
TrailingStopPoints Trailing stop distance in price points. 0 disables trailing. 0
TakeProfitPoints Take profit distance in price points. 0 disables the take profit. 0
InitialStopPoints Initial stop loss distance in price points. 0 disables the protective stop. 0
FridayCutoffHour Hour (exchange time) after which new trades on Friday are disallowed. 16
CandleType Candle type or timeframe used for analysis. 1h candles

Additional Notes

  • Only one position is open at any time, matching the single-trade behavior of the original expert advisor.
  • The implementation uses StockSharp high-level API, so the strategy subscribes to candles and performs logic only on finished bars.
  • Trailing and fixed stops are managed internally and will close the position at market price once triggered.
using System;

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

namespace StockSharp.Samples.Strategies;

public class SilverTrendV3JtpoStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<decimal> _oversold;
	private readonly StrategyParam<decimal> _overbought;
	private readonly StrategyParam<int> _cooldownCandles;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;
	private bool _hasPrev;
	private int _cooldownRemaining;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }
	public decimal Oversold { get => _oversold.Value; set => _oversold.Value = value; }
	public decimal Overbought { get => _overbought.Value; set => _overbought.Value = value; }
	public int CooldownCandles { get => _cooldownCandles.Value; set => _cooldownCandles.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SilverTrendV3JtpoStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14).SetDisplay("RSI Period", "RSI lookback", "Indicators");
		_emaPeriod = Param(nameof(EmaPeriod), 20).SetDisplay("EMA Period", "EMA filter", "Indicators");
		_oversold = Param(nameof(Oversold), 25m).SetDisplay("Oversold", "RSI oversold level", "Levels");
		_overbought = Param(nameof(Overbought), 75m).SetDisplay("Overbought", "RSI overbought level", "Levels");
		_cooldownCandles = Param(nameof(CooldownCandles), 50).SetDisplay("Cooldown", "Candles between signals", "General");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame()).SetDisplay("Candle Type", "Candle timeframe", "General");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevRsi = default;
		_hasPrev = default;
		_cooldownRemaining = default;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_prevRsi = 0;
		_hasPrev = false;
		_cooldownRemaining = 0;

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		var ema = new ExponentialMovingAverage { Length = EmaPeriod };
		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(rsi, ema, ProcessCandle).Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal rsi, decimal ema)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_hasPrev) { _prevRsi = rsi; _hasPrev = true; return; }

		if (_cooldownRemaining > 0)
		{
			_cooldownRemaining--;
			_prevRsi = rsi;
			return;
		}

		var oversold = Oversold;
		var overbought = Overbought;
		var close = candle.ClosePrice;

		if (_prevRsi <= oversold && rsi > oversold && close > ema && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
			_cooldownRemaining = CooldownCandles;
		}
		else if (_prevRsi >= overbought && rsi < overbought && close < ema && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
			_cooldownRemaining = CooldownCandles;
		}
		_prevRsi = rsi;
	}
}