VIDYA ProTrend Multi-Tier Profit Strategy
Trend-following strategy using fast and slow VIDYA averages with a Bollinger Band filter. Optional multi-step take profit orders are placed using ATR multiples and percentage targets.
Details
- Entry Criteria: fast VIDYA above slow VIDYA with price outside Bollinger filter
- Long/Short: Both
- Exit Criteria: opposite slope or cross
- Stops: No
- Default Values:
FastVidyaLength= 10SlowVidyaLength= 30MinSlopeThreshold= 0.05
- Filters:
- Category: Trend
- Direction: Both
- Indicators: VIDYA, Bollinger Bands, ATR
- Stops: No
- Complexity: Advanced
- Timeframe: Intraday
- Seasonality: No
- Neural networks: No
- Divergence: No
- Risk level: Medium
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>
/// VIDYA-inspired ProTrend strategy with multi-tier take profit.
/// Uses fast/slow KAMA as VIDYA proxy with slope confirmation and percent-based TP tiers.
/// </summary>
public class VidyaProTrendMultiTierProfitStrategy : Strategy
{
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<decimal> _tp1Pct;
private readonly StrategyParam<decimal> _tp2Pct;
private readonly StrategyParam<decimal> _slPct;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFast;
private decimal _prevSlow;
private decimal _entryPrice;
private int _cooldown;
public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
public decimal Tp1Pct { get => _tp1Pct.Value; set => _tp1Pct.Value = value; }
public decimal Tp2Pct { get => _tp2Pct.Value; set => _tp2Pct.Value = value; }
public decimal SlPct { get => _slPct.Value; set => _slPct.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public VidyaProTrendMultiTierProfitStrategy()
{
_fastLength = Param(nameof(FastLength), 10)
.SetGreaterThanZero()
.SetDisplay("Fast Length", "Fast adaptive MA period", "General");
_slowLength = Param(nameof(SlowLength), 30)
.SetGreaterThanZero()
.SetDisplay("Slow Length", "Slow adaptive MA period", "General");
_tp1Pct = Param(nameof(Tp1Pct), 1.0m)
.SetGreaterThanZero()
.SetDisplay("TP1 %", "First take profit percent", "Risk");
_tp2Pct = Param(nameof(Tp2Pct), 5.0m)
.SetGreaterThanZero()
.SetDisplay("TP2 %", "Second take profit percent", "Risk");
_slPct = Param(nameof(SlPct), 3m)
.SetGreaterThanZero()
.SetDisplay("SL %", "Stop loss percent", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
protected override void OnReseted()
{
base.OnReseted();
_prevFast = 0;
_prevSlow = 0;
_entryPrice = 0;
_cooldown = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var kamaFast = new ExponentialMovingAverage { Length = FastLength };
var kamaSlow = new ExponentialMovingAverage { Length = SlowLength };
_prevFast = 0;
_prevSlow = 0;
_entryPrice = 0;
var subscription = SubscribeCandles(CandleType);
subscription.Bind(kamaFast, kamaSlow, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, kamaFast);
DrawIndicator(area, kamaSlow);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
{
if (candle.State != CandleStates.Finished)
return;
if (_cooldown > 0)
_cooldown--;
if (_prevFast == 0)
{
_prevFast = fast;
_prevSlow = slow;
return;
}
// TP/SL management
if (Position > 0 && _entryPrice > 0)
{
if (candle.ClosePrice >= _entryPrice * (1m + Tp2Pct / 100m) ||
candle.ClosePrice <= _entryPrice * (1m - SlPct / 100m))
{
SellMarket();
_entryPrice = 0;
_cooldown = 60;
_prevFast = fast;
_prevSlow = slow;
return;
}
}
else if (Position < 0 && _entryPrice > 0)
{
if (candle.ClosePrice <= _entryPrice * (1m - Tp2Pct / 100m) ||
candle.ClosePrice >= _entryPrice * (1m + SlPct / 100m))
{
BuyMarket();
_entryPrice = 0;
_cooldown = 60;
_prevFast = fast;
_prevSlow = slow;
return;
}
}
if (_cooldown > 0)
{
_prevFast = fast;
_prevSlow = slow;
return;
}
// Crossover entry
var longCross = _prevFast <= _prevSlow && fast > slow;
var shortCross = _prevFast >= _prevSlow && fast < slow;
if (longCross && Position <= 0)
{
BuyMarket();
_entryPrice = candle.ClosePrice;
_cooldown = 60;
}
else if (shortCross && Position >= 0)
{
SellMarket();
_entryPrice = candle.ClosePrice;
_cooldown = 60;
}
_prevFast = fast;
_prevSlow = slow;
}
}
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class vidya_pro_trend_multi_tier_profit_strategy(Strategy):
def __init__(self):
super(vidya_pro_trend_multi_tier_profit_strategy, self).__init__()
self._fast_length = self.Param("FastLength", 10) \
.SetDisplay("Fast Length", "Fast adaptive MA period", "General")
self._slow_length = self.Param("SlowLength", 30) \
.SetDisplay("Slow Length", "Slow adaptive MA period", "General")
self._tp1_pct = self.Param("Tp1Pct", 1.0) \
.SetDisplay("TP1 %", "First take profit percent", "Risk")
self._tp2_pct = self.Param("Tp2Pct", 5.0) \
.SetDisplay("TP2 %", "Second take profit percent", "Risk")
self._sl_pct = self.Param("SlPct", 3.0) \
.SetDisplay("SL %", "Stop loss percent", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
@property
def fast_length(self):
return self._fast_length.Value
@property
def slow_length(self):
return self._slow_length.Value
@property
def tp1_pct(self):
return self._tp1_pct.Value
@property
def tp2_pct(self):
return self._tp2_pct.Value
@property
def sl_pct(self):
return self._sl_pct.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(vidya_pro_trend_multi_tier_profit_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(vidya_pro_trend_multi_tier_profit_strategy, self).OnStarted2(time)
kama_fast = ExponentialMovingAverage()
kama_fast.Length = self.fast_length
kama_slow = ExponentialMovingAverage()
kama_slow.Length = self.slow_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(kama_fast, kama_slow, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, kama_fast)
self.DrawIndicator(area, kama_slow)
self.DrawOwnTrades(area)
def on_process(self, candle, fast, slow):
if candle.State != CandleStates.Finished:
return
fast = float(fast)
slow = float(slow)
close = float(candle.ClosePrice)
if self._cooldown > 0:
self._cooldown -= 1
if self._prev_fast == 0:
self._prev_fast = fast
self._prev_slow = slow
return
# TP/SL management
if self.Position > 0 and self._entry_price > 0:
if close >= self._entry_price * (1 + float(self.tp2_pct) / 100) or \
close <= self._entry_price * (1 - float(self.sl_pct) / 100):
self.SellMarket()
self._entry_price = 0
self._cooldown = 60
self._prev_fast = fast
self._prev_slow = slow
return
elif self.Position < 0 and self._entry_price > 0:
if close <= self._entry_price * (1 - float(self.tp2_pct) / 100) or \
close >= self._entry_price * (1 + float(self.sl_pct) / 100):
self.BuyMarket()
self._entry_price = 0
self._cooldown = 60
self._prev_fast = fast
self._prev_slow = slow
return
if self._cooldown > 0:
self._prev_fast = fast
self._prev_slow = slow
return
# Crossover entry
long_cross = self._prev_fast <= self._prev_slow and fast > slow
short_cross = self._prev_fast >= self._prev_slow and fast < slow
if long_cross and self.Position <= 0:
self.BuyMarket()
self._entry_price = close
self._cooldown = 60
elif short_cross and self.Position >= 0:
self.SellMarket()
self._entry_price = close
self._cooldown = 60
self._prev_fast = fast
self._prev_slow = slow
def CreateClone(self):
return vidya_pro_trend_multi_tier_profit_strategy()