Pine Script v4 · Strategy
Delta-RSI Strategy (with filters)
A Pine Script v4 strategy built on RSI (close, 21), EMA (drsi, 9) and Crossover (drsi, 0.0). It has 7 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 30 settings; defaults include Polynomial Order 3, RSI Length 21 and Length ( > Order) 50.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Delta-RSI Strategy (with filters) repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 21 of 21 trades.
- How did Delta-RSI Strategy (with filters) do on BTCUSDT 1D with default settings?
- −28.82%
- From 26 Jan 2024 to 21 Oct 2026, over 71 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Delta-RSI Strategy (with filters) be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- What alerts does Delta-RSI Strategy (with filters) have?
- 4
- "D-RSI Long " + syminfo.tickerid; "D-RSI Short " + syminfo.tickerid; "D-RSI Exit Long " + syminfo.tickerid; "D-RSI Exit Short " + syminfo.tickerid.
- Does Delta-RSI Strategy (with filters) use a stop loss?
- Yes
- strategy.exit sets a stop on line 349, 351.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −28.82%
- −$28,819.20
- Buy and hold
- +102.5%
- same period
- Closed trades
- 71
- 31 won · 40 lost
- Profitable
- 43.7%
- of closed trades
- Profit factor
- 0.85
- gross profit ÷ gross loss
- Max drawdown
- 83.81%
- −$98,362.10
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A short position was still open at the last bar. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Likely bug
rsi(close, 14) ignores the settings line 306
Line 306 hard-codes 14 where line 290 uses “RSI Length”. Changing those settings moves one band but not this one.
rsi(close,14)rsi(close, rsi_l) -
Likely bug
rsi(close, 14) ignores the settings line 306
Line 306 hard-codes 14 where line 290 uses “RSI Length”. Changing those settings moves one band but not this one.
rsi(close,14)rsi(close, rsi_l) -
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
8 settings have no label
In the settings dialog usevol (line 245), useatr (line 252), usersi (line 259), rsitrhs1 (line 261), fixedstart (line 276), backtest_start (line 278), fixedend (line 282), backtest_end (line 284) show an empty name. The labels on this page come from the variable names.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses transp =, which v5 removed line 330
transp = on line 330, 331, 332, 333, 334. v5 folds the transparency into the colour with color.new(c, transp).
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames pow → math.pow, sqrt → math.sqrt, rsi → ta.rsi, ema → ta.ema, crossover → ta.crossover, crossunder → ta.crossunder, atr → ta.atr, sma → ta.sma, valuewhen → ta.valuewhen, sign → math.sign, max → math.max, min → math.min.
Same 71 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 29: //@version=4 → //@version=5
- Line 86: pow → math.pow
- Line 87: sqrt → math.sqrt
- Line 178: pow → math.pow
- Line 180: sqrt → math.sqrt
- Line 199: pow → math.pow
53 more edits
- Line 210: pow → math.pow
- Line 216: input(type = input.integer) → input.int()
- Line 218: input(type = input.integer) → input.int()
- Line 221: input(type = input.integer) → input.int()
- Line 223: input(type = input.integer) → input.int()
- Line 226: input(type = input.bool) → input.bool()
- Line 228: input(type = input.bool) → input.bool()
- Line 230: input(type = input.string) → input.string()
- Line 233: input(type = input.string) → input.string()
- Line 236: input(type = input.string) → input.string()
- Line 239: input(type = input.bool) → input.bool()
- Line 241: input(type = input.bool) → input.bool()
- Line 243: input(type = input.bool) → input.bool()
- Line 245: input(type = input.bool) → input.bool()
- Line 247: input(type = input.integer) → input.int()
- Line 249: input(type = input.integer) → input.int()
- Line 252: input(type = input.bool) → input.bool()
- Line 254: input(type = input.integer) → input.int()
- Line 256: input(type = input.integer) → input.int()
- Line 259: input(type = input.bool) → input.bool()
- Line 261: input(type = input.integer) → input.int()
- Line 263: input(type = input.integer) → input.int()
- Line 266: input(type = input.bool) → input.bool()
- Line 268: input(type = input.float) → input.float()
- Line 270: input(type = input.bool) → input.bool()
- Line 272: input(type = input.bool) → input.bool()
- Line 274: input(type = input.float) → input.float()
- Line 276: input(type = input.bool) → input.bool()
- Line 278: input(type = input.time) → input.time()
- Line 282: input(type = input.bool) → input.bool()
- Line 284: input(type = input.time) → input.time()
- Line 290: rsi → ta.rsi
- Line 292: ema → ta.ema
- Line 296: crossover → ta.crossover
- Line 297: crossunder → ta.crossunder
- Line 298: crossover → ta.crossover
- Line 299: crossunder → ta.crossunder
- Line 306: rsi → ta.rsi
- Line 306: rsi → ta.rsi
- Line 307: atr → ta.atr
- Line 307: atr → ta.atr
- Line 308: sma → ta.sma
- Line 320: valuewhen → ta.valuewhen
- Line 321: sign → math.sign
- Line 327: max → math.max
- Line 329: min → math.min
- Line 330: transp = 100 → color.new(color.blue, 100)
- Line 331: transp = 100 → color.new(color.red, 100)
- Line 332: transp = 100 → color.new(color.white, 100)
- Line 333: transp = istp?70:100 → color.new(color.green, istp?70:100)
- Line 334: transp = issl?70:100 → color.new(color.red, issl?70:100)
- Line 337: strategy.entry(…, true) → strategy.entry(…, strategy.long)
- Line 340: strategy.entry(…, false) → strategy.entry(…, strategy.short)
⋯// -//@version=4+//@version=5strategy(title="Delta-RSI Strategy with Filters", shorttitle = "D-RSI with filters", overlay = true)⋯ for i = 0 to _n-1- _norm := _norm + pow(array.get(_X,i),2)+ _norm := _norm + math.pow(array.get(_X,i),2)- sqrt(_norm)+ math.sqrt(_norm)qr_diag(_A,_nrows,_ncolumns) => ⋯ for i = 0 to _N-1- _mse := _mse + pow(array.get(_x,i) - array.get(_xhat,i),2)/_N+ _mse := _mse + math.pow(array.get(_x,i) - array.get(_xhat,i),2)/_N _xmean = array.sum(_x)/_N- _nrmse := sqrt(_mse) /_xmean+ _nrmse := math.sqrt(_mse) /_xmean _nrmse⋯ for j = 0 to _degree- matrix_set(_J,pow(i,j),i,j,_window)+ matrix_set(_J,math.pow(i,j),i,j,_window) // Vector of raw datapoints:⋯ for i = 1 to _degree- _diff := _diff + i*array.get(_a_coef,i)*pow(_window-1,i-1)+ _diff := _diff + i*array.get(_a_coef,i)*math.pow(_window-1,i-1) // Calculates data estimate (needed for rmse)⋯/// --- main ----degree = input(title="Polynomial Order", group = "Model Parameters:",+degree = input.int(title="Polynomial Order", group = "Model Parameters:",- inline = "linepar1", type = input.integer, defval=3, minval = 1)+ inline = "linepar1", defval=3, minval = 1)-rsi_l = input(title = "RSI Length", group = "Model Parameters:", +rsi_l = input.int(title = "RSI Length", group = "Model Parameters:", - inline = "linepar1", type = input.integer, defval = 21, minval = 1,+ inline = "linepar1", defval = 21, minval = 1, tooltip="The period length of RSI that is used as input.")-window = input(title="Length ( > Order)", group = "Model Parameters:",+window = input.int(title="Length ( > Order)", group = "Model Parameters:",- inline = "linepar2", type = input.integer, defval=50, minval = 2)+ inline = "linepar2", defval=50, minval = 2)-signalLength = input(title="Signal Length", group = "Model Parameters:",+signalLength = input.int(title="Signal Length", group = "Model Parameters:",- inline = "linepar2", type=input.integer, defval=9,+ inline = "linepar2", defval=9, tooltip="The signal line is a EMA of the D-RSI time series.")-islong = input(title = "Long", group = "Allowed Entries:",+islong = input.bool(title = "Long", group = "Allowed Entries:",- inline = "lineent",type = input.bool, defval = true)+ inline = "lineent", defval = true)-isshort = input(title = "Short", group = "Allowed Entries:",+isshort = input.bool(title = "Short", group = "Allowed Entries:",- inline = "lineent", type = input.bool, defval= true)+ inline = "lineent", defval= true)-buycond = input(title="Buy", group = "Entry and Exit Conditions:", +buycond = input.string(title="Buy", group = "Entry and Exit Conditions:", - inline = "linecond",type = input.string, defval="Signal Line Crossing", + inline = "linecond", defval="Signal Line Crossing", options=["Zero-Crossing", "Signal Line Crossing","Direction Change"])-sellcond = input(title="Sell", group = "Entry and Exit Conditions:", +sellcond = input.string(title="Sell", group = "Entry and Exit Conditions:", - inline = "linecond",type = input.string, defval="Signal Line Crossing", + inline = "linecond", defval="Signal Line Crossing", options=["Zero-Crossing", "Signal Line Crossing","Direction Change"])-endcond = input(title="Exit", group = "Entry and Exit Conditions:", +endcond = input.string(title="Exit", group = "Entry and Exit Conditions:", - inline = "linecond",type = input.string, defval="Signal Line Crossing", + inline = "linecond", defval="Signal Line Crossing", options=["Zero-Crossing", "Signal Line Crossing","Direction Change"])-filterlong =input(title = "Long Entries", inline = 'linefilt', group = 'Apply Filters to', - type = input.bool, defval = true)+filterlong =input.bool(title = "Long Entries", inline = 'linefilt', group = 'Apply Filters to', defval = true)-filtershort =input(title = "Short Enties", inline = 'linefilt', group = 'Apply Filters to', - type = input.bool, defval = true)+filtershort =input.bool(title = "Short Enties", inline = 'linefilt', group = 'Apply Filters to', defval = true)-filterend =input(title = "Exits", inline = 'linefilt', group = 'Apply Filters to', - type = input.bool, defval = true)+filterend =input.bool(title = "Exits", inline = 'linefilt', group = 'Apply Filters to', defval = true)-usevol =input(title = "", inline = 'linefiltvol', group = 'Relative Volume Filter:', - type = input.bool, defval = false)+usevol =input.bool(title = "", inline = 'linefiltvol', group = 'Relative Volume Filter:', defval = false)-rvol = input(title = "Volume >", inline = 'linefiltvol', group = 'Relative Volume Filter:', - type = input.integer, defval = 1)+rvol = input.int(title = "Volume >", inline = 'linefiltvol', group = 'Relative Volume Filter:', defval = 1)-len_vol = input(title = "Avg. Volume Over Period", inline = 'linefiltvol', group = 'Relative Volume Filter:', - type = input.integer, defval = 30, minval = 1,+len_vol = input.int(title = "Avg. Volume Over Period", inline = 'linefiltvol', group = 'Relative Volume Filter:', defval = 30, minval = 1, tooltip="The current volume must be greater than N times the M-period average volume.")-useatr =input(title = "", inline = 'linefiltatr', group = 'Volatility Filter:', - type = input.bool, defval = false)+useatr =input.bool(title = "", inline = 'linefiltatr', group = 'Volatility Filter:', defval = false)-len_atr1 = input(title = "ATR", inline = 'linefiltatr', group = 'Volatility Filter:', - type = input.integer, defval = 5, minval = 1)+len_atr1 = input.int(title = "ATR", inline = 'linefiltatr', group = 'Volatility Filter:', defval = 5, minval = 1)-len_atr2 = input(title = "> ATR", inline = 'linefiltatr', group = 'Volatility Filter:', - type = input.integer, defval = 30, minval = 1,+len_atr2 = input.int(title = "> ATR", inline = 'linefiltatr', group = 'Volatility Filter:', defval = 30, minval = 1, tooltip="The N-period ATR must be greater than the M-period ATR.")-usersi =input(title = "", inline = 'linersi', group = 'Overbought/Oversold Filter:', - type = input.bool, defval = false)+usersi =input.bool(title = "", inline = 'linersi', group = 'Overbought/Oversold Filter:', defval = false)-rsitrhs1 = input(title = "", inline = 'linersi', group = 'Overbought/Oversold Filter:', - type = input.integer, defval = 0, minval=0, maxval=100)+rsitrhs1 = input.int(title = "", inline = 'linersi', group = 'Overbought/Oversold Filter:', defval = 0, minval=0, maxval=100)-rsitrhs2 = input(title = "< RSI (14) <", inline = 'linersi', group = 'Overbought/Oversold Filter:', - type = input.integer, defval = 100, minval=0, maxval=100,+rsitrhs2 = input.int(title = "< RSI (14) <", inline = 'linersi', group = 'Overbought/Oversold Filter:', defval = 100, minval=0, maxval=100, tooltip="RSI(14) must be in the range between N and M.")-issl = input(title = "SL", inline = 'linesl1', group = 'Stop Loss / Take Profit:', - type = input.bool, defval = false)+issl = input.bool(title = "SL", inline = 'linesl1', group = 'Stop Loss / Take Profit:', defval = false)-slpercent = input(title = ", %", inline = 'linesl1', group = 'Stop Loss / Take Profit:', - type = input.float, defval = 10, minval=0.0)+slpercent = input.float(title = ", %", inline = 'linesl1', group = 'Stop Loss / Take Profit:', defval = 10, minval=0.0)-istrailing = input(title = "Trailing", inline = 'linesl1', group = 'Stop Loss / Take Profit:', - type = input.bool, defval = false)+istrailing = input.bool(title = "Trailing", inline = 'linesl1', group = 'Stop Loss / Take Profit:', defval = false)-istp = input(title = "TP", inline = 'linetp1', group = 'Stop Loss / Take Profit:', - type = input.bool, defval = false)+istp = input.bool(title = "TP", inline = 'linetp1', group = 'Stop Loss / Take Profit:', defval = false)-tppercent = input(title = ", %", inline = 'linetp1', group = 'Stop Loss / Take Profit:', - type = input.float, defval = 20)+tppercent = input.float(title = ", %", inline = 'linetp1', group = 'Stop Loss / Take Profit:', defval = 20)-fixedstart =input(title="", group = "Fixed Backtest Period Start/End Dates:",+fixedstart =input.bool(title="", group = "Fixed Backtest Period Start/End Dates:",- inline = "linebac1", type = input.bool, defval = true)+ inline = "linebac1", defval = true)-backtest_start=input(title = "", type = input.time, inline = "linebac1", +backtest_start=input.time(title = "", inline = "linebac1", group = "Fixed Backtest Period Start/End Dates:",⋯ tooltip="If deactivated, backtest staring from the first available price bar.")-fixedend = input(title="", group = "Fixed Backtest Period Start/End Dates:",+fixedend = input.bool(title="", group = "Fixed Backtest Period Start/End Dates:",- inline = "linebac2", type = input.bool, defval = false)+ inline = "linebac2", defval = false)-backtest_end =input(title = "", type = input.time, inline = "linebac2", +backtest_end =input.time(title = "", inline = "linebac2", group = "Fixed Backtest Period Start/End Dates:",⋯ window := degree+1-src = rsi(close,rsi_l)+src = ta.rsi(close,rsi_l)[drsi,nrmse] = diff(src,window,degree)-signalline = ema(drsi, signalLength)+signalline = ta.ema(drsi, signalLength)// Conditions for D-RSI⋯dirchangedw = (drsi<drsi[1]) and (drsi[1]>drsi[2]) and drsi[1]>0.0-crossup = crossover(drsi,0.0)+crossup = ta.crossover(drsi,0.0)-crossdw = crossunder(drsi,0.0)+crossdw = ta.crossunder(drsi,0.0)-crosssignalup = crossover(drsi,signalline)+crosssignalup = ta.crossover(drsi,signalline)-crosssignaldw = crossunder(drsi,signalline)+crosssignaldw = ta.crossunder(drsi,signalline)// D-RSI signals⋯// Filters-rsifilter = usersi?(rsi(close,14) > rsitrhs1 and rsi(close,14) < rsitrhs2):true+rsifilter = usersi?(ta.rsi(close,14) > rsitrhs1 and ta.rsi(close,14) < rsitrhs2):true-volatilityfilter = useatr?(atr(len_atr1) > atr(len_atr2)):true+volatilityfilter = useatr?(ta.atr(len_atr1) > ta.atr(len_atr2)):true-volumefilter = usevol?(volume > rvol*sma(volume,len_vol)):true+volumefilter = usevol?(volume > rvol*ta.sma(volume,len_vol)):truetotalfilter = volatilityfilter and volumefilter and rsifilter⋯// Entry price / Take profit / Stop Loss-startprice = valuewhen(condition=golong or goshort, source=close, occurrence=0)+startprice = ta.valuewhen(condition=golong or goshort, source=close, occurrence=0)-pm = golong?1:goshort?-1:1/sign(strategy.position_size)+pm = golong?1:goshort?-1:1/math.sign(strategy.position_size)takeprofit = startprice*(1+pm*tppercent*0.01)⋯if istrailing and strategy.position_size>0- stoploss := max(close*(1 - slpercent*0.01),stoploss[1])+ stoploss := math.max(close*(1 - slpercent*0.01),stoploss[1])else if istrailing and strategy.position_size<0- stoploss := min(close*(1 + slpercent*0.01),stoploss[1])+ stoploss := math.min(close*(1 + slpercent*0.01),stoploss[1])-tpline = plot(takeprofit,color=color.blue,transp=100, title="TP")+tpline = plot(takeprofit,color=color.new(color.blue, 100), title="TP")-slline = plot(stoploss, color=color.red, transp=100, title="SL")+slline = plot(stoploss, color=color.new(color.red, 100), title="SL")-p1 = plot(close,transp=100,color=color.white, title="Dummy Close")+p1 = plot(close,color=color.new(color.white, 100), title="Dummy Close")-fill(p1, tpline, color=color.green, transp=istp?70:100, title="TP")+fill(p1, tpline, color=color.new(color.green, istp?70:100), title="TP")-fill(p1, slline, color=color.red, transp=issl?70:100, title="SL")+fill(p1, slline, color=color.new(color.red, issl?70:100), title="SL")// Backtest: Basic Entry and Exit Conditionsif golong and isinrange and islong- strategy.entry("long", true )+ strategy.entry("long", strategy.long ) alert("D-RSI Long " + syminfo.tickerid, alert.freq_once_per_bar_close) if goshort and isinrange and isshort- strategy.entry("short", false)+ strategy.entry("short", strategy.short) alert("D-RSI Short " + syminfo.tickerid, alert.freq_once_per_bar_close) ⋯Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 71 before, 58 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// -//@version=5+//@version=6strategy(title="Delta-RSI Strategy with Filters", shorttitle = "D-RSI with filters", overlay = true)⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Model Parameters: | |||
| Polynomial Order | 3 | int (written input.integer) · min 1 | window drsi nrmse line 288 |
| RSI LengthThe period length of RSI that is used as input. | 21 | int (written input.integer) · min 1 | src |
| Length ( > Order) | 50 | int (written input.integer) · min 2 | drsi nrmse line 288 |
| Signal LengthThe signal line is a EMA of the D-RSI time series. | 9 | int (written input.integer) | signalline |
| Allowed Entries: | |||
| Long | true | bool | golong entry long line 336 |
| Short | true | bool | goshort entry short line 339 |
| Entry and Exit Conditions: | |||
| Buy | Signal Line Crossing | string · options ["Zero-Crossing", "Signal Line Crossing","Direction Change"] | drsilong |
| Sell | Signal Line Crossing | string · options ["Zero-Crossing", "Signal Line Crossing","Direction Change"] | drsishort |
| Exit | Signal Line Crossing | string · options ["Zero-Crossing", "Signal Line Crossing","Direction Change"] | drisendlong drisendshort |
| Apply Filters to | |||
| Long Entries | true | bool | golong |
| Short Enties | true | bool | goshort |
| Exits | true | bool | endlong endshort |
| Relative Volume Filter: | |||
| Usevol (no label in the script) | false | bool | volumefilter |
| Volume > | 1 | int (written input.integer) | volumefilter |
| Avg. Volume Over PeriodThe current volume must be greater than N times the M-period average volume. | 30 | int (written input.integer) · min 1 | volumefilter |
| Volatility Filter: | |||
| Useatr (no label in the script) | false | bool | volatilityfilter |
| ATR | 5 | int (written input.integer) · min 1 | volatilityfilter |
| > ATRThe N-period ATR must be greater than the M-period ATR. | 30 | int (written input.integer) · min 1 | volatilityfilter |
| Overbought/Oversold Filter: | |||
| Usersi (no label in the script) | false | bool | rsifilter |
| Rsitrhs1 (no label in the script) | 0 | int (written input.integer) · min 0 · max 100 | rsifilter |
| < RSI (14) <RSI(14) must be in the range between N and M. | 100 | int (written input.integer) · min 0 · max 100 | rsifilter |
| Stop Loss / Take Profit: | |||
| SL | false | bool | SL exit sl/tp long exit sl/tp short |
| , % | 10 | float · min 0.0 | stoploss |
| Trailing | false | bool | line 326 line 328 |
| TP | false | bool | TP exit sl/tp long exit sl/tp short |
| , % | 20 | float | takeprofit |
| Fixed Backtest Period Start/End Dates: | |||
| Fixedstart (no label in the script) | true | bool | isinrange |
| Backtest start (no label in the script)If deactivated, backtest staring from the first available price bar. | timestamp("01 Jan 2017 13:30 +0000") | time | isinrange |
| Fixedend (no label in the script) | false | bool | isinrange |
| Backtest end (no label in the script)If deactivated, backtesting ends at the last available price bar. | timestamp("30 Dec 2080 23:30 +0000") | time | isinrange |
What it calculates
- RSI (
rsi,ta.rsiin v5)close, RSI Length (21)src · line 290 - EMA (
ema,ta.emain v5)drsi, Signal Length (9)signalline · line 292 - Crossover (
crossover,ta.crossoverin v5)drsi, 0.0crossup · line 296 - Crossunder (
crossunder,ta.crossunderin v5)drsi, 0.0crossdw · line 297 - Crossover (
crossover,ta.crossoverin v5)drsi, signallinecrosssignalup · line 298 - Crossunder (
crossunder,ta.crossunderin v5)drsi, signallinecrosssignaldw · line 299 - RSI (
rsi,ta.rsiin v5)close, 14rsifilter · line 306 - ATR (
atr,ta.atrin v5)ATR (5)volatilityfilter · line 307 - SMA (
sma,ta.smain v5)volume, Avg. Volume Over Period (30)volumefilter · line 308
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
The script has 75 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long long | golong and isinrange and islong | On | 337 |
| Open short short | goshort and isinrange and isshort | On | 340 |
| Close long | endlong | On | 343 |
| Close short | endshort | On | 346 |
| Exit sl/tp long stop and limit | every bar | On | 349 |
| Exit sl/tp short stop and limit | every bar | On | 351 |
| Close | (not isinrange) | On | 355 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| (no title) | alert | "D-RSI Long " + syminfo.tickerid | 338 |
| (no title) | alert | "D-RSI Short " + syminfo.tickerid | 341 |
| (no title) | alert | "D-RSI Exit Long " + syminfo.tickerid | 344 |
| (no title) | alert | "D-RSI Exit Short " + syminfo.tickerid | 347 |
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −28.8% | +102.5% | 71 | 44% | 0.85 | 83.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −2.3% | +5.5% | 73 | 42% | 0.96 | 14.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.7% | +9.8% | 77 | 38% | 0.84 | 4.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.4% | +8.4% | 59 | 47% | 1.27 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.2% | +16.7% | 77 | 47% | 0.75 | 0.3% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 71 | 39% | 1.09 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.4% | +148.3% | 75 | 47% | 0.79 | 0.9% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 67 | 40% | 1.22 | 0.2% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 21 | 48% | $9,745.74 |
| Sideways market | 37 | 32% | −$92,487.20 |
| Falling market | 13 | 69% | $53,922.20 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
81.8%
The 95th percentile of the worst drawdown when the same 71 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 55.0% to 81.8%; the tick is what actually happened, 81.9%. Order luck alone moves it this much.
All 71 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 24 Apr 2024 66,414.00 | 25 Apr 2024 64,289.60 | 1 | −$2,124.42 |
| short | 25 Apr 2024 64,289.60 | 4 May 2024 62,882.00 | 9 | $1,407.57 |
| long | 4 May 2024 62,882.00 | 15 May 2024 61,577.50 | 11 | −$1,304.52 |
| short | 15 May 2024 61,577.50 | 16 May 2024 66,206.50 | 1 | −$4,629.02 |
| long | 16 May 2024 66,206.50 | 25 May 2024 68,550.00 | 9 | $2,343.48 |
| short | 25 May 2024 68,550.00 | 26 Jun 2024 61,806.00 | 32 | $6,743.98 |
| long | 26 Jun 2024 61,806.00 | 25 Jul 2024 65,376.00 | 29 | $3,570.00 |
| short | 25 Jul 2024 65,376.00 | 12 Aug 2024 58,712.60 | 18 | $6,663.42 |
| long | 12 Aug 2024 58,712.60 | 31 Aug 2024 59,124.00 | 19 | $411.40 |
| short | 31 Aug 2024 59,124.00 | 14 Sep 2024 60,498.00 | 14 | −$1,374.00 |
| long | 14 Sep 2024 60,498.00 | 1 Oct 2024 63,327.60 | 17 | $2,829.61 |
| short | 1 Oct 2024 63,327.60 | 15 Oct 2024 66,084.00 | 14 | −$2,756.40 |
| long | 15 Oct 2024 66,084.00 | 28 Oct 2024 68,021.70 | 13 | $1,937.69 |
| short | 28 Oct 2024 68,021.70 | 30 Oct 2024 72,736.40 | 2 | −$4,714.72 |
| long | 30 Oct 2024 72,736.40 | 1 Nov 2024 70,292.00 | 2 | −$2,444.40 |
| short | 1 Nov 2024 70,292.00 | 11 Nov 2024 80,370.00 | 10 | −$10,078.00 |
| long | 11 Nov 2024 80,370.00 | 25 Nov 2024 97,900.10 | 14 | $17,530.00 |
| short | 25 Nov 2024 97,900.10 | 9 Dec 2024 101,110.00 | 14 | −$3,209.55 |
| long | 9 Dec 2024 101,110.00 | 24 Dec 2024 94,881.50 | 15 | −$6,228.13 |
| short | 24 Dec 2024 94,881.50 | 3 Jan 2025 96,984.80 | 10 | −$2,103.32 |
| long | 3 Jan 2025 96,984.80 | 25 Jan 2025 104,871.00 | 22 | $7,885.72 |
| short | 25 Jan 2025 104,871.00 | 13 Feb 2025 97,870.00 | 19 | $7,000.51 |
| long | 13 Feb 2025 97,870.00 | 28 Feb 2025 84,708.60 | 15 | −$13,161.40 |
| short | 28 Feb 2025 84,708.60 | 3 Mar 2025 94,270.00 | 3 | −$9,561.42 |
| long | 3 Mar 2025 94,270.00 | 11 Mar 2025 78,595.90 | 8 | −$15,674.10 |
| short | 11 Mar 2025 78,595.90 | 15 Mar 2025 83,983.20 | 4 | −$5,387.33 |
| long | 15 Mar 2025 83,983.20 | 29 Mar 2025 84,424.40 | 14 | $441.19 |
| short | 29 Mar 2025 84,424.40 | 12 Apr 2025 83,423.80 | 14 | $1,000.55 |
| long | 12 Apr 2025 83,423.80 | 2 May 2025 96,489.90 | 20 | $13,066.10 |
| short | 2 May 2025 96,489.90 | 22 May 2025 109,644.00 | 20 | −$13,154.10 |
| long | 22 May 2025 109,644.00 | 28 May 2025 108,938.00 | 6 | −$705.82 |
| short | 28 May 2025 108,938.00 | 10 Jun 2025 110,263.00 | 13 | −$1,324.85 |
| long | 10 Jun 2025 110,263.00 | 23 Jun 2025 100,964.00 | 13 | −$9,299.15 |
| short | 23 Jun 2025 100,964.00 | 24 Jun 2025 105,334.00 | 1 | −$4,370.07 |
| long | 24 Jun 2025 105,334.00 | 6 Jul 2025 108,198.00 | 12 | $2,864.18 |
| short | 6 Jul 2025 108,198.00 | 11 Jul 2025 116,010.00 | 5 | −$7,811.89 |
| long | 11 Jul 2025 116,010.00 | 20 Jul 2025 117,840.00 | 9 | $1,830.00 |
| short | 20 Jul 2025 117,840.00 | 8 Aug 2025 117,472.00 | 19 | $367.99 |
| long | 8 Aug 2025 117,472.00 | 22 Aug 2025 112,500.00 | 14 | −$4,972.02 |
| short | 22 Aug 2025 112,500.00 | 23 Aug 2025 116,936.00 | 1 | −$4,436.00 |
| long | 23 Aug 2025 116,936.00 | 25 Aug 2025 113,494.00 | 2 | −$3,442.41 |
| short | 25 Aug 2025 113,494.00 | 4 Sep 2025 111,706.00 | 10 | $1,787.87 |
| long | 4 Sep 2025 111,706.00 | 22 Sep 2025 115,232.00 | 18 | $3,526.57 |
| short | 22 Sep 2025 115,232.00 | 3 Oct 2025 120,529.00 | 11 | −$5,297.06 |
| long | 3 Oct 2025 120,529.00 | 11 Oct 2025 112,774.00 | 8 | −$7,754.86 |
| short | 11 Oct 2025 112,774.00 | 25 Oct 2025 111,005.00 | 14 | $1,769.59 |
| long | 25 Oct 2025 111,005.00 | 15 Nov 2025 94,594.00 | 21 | −$16,410.90 |
| short | 15 Nov 2025 94,594.00 | 26 Nov 2025 87,370.00 | 11 | $7,224.03 |
| long | 26 Nov 2025 87,370.00 | 12 Dec 2025 92,513.40 | 16 | $5,143.41 |
| short | 12 Dec 2025 92,513.40 | 31 Dec 2025 88,485.50 | 19 | $4,027.88 |
| long | 31 Dec 2025 88,485.50 | 13 Jan 2026 91,296.20 | 13 | $2,810.70 |
| short | 13 Jan 2026 91,296.20 | 14 Jan 2026 95,414.00 | 1 | −$4,117.79 |
| long | 14 Jan 2026 95,414.00 | 18 Jan 2026 95,147.80 | 4 | −$266.22 |
| short | 18 Jan 2026 95,147.80 | 8 Feb 2026 69,289.40 | 21 | $25,858.40 |
| long | 8 Feb 2026 69,289.40 | 28 Feb 2026 65,872.10 | 20 | −$3,417.28 |
| short | 28 Feb 2026 65,872.10 | 6 Apr 2026 69,034.20 | 37 | −$3,162.09 |
| long | 6 Apr 2026 69,034.20 | 21 Apr 2026 75,841.00 | 15 | $6,806.79 |
| short | 21 Apr 2026 75,841.00 | 7 May 2026 81,447.00 | 16 | −$5,606.04 |
| long | 7 May 2026 81,447.00 | 17 May 2026 78,148.10 | 10 | −$3,298.96 |
| short | 17 May 2026 78,148.10 | 1 Jun 2026 73,674.40 | 15 | $4,473.66 |
| long | 1 Jun 2026 73,674.40 | 2 Jun 2026 71,408.90 | 1 | −$2,265.49 |
| short | 2 Jun 2026 71,408.90 | 9 Jun 2026 63,086.00 | 7 | $8,322.90 |
| long | 9 Jun 2026 63,086.00 | 25 Jun 2026 61,078.00 | 16 | −$2,008.00 |
| short | 25 Jun 2026 61,078.00 | 6 Jul 2026 63,650.00 | 11 | −$2,572.01 |
| long | 6 Jul 2026 63,650.00 | 9 Jul 2026 62,290.00 | 3 | −$1,360.00 |
| short | 9 Jul 2026 62,290.00 | 16 Jul 2026 64,756.30 | 7 | −$2,466.27 |
| long | 16 Jul 2026 64,756.30 | 26 Jul 2026 64,375.00 | 10 | −$381.27 |
| short | 26 Jul 2026 64,375.00 | 7 Aug 2026 64,323.60 | 12 | $51.40 |
| long | 7 Aug 2026 64,323.60 | 30 Aug 2026 78,230.00 | 23 | $13,906.40 |
| short | 30 Aug 2026 78,230.00 | 19 Sep 2026 80,883.90 | 20 | −$2,653.86 |
| long | 19 Sep 2026 80,883.90 | 6 Oct 2026 85,766.90 | 17 | $4,883.01 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Delta-RSI Strategy (with filters) this page | — | v4 strategy | −28.8% | 71 | 83.8% |
| 3 x EMA + Stochastic RSI + ATR by tomimarson | ema rsi sma atr | v4 strategy | −42.3% | 18 | 44.8% |
| Atrend by OTS by prim722 | ema rsi sma atr | v4 strategy | +12.1% | 76 | 34.4% |
| EMA crossover (daily TF) by tradeswithashish | ema sma rsi atr | v5 strategy | +11.0% | 12 | 11.6% |
| [KL] Bollinger bands + RSI Strategy by DojiEmoji | sma atr ema rsi | v4 strategy | +28.2% | 30 | 14.7% |
| 3x Supertrend and Stoch RSI by M3RZI | rsi sma ema atr | v4 strategy | −128.7% | 24 | 207.6% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 56 ms | With a workaround 1,000 bars in 8,412 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as true, false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 337, 340) so the engine trades the way the script says. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © tbiktag//// Delta-RSI Oscillator Strategy With Filters//// This is a version of the Delta-RSI Oscillator Strategy compatible with // the Strategy Tester.//// This version also allows filtering the trade signals generated by Delts-RSI// by means of volatility (defined by ATR), relative volume and RSI(14).//// Delta-RSI (© tbiktag) is a smoothed time derivative of the RSI designed// as a momentum indicator. For the original publication, see link below:// https://www.tradingview.com/script/OXQVFTQD-Delta-RSI-Oscillator/// // D-RSI model parameters:// RSI Length: The timeframe of the RSI that serves as an input to D-RSI.// Frame Length: The length of the lookback frame used for local regression.// Polynomial Order: The order of the local polynomial function used to interpolate // the RSI.// Trade signals are generated based on three optional conditions:// - Zero-crossing: bullish when D-RSI crosses zero from negative to positive // values (bearish otherwise)// - Signal Line Crossing: bullish when D-RSI crosses from below to above the signal // line (bearish otherwise)// - Direction Change: bullish when D-RSI was negative and starts ascending // (bearish otherwise)// //@version=4strategy(title="Delta-RSI Strategy with Filters", shorttitle = "D-RSI with filters", overlay = true)// ---Subroutines---matrix_get(_A,_i,_j,_nrows) => // Get the value of the element of an implied 2d matrix //input: // _A :: array: pseudo 2d matrix _A = [[column_0],[column_1],...,[column_(n-1)]] // _i :: integer: row number // _j :: integer: column number // _nrows :: integer: number of rows in the implied 2d matrix array.get(_A,_i+_nrows*_j)matrix_set(_A,_value,_i,_j,_nrows) => // Set a value to the element of an implied 2d matrix //input: // _A :: array, changed on output: pseudo 2d matrix _A = [[column_0],[column_1],...,[column_(n-1)]] // _value :: float: the new value to be set // _i :: integer: row number // _j :: integer: column number // _nrows :: integer: number of rows in the implied 2d matrix array.set(_A,_i+_nrows*_j,_value)transpose(_A,_nrows,_ncolumns) => // Transpose an implied 2d matrix // input: // _A :: array: pseudo 2d matrix _A = [[column_0],[column_1],...,[column_(n-1)]] // _nrows :: integer: number of rows in _A // _ncolumns :: integer: number of columns in _A // output: // _AT :: array: pseudo 2d matrix with implied dimensions: _ncolums x _nrows var _AT = array.new_float(_nrows*_ncolumns,0) for i = 0 to _nrows-1 for j = 0 to _ncolumns-1 matrix_set(_AT, matrix_get(_A,i,j,_nrows),j,i,_ncolumns) _ATmultiply(_A,_B,_nrowsA,_ncolumnsA,_ncolumnsB) => // Calculate scalar product of two matrices // input: // _A :: array: pseudo 2d matrix // _B :: array: pseudo 2d matrix // _nrowsA :: integer: number of rows in _A // _ncolumnsA :: integer: number of columns in _A // _ncolumnsB :: integer: number of columns in _B // output: // _C:: array: pseudo 2d matrix with implied dimensions _nrowsA x _ncolumnsB var _C = array.new_float(_nrowsA*_ncolumnsB,0) int _nrowsB = _ncolumnsA float elementC= 0.0 for i = 0 to _nrowsA-1 for j = 0 to _ncolumnsB-1 elementC := 0 for k = 0 to _ncolumnsA-1 elementC := elementC + matrix_get(_A,i,k,_nrowsA)*matrix_get(_B,k,j,_nrowsB) matrix_set(_C,elementC,i,j,_nrowsA) _Cvnorm(_X,_n) => //Square norm of vector _X with size _n float _norm = 0.0 for i = 0 to _n-1 _norm := _norm + pow(array.get(_X,i),2) sqrt(_norm)qr_diag(_A,_nrows,_ncolumns) => //QR Decomposition with Modified Gram-Schmidt Algorithm (Column-Oriented) // input: // _A :: array: pseudo 2d matrix _A = [[column_0],[column_1],...,[column_(n-1)]] // _nrows :: integer: number of rows in _A // _ncolumns :: integer: number of columns in _A // output: // _Q: unitary matrix, implied dimenstions _nrows x _ncolumns // _R: upper triangular matrix, implied dimansions _ncolumns x _ncolumns var _Q = array.new_float(_nrows*_ncolumns,0) var _R = array.new_float(_ncolumns*_ncolumns,0) var _a = array.new_float(_nrows,0) var _q = array.new_float(_nrows,0) float _r = 0.0 float _aux = 0.0 //get first column of _A and its norm: for i = 0 to _nrows-1 array.set(_a,i,matrix_get(_A,i,0,_nrows)) _r := vnorm(_a,_nrows) //assign first diagonal element of R and first column of Q matrix_set(_R,_r,0,0,_ncolumns) for i = 0 to _nrows-1 matrix_set(_Q,array.get(_a,i)/_r,i,0,_nrows) if _ncolumns != 1 //repeat for the rest of the columns for k = 1 to _ncolumns-1 for i = 0 to _nrows-1 array.set(_a,i,matrix_get(_A,i,k,_nrows)) for j = 0 to k-1 //get R_jk as scalar product of Q_j column and A_k column: _r := 0 for i = 0 to _nrows-1 _r := _r + matrix_get(_Q,i,j,_nrows)*array.get(_a,i) matrix_set(_R,_r,j,k,_ncolumns) //update vector _a for i = 0 to _nrows-1 _aux := array.get(_a,i) - _r*matrix_get(_Q,i,j,_nrows) array.set(_a,i,_aux) //get diagonal R_kk and Q_k column _r := vnorm(_a,_nrows) matrix_set(_R,_r,k,k,_ncolumns) for i = 0 to _nrows-1 matrix_set(_Q,array.get(_a,i)/_r,i,k,_nrows) [_Q,_R] pinv(_A,_nrows,_ncolumns) => //Pseudoinverse of matrix _A calculated using QR decomposition // Input: // _A:: array: implied as a (_nrows x _ncolumns) matrix //. _A = [[column_0],[column_1],...,[column_(_ncolumns-1)]] // Output: // _Ainv:: array implied as a (_ncolumns x _nrows) matrix // _A = [[row_0],[row_1],...,[row_(_nrows-1)]] // ---- // First find the QR factorization of A: A = QR, // where R is upper triangular matrix. // Then _Ainv = R^-1*Q^T. // ---- [_Q,_R] = qr_diag(_A,_nrows,_ncolumns) _QT = transpose(_Q,_nrows,_ncolumns) // Calculate Rinv: var _Rinv = array.new_float(_ncolumns*_ncolumns,0) float _r = 0.0 matrix_set(_Rinv,1/matrix_get(_R,0,0,_ncolumns),0,0,_ncolumns) if _ncolumns != 1 for j = 1 to _ncolumns-1 for i = 0 to j-1 _r := 0.0 for k = i to j-1 _r := _r + matrix_get(_Rinv,i,k,_ncolumns)*matrix_get(_R,k,j,_ncolumns) matrix_set(_Rinv,_r,i,j,_ncolumns) for k = 0 to j-1 matrix_set(_Rinv,-matrix_get(_Rinv,k,j,_ncolumns)/matrix_get(_R,j,j,_ncolumns),k,j,_ncolumns) matrix_set(_Rinv,1/matrix_get(_R,j,j,_ncolumns),j,j,_ncolumns) // _Ainv = multiply(_Rinv,_QT,_ncolumns,_ncolumns,_nrows) _Ainvnorm_rmse(_x, _xhat) => // Root Mean Square Error normalized to the sample mean // _x. :: array float, original data // _xhat :: array float, model estimate // output // _nrmse:: float float _nrmse = 0.0 if array.size(_x) != array.size(_xhat) _nrmse := na else int _N = array.size(_x) float _mse = 0.0 for i = 0 to _N-1 _mse := _mse + pow(array.get(_x,i) - array.get(_xhat,i),2)/_N _xmean = array.sum(_x)/_N _nrmse := sqrt(_mse) /_xmean _nrmse diff(_src,_window,_degree) => // Polynomial differentiator // input: // _src:: input series // _window:: integer: wigth of the moving lookback window // _degree:: integer: degree of fitting polynomial // output: // _diff :: series: time derivative // _nrmse:: float: normalized root mean square error // // Vandermonde matrix with implied dimensions (window x degree+1) // Linear form: J = [ [z]^0, [z]^1, ... [z]^degree], // with z = [ (1-window)/2 to (window-1)/2 ] var _J = array.new_float(_window*(_degree+1),0) for i = 0 to _window-1 for j = 0 to _degree matrix_set(_J,pow(i,j),i,j,_window) // Vector of raw datapoints: var _Y_raw = array.new_float(_window,na) for j = 0 to _window-1 array.set(_Y_raw,j,_src[_window-1-j]) // Calculate polynomial coefficients which minimize the loss function _C = pinv(_J,_window,_degree+1) _a_coef = multiply(_C,_Y_raw,_degree+1,_window,1) // For first derivative, approximate the last point (i.e. z=window-1) by float _diff = 0.0 for i = 1 to _degree _diff := _diff + i*array.get(_a_coef,i)*pow(_window-1,i-1) // Calculates data estimate (needed for rmse) _Y_hat = multiply(_J,_a_coef,_window,_degree+1,1) float _nrmse = norm_rmse(_Y_raw,_Y_hat) [_diff,_nrmse]/// --- main ---degree = input(title="Polynomial Order", group = "Model Parameters:", inline = "linepar1", type = input.integer, defval=3, minval = 1)rsi_l = input(title = "RSI Length", group = "Model Parameters:", inline = "linepar1", type = input.integer, defval = 21, minval = 1, tooltip="The period length of RSI that is used as input.")window = input(title="Length ( > Order)", group = "Model Parameters:", inline = "linepar2", type = input.integer, defval=50, minval = 2)signalLength = input(title="Signal Length", group = "Model Parameters:", inline = "linepar2", type=input.integer, defval=9, tooltip="The signal line is a EMA of the D-RSI time series.")islong = input(title = "Long", group = "Allowed Entries:", inline = "lineent",type = input.bool, defval = true)isshort = input(title = "Short", group = "Allowed Entries:", inline = "lineent", type = input.bool, defval= true)buycond = input(title="Buy", group = "Entry and Exit Conditions:", inline = "linecond",type = input.string, defval="Signal Line Crossing", options=["Zero-Crossing", "Signal Line Crossing","Direction Change"])sellcond = input(title="Sell", group = "Entry and Exit Conditions:", inline = "linecond",type = input.string, defval="Signal Line Crossing", options=["Zero-Crossing", "Signal Line Crossing","Direction Change"])endcond = input(title="Exit", group = "Entry and Exit Conditions:", inline = "linecond",type = input.string, defval="Signal Line Crossing", options=["Zero-Crossing", "Signal Line Crossing","Direction Change"])filterlong =input(title = "Long Entries", inline = 'linefilt', group = 'Apply Filters to', type = input.bool, defval = true)filtershort =input(title = "Short Enties", inline = 'linefilt', group = 'Apply Filters to', type = input.bool, defval = true)filterend =input(title = "Exits", inline = 'linefilt', group = 'Apply Filters to', type = input.bool, defval = true)usevol =input(title = "", inline = 'linefiltvol', group = 'Relative Volume Filter:', type = input.bool, defval = false)rvol = input(title = "Volume >", inline = 'linefiltvol', group = 'Relative Volume Filter:', type = input.integer, defval = 1)len_vol = input(title = "Avg. Volume Over Period", inline = 'linefiltvol', group = 'Relative Volume Filter:', type = input.integer, defval = 30, minval = 1, tooltip="The current volume must be greater than N times the M-period average volume.")useatr =input(title = "", inline = 'linefiltatr', group = 'Volatility Filter:', type = input.bool, defval = false)len_atr1 = input(title = "ATR", inline = 'linefiltatr', group = 'Volatility Filter:', type = input.integer, defval = 5, minval = 1)len_atr2 = input(title = "> ATR", inline = 'linefiltatr', group = 'Volatility Filter:', type = input.integer, defval = 30, minval = 1, tooltip="The N-period ATR must be greater than the M-period ATR.")usersi =input(title = "", inline = 'linersi', group = 'Overbought/Oversold Filter:', type = input.bool, defval = false)rsitrhs1 = input(title = "", inline = 'linersi', group = 'Overbought/Oversold Filter:', type = input.integer, defval = 0, minval=0, maxval=100)rsitrhs2 = input(title = "< RSI (14) <", inline = 'linersi', group = 'Overbought/Oversold Filter:', type = input.integer, defval = 100, minval=0, maxval=100, tooltip="RSI(14) must be in the range between N and M.")issl = input(title = "SL", inline = 'linesl1', group = 'Stop Loss / Take Profit:', type = input.bool, defval = false)slpercent = input(title = ", %", inline = 'linesl1', group = 'Stop Loss / Take Profit:', type = input.float, defval = 10, minval=0.0)istrailing = input(title = "Trailing", inline = 'linesl1', group = 'Stop Loss / Take Profit:', type = input.bool, defval = false)istp = input(title = "TP", inline = 'linetp1', group = 'Stop Loss / Take Profit:', type = input.bool, defval = false)tppercent = input(title = ", %", inline = 'linetp1', group = 'Stop Loss / Take Profit:', type = input.float, defval = 20)fixedstart =input(title="", group = "Fixed Backtest Period Start/End Dates:", inline = "linebac1", type = input.bool, defval = true)backtest_start=input(title = "", type = input.time, inline = "linebac1", group = "Fixed Backtest Period Start/End Dates:", defval = timestamp("01 Jan 2017 13:30 +0000"), tooltip="If deactivated, backtest staring from the first available price bar.")fixedend = input(title="", group = "Fixed Backtest Period Start/End Dates:", inline = "linebac2", type = input.bool, defval = false)backtest_end =input(title = "", type = input.time, inline = "linebac2", group = "Fixed Backtest Period Start/End Dates:", defval = timestamp("30 Dec 2080 23:30 +0000"), tooltip="If deactivated, backtesting ends at the last available price bar.")if window < degree window := degree+1src = rsi(close,rsi_l)[drsi,nrmse] = diff(src,window,degree)signalline = ema(drsi, signalLength)// Conditions for D-RSIdirchangeup = (drsi>drsi[1]) and (drsi[1]<drsi[2]) and drsi[1]<0.0dirchangedw = (drsi<drsi[1]) and (drsi[1]>drsi[2]) and drsi[1]>0.0crossup = crossover(drsi,0.0)crossdw = crossunder(drsi,0.0)crosssignalup = crossover(drsi,signalline)crosssignaldw = crossunder(drsi,signalline)// D-RSI signalsdrsilong = (buycond=="Direction Change"?dirchangeup:(buycond=="Zero-Crossing"?crossup:crosssignalup)) drsishort= (sellcond=="Direction Change"?dirchangedw:(sellcond=="Zero-Crossing"?crossdw:crosssignaldw)) drisendlong = (endcond=="Direction Change"?dirchangedw:(endcond=="Zero-Crossing"?crossdw:crosssignaldw)) drisendshort= (endcond=="Direction Change"?dirchangeup:(endcond=="Zero-Crossing"?crossup:crosssignalup)) // Filtersrsifilter = usersi?(rsi(close,14) > rsitrhs1 and rsi(close,14) < rsitrhs2):truevolatilityfilter = useatr?(atr(len_atr1) > atr(len_atr2)):truevolumefilter = usevol?(volume > rvol*sma(volume,len_vol)):truetotalfilter = volatilityfilter and volumefilter and rsifilter//Filtered signalsgolong = drsilong and islong and (filterlong?totalfilter:true) goshort = drsishort and isshort and (filtershort?totalfilter:true)endlong = drisendlong and (filterend?totalfilter:true)endshort = drisendlong and (filterend?totalfilter:true)// Backtest period//backtest_start = timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)//backtest_end = timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0)isinrange = (fixedstart?time>=backtest_start:true) and (fixedend?time<=backtest_end:true)// Entry price / Take profit / Stop Lossstartprice = valuewhen(condition=golong or goshort, source=close, occurrence=0)pm = golong?1:goshort?-1:1/sign(strategy.position_size)takeprofit = startprice*(1+pm*tppercent*0.01)// fixed stop lossstoploss = startprice * (1-pm*slpercent*0.01)// trailing stop lossif istrailing and strategy.position_size>0 stoploss := max(close*(1 - slpercent*0.01),stoploss[1])else if istrailing and strategy.position_size<0 stoploss := min(close*(1 + slpercent*0.01),stoploss[1])tpline = plot(takeprofit,color=color.blue,transp=100, title="TP")slline = plot(stoploss, color=color.red, transp=100, title="SL")p1 = plot(close,transp=100,color=color.white, title="Dummy Close")fill(p1, tpline, color=color.green, transp=istp?70:100, title="TP")fill(p1, slline, color=color.red, transp=issl?70:100, title="SL")// Backtest: Basic Entry and Exit Conditionsif golong and isinrange and islong strategy.entry("long", true ) alert("D-RSI Long " + syminfo.tickerid, alert.freq_once_per_bar_close) if goshort and isinrange and isshort strategy.entry("short", false) alert("D-RSI Short " + syminfo.tickerid, alert.freq_once_per_bar_close) if endlong strategy.close("long", alert_message="Close Long") alert("D-RSI Exit Long " + syminfo.tickerid, alert.freq_once_per_bar_close) if endshort strategy.close("short", alert_message="Close Short") alert("D-RSI Exit Short " + syminfo.tickerid, alert.freq_once_per_bar_close) // Exit via SL or TPstrategy.exit(id="sl/tp long", from_entry="long", stop=issl?stoploss:na, limit=istp?takeprofit:na, alert_message="Close Long")strategy.exit(id="sl/tp short",from_entry="short",stop=issl?stoploss:na, limit=istp?takeprofit:na, alert_message="Stop Loss Short")// Close if outside the rangeif (not isinrange) strategy.close_all()