VWAP RSI Scalper FINAL v1
Scalping strategy combining VWAP and RSI with ATR-based exits and daily trade limits.
Details
- Entry Criteria: Price relative to VWAP and EMA with RSI thresholds within session.
- Long/Short: Both directions.
- Exit Criteria: ATR-based stop and target.
- Stops: Yes.
- Default Values:
RsiLength= 3RsiOversold= 35mRsiOverbought= 70mEmaLength= 50SessionStart= 09:00SessionEnd= 16:00MaxTradesPerDay= 3AtrLength= 14StopAtrMult= 1mTargetAtrMult= 2mCandleType= TimeSpan.FromMinutes(1)
- Filters:
- Category: Scalping
- Direction: Both
- Indicators: VWAP, RSI, EMA, ATR
- Stops: Yes
- Complexity: Intermediate
- Timeframe: Intraday (1m)
- 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>
/// Scalping strategy using RSI and EMA with StdDev-based stops.
/// Buys on RSI oversold with bullish EMA trend, sells on RSI overbought with bearish EMA.
/// </summary>
public class VwapRsiScalperFinalV1Strategy : Strategy
{
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<decimal> _rsiOversold;
private readonly StrategyParam<decimal> _rsiOverbought;
private readonly StrategyParam<int> _emaLength;
private readonly StrategyParam<int> _maxTradesPerDay;
private readonly StrategyParam<decimal> _stopMult;
private readonly StrategyParam<decimal> _targetMult;
private readonly StrategyParam<DataType> _candleType;
private int _tradesToday;
private DateTime _currentDay;
private decimal _stopPrice;
private decimal _takeProfitPrice;
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public decimal RsiOversold { get => _rsiOversold.Value; set => _rsiOversold.Value = value; }
public decimal RsiOverbought { get => _rsiOverbought.Value; set => _rsiOverbought.Value = value; }
public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
public int MaxTradesPerDay { get => _maxTradesPerDay.Value; set => _maxTradesPerDay.Value = value; }
public decimal StopMult { get => _stopMult.Value; set => _stopMult.Value = value; }
public decimal TargetMult { get => _targetMult.Value; set => _targetMult.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public VwapRsiScalperFinalV1Strategy()
{
_rsiLength = Param(nameof(RsiLength), 7)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "RSI period", "Indicators");
_rsiOversold = Param(nameof(RsiOversold), 25m)
.SetDisplay("RSI Oversold", "Oversold level", "Indicators");
_rsiOverbought = Param(nameof(RsiOverbought), 75m)
.SetDisplay("RSI Overbought", "Overbought level", "Indicators");
_emaLength = Param(nameof(EmaLength), 50)
.SetGreaterThanZero()
.SetDisplay("EMA Length", "EMA period", "Indicators");
_maxTradesPerDay = Param(nameof(MaxTradesPerDay), 2)
.SetGreaterThanZero()
.SetDisplay("Max Trades", "Max trades per day", "Risk");
_stopMult = Param(nameof(StopMult), 1m)
.SetGreaterThanZero()
.SetDisplay("Stop Mult", "StdDev multiplier for stop", "Risk");
_targetMult = Param(nameof(TargetMult), 2m)
.SetGreaterThanZero()
.SetDisplay("Target Mult", "StdDev multiplier for target", "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();
_tradesToday = 0;
_currentDay = default;
_stopPrice = 0;
_takeProfitPrice = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var ema = new ExponentialMovingAverage { Length = EmaLength };
var stdDev = new StandardDeviation { Length = 14 };
_tradesToday = 0;
_currentDay = default;
_stopPrice = 0;
_takeProfitPrice = 0;
var subscription = SubscribeCandles(CandleType);
subscription.Bind(rsi, ema, stdDev, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsiVal, decimal emaVal, decimal stdVal)
{
if (candle.State != CandleStates.Finished)
return;
var day = candle.OpenTime.Date;
if (day != _currentDay)
{
_currentDay = day;
_tradesToday = 0;
}
// TP/SL exit
if (Position > 0)
{
if (candle.LowPrice <= _stopPrice || candle.HighPrice >= _takeProfitPrice)
{
SellMarket();
return;
}
}
else if (Position < 0)
{
if (candle.HighPrice >= _stopPrice || candle.LowPrice <= _takeProfitPrice)
{
BuyMarket();
return;
}
}
if (stdVal <= 0 || _tradesToday >= MaxTradesPerDay)
return;
// Entry signals
if (Position == 0)
{
var canLong = rsiVal < RsiOversold && candle.ClosePrice > emaVal;
var canShort = rsiVal > RsiOverbought && candle.ClosePrice < emaVal;
if (canLong)
{
BuyMarket();
_tradesToday++;
_stopPrice = candle.ClosePrice - stdVal * StopMult;
_takeProfitPrice = candle.ClosePrice + stdVal * TargetMult;
}
else if (canShort)
{
SellMarket();
_tradesToday++;
_stopPrice = candle.ClosePrice + stdVal * StopMult;
_takeProfitPrice = candle.ClosePrice - stdVal * TargetMult;
}
}
}
}
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, RelativeStrengthIndex, StandardDeviation
from StockSharp.Algo.Strategies import Strategy
class vwap_rsi_scalper_final_v1_strategy(Strategy):
def __init__(self):
super(vwap_rsi_scalper_final_v1_strategy, self).__init__()
self._rsi_length = self.Param("RsiLength", 7) \
.SetDisplay("RSI Length", "RSI period", "Indicators")
self._rsi_oversold = self.Param("RsiOversold", 25) \
.SetDisplay("RSI Oversold", "Oversold level", "Indicators")
self._rsi_overbought = self.Param("RsiOverbought", 75) \
.SetDisplay("RSI Overbought", "Overbought level", "Indicators")
self._ema_length = self.Param("EmaLength", 50) \
.SetDisplay("EMA Length", "EMA period", "Indicators")
self._max_trades_per_day = self.Param("MaxTradesPerDay", 2) \
.SetDisplay("Max Trades", "Max trades per day", "Risk")
self._stop_mult = self.Param("StopMult", 1) \
.SetDisplay("Stop Mult", "StdDev multiplier for stop", "Risk")
self._target_mult = self.Param("TargetMult", 2) \
.SetDisplay("Target Mult", "StdDev multiplier for target", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._trades_today = 0
self._current_day = None
self._stop_price = 0.0
self._take_profit_price = 0.0
@property
def rsi_length(self):
return self._rsi_length.Value
@property
def rsi_oversold(self):
return self._rsi_oversold.Value
@property
def rsi_overbought(self):
return self._rsi_overbought.Value
@property
def ema_length(self):
return self._ema_length.Value
@property
def max_trades_per_day(self):
return self._max_trades_per_day.Value
@property
def stop_mult(self):
return self._stop_mult.Value
@property
def target_mult(self):
return self._target_mult.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(vwap_rsi_scalper_final_v1_strategy, self).OnReseted()
self._trades_today = 0
self._current_day = None
self._stop_price = 0.0
self._take_profit_price = 0.0
def OnStarted2(self, time):
super(vwap_rsi_scalper_final_v1_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_length
ema = ExponentialMovingAverage()
ema.Length = self.ema_length
std_dev = StandardDeviation()
std_dev.Length = 14
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(rsi, ema, std_dev, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def on_process(self, candle, rsi_val, ema_val, std_val):
if candle.State != CandleStates.Finished:
return
day = candle.OpenTime.Date
if day != self._current_day:
self._current_day = day
self._trades_today = 0
# TP/SL exit
if self.Position > 0:
if candle.LowPrice <= self._stop_price or candle.HighPrice >= self._take_profit_price:
self.SellMarket()
return
elif self.Position < 0:
if candle.HighPrice >= self._stop_price or candle.LowPrice <= self._take_profit_price:
self.BuyMarket()
return
if std_val <= 0 or self._trades_today >= self.max_trades_per_day:
return
# Entry signals
if self.Position == 0:
can_long = rsi_val < self.rsi_oversold and candle.ClosePrice > ema_val
can_short = rsi_val > self.rsi_overbought and candle.ClosePrice < ema_val
if can_long:
self.BuyMarket()
self._trades_today += 1
self._stop_price = candle.ClosePrice - std_val * self.stop_mult
self._take_profit_price = candle.ClosePrice + std_val * self.target_mult
elif can_short:
self.SellMarket()
self._trades_today += 1
self._stop_price = candle.ClosePrice + std_val * self.stop_mult
self._take_profit_price = candle.ClosePrice - std_val * self.target_mult
def CreateClone(self):
return vwap_rsi_scalper_final_v1_strategy()