A trend-following strategy based on the custom SilverTrend indicator. The indicator builds a dynamic price channel using the highest high and lowest low over a lookback window and a risk factor. A trading signal occurs when price crosses the channel and the trend direction reverses.
Details
Entry: Buy when the indicator switches to an uptrend. Sell when the indicator switches to a downtrend.
Exit: Position reverses on the opposite signal.
Indicators: Highest, Lowest, SimpleMovingAverage (inside the SilverTrend calculation).
Stops: None.
Default Values:
Ssp = 9 — number of bars for channel calculation.
Risk = 3 — percentage that shrinks the channel width.
CandleType = 1 hour candles.
Direction: Both long and short.
The SilverTrend indicator computes the average high-low range over Ssp + 1 bars and finds the highest high and lowest low over Ssp bars. The channel boundaries are:
If the close falls below smin, the trend turns bearish. If the close rises above smax, the trend turns bullish. A signal is generated when the trend flips, and the strategy immediately reverses its position accordingly.
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>
/// Strategy based on SilverTrend indicator -- trades reversals based on
/// price channel breakouts with a risk-based filter.
/// </summary>
public class SilverTrendStrategy : Strategy
{
private readonly StrategyParam<int> _ssp;
private readonly StrategyParam<int> _risk;
private readonly StrategyParam<DataType> _candleType;
private Highest _highest;
private Lowest _lowest;
private bool? _uptrend;
private bool? _prevUptrend;
public int Ssp { get => _ssp.Value; set => _ssp.Value = value; }
public int Risk { get => _risk.Value; set => _risk.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public SilverTrendStrategy()
{
_ssp = Param(nameof(Ssp), 9)
.SetGreaterThanZero()
.SetDisplay("SSP", "Lookback length for price channel", "Indicator");
_risk = Param(nameof(Risk), 3)
.SetGreaterThanZero()
.SetDisplay("Risk", "Risk factor used to tighten the channel", "Indicator");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for indicator", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_highest = null;
_lowest = null;
_uptrend = null;
_prevUptrend = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_highest = new Highest { Length = Ssp };
_lowest = new Lowest { Length = Ssp };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_highest, _lowest, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal maxHigh, decimal minLow)
{
if (candle.State != CandleStates.Finished)
return;
if (!_highest.IsFormed || !_lowest.IsFormed)
return;
var k = 33 - Risk;
var smin = minLow + (maxHigh - minLow) * k / 100m;
var smax = maxHigh - (maxHigh - minLow) * k / 100m;
var uptrend = _uptrend ?? false;
if (candle.ClosePrice < smin)
uptrend = false;
else if (candle.ClosePrice > smax)
uptrend = true;
var reversed = _uptrend is not null && uptrend != _uptrend;
if (IsFormedAndOnlineAndAllowTrading() && reversed)
{
if (uptrend && Position <= 0)
BuyMarket();
else if (!uptrend && Position >= 0)
SellMarket();
}
_prevUptrend = _uptrend;
_uptrend = uptrend;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class silver_trend_strategy(Strategy):
def __init__(self):
super(silver_trend_strategy, self).__init__()
self._ssp = self.Param("Ssp", 9) \
.SetDisplay("SSP", "Lookback length for price channel", "Indicator")
self._risk = self.Param("Risk", 3) \
.SetDisplay("Risk", "Risk factor used to tighten the channel", "Indicator")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for indicator", "General")
self._uptrend = None
@property
def ssp(self):
return self._ssp.Value
@property
def risk(self):
return self._risk.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(silver_trend_strategy, self).OnReseted()
self._uptrend = None
def OnStarted2(self, time):
super(silver_trend_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self.ssp
lowest = Lowest()
lowest.Length = self.ssp
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(highest, lowest, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def process_candle(self, candle, max_high, min_low):
if candle.State != CandleStates.Finished:
return
max_high = float(max_high)
min_low = float(min_low)
k = 33 - self.risk
smin = min_low + (max_high - min_low) * k / 100.0
smax = max_high - (max_high - min_low) * k / 100.0
close = float(candle.ClosePrice)
uptrend = self._uptrend if self._uptrend is not None else False
if close < smin:
uptrend = False
elif close > smax:
uptrend = True
reversed_trend = self._uptrend is not None and uptrend != self._uptrend
if reversed_trend:
if uptrend and self.Position <= 0:
self.BuyMarket()
elif not uptrend and self.Position >= 0:
self.SellMarket()
self._uptrend = uptrend
def CreateClone(self):
return silver_trend_strategy()