Pine Script v4 · Strategy
RSI TrueLevel Strategy
A Pine Script v4 strategy built on RSI (close, 14), Linreg (close, length) and Stdev (close, length). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 10% of equity and pays no commission. 23 settings; defaults include RSI Period 14, RSI Overbought Level 65 and RSI Oversold Level 40.
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 RSI TrueLevel Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
- How did RSI TrueLevel Strategy do on BTCUSDT 1D with default settings?
- +2.79%
- From 26 Jan 2024 to 21 Oct 2026, over 10 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can RSI TrueLevel Strategy be converted to Pine Script v5?
- No
- The automatic step from v4 to v5 changes the results, so no v5 download is offered; the edits are shown below.
- Does RSI TrueLevel Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does RSI TrueLevel Strategy run in Zeal Terminal?
- Yes
- It compiles in 57 ms and runs 1,000 bars in 329 ms, in the browser.
BTCUSDT · 1D · default settings
- Upper Band 1
- Lower Band 1
- Upper Band 2
- Lower Band 2
- Upper Band 3
- Lower Band 3
- Upper Band 4
- Lower Band 4
- Upper Band 5
- Lower Band 5
- Upper Band 6
- Lower Band 6
- Upper Band 7
- Lower Band 7
- Winning trade
- Losing trade
- Net profit
- +2.79%
- $2,790.04
- Buy and hold
- +102.5%
- same period
- Closed trades
- 10
- 8 won · 2 lost
- Profitable
- 80.0%
- of closed trades
- Profit factor
- 1.80
- gross profit ÷ gross loss
- Max drawdown
- 5.08%
- −$5,292.27
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. 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.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 172, 173. The upgraded version below moves each call inside an if block.
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 rsi → ta.rsi, linreg → ta.linreg, stdev → ta.stdev, max → math.max, min → math.min, highest → ta.highest, lowest → ta.lowest, crossover → ta.crossover, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 172, 173). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
DiffersTrades differ: 10 before, 10 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.integer) → input.int()
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 10: input(type = input.integer) → input.int()
- Line 11: input(type = input.integer) → input.int()
59 more edits
- Line 13: input(type = input.bool) → input.bool()
- Line 15: rsi → ta.rsi
- Line 17: input(type = input.integer) → input.int()
- Line 18: input(type = input.integer) → input.int()
- Line 19: input(type = input.integer) → input.int()
- Line 20: input(type = input.integer) → input.int()
- Line 21: input(type = input.integer) → input.int()
- Line 22: input(type = input.integer) → input.int()
- Line 23: input(type = input.integer) → input.int()
- Line 24: input(type = input.integer) → input.int()
- Line 25: input(type = input.integer) → input.int()
- Line 26: input(type = input.integer) → input.int()
- Line 27: input(type = input.integer) → input.int()
- Line 28: input(type = input.integer) → input.int()
- Line 29: input(type = input.integer) → input.int()
- Line 30: input(type = input.integer) → input.int()
- Line 31: input(type = input.color) → input.color()
- Line 32: input(type = input.float) → input.float()
- Line 33: input(type = input.source) → input.source()
- Line 36: linreg → ta.linreg
- Line 37: stdev → ta.stdev
- Line 42: linreg → ta.linreg
- Line 43: stdev → ta.stdev
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 123: max → math.max
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 124: min → math.min
- Line 126: highest → ta.highest
- Line 127: lowest → ta.lowest
- Line 163: crossover → ta.crossover
- Line 163: crossover → ta.crossover
- Line 164: crossunder → ta.crossunder
- Line 164: crossunder → ta.crossunder
- Line 170: crossunder → ta.crossunder
- Line 170: crossunder → ta.crossunder
- Line 171: crossover → ta.crossover
- Line 171: crossover → ta.crossover
⋯// © Julien_Eche-//@version=4+//@version=5strategy("RSI TrueLevel Strategy", shorttitle="RSI TL", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)// Input parameters for RSI-rsiPeriod = input(14, title="RSI Period", type=input.integer)+rsiPeriod = input.int(14, title="RSI Period")-rsiOverbought = input(65, title="RSI Overbought Level", type=input.integer)+rsiOverbought = input.int(65, title="RSI Overbought Level")-rsiOversold = input(40, title="RSI Oversold Level", type=input.integer)+rsiOversold = input.int(40, title="RSI Oversold Level")// Inputs for selecting bands-entry_band = input(12, title="Entry TrueLevel Band", type=input.integer, minval=1, maxval=14)+entry_band = input.int(12, title="Entry TrueLevel Band", minval=1, maxval=14)-exit_band = input(12, title="Exit TrueLevel Band", type=input.integer, minval=1, maxval=14)+exit_band = input.int(12, title="Exit TrueLevel Band", minval=1, maxval=14)// Input for long and short mode-long_and_short = input(false, title="Enable Long and Short", type=input.bool)+long_and_short = input.bool(false, title="Enable Long and Short")// Calculate the RSI-rsi = rsi(close, rsiPeriod)+rsi = ta.rsi(close, rsiPeriod)// User inputs-len1 = input(title="Length 1", type=input.integer, defval=126)+len1 = input.int(title="Length 1", defval=126)-len2 = input(title="Length 2", type=input.integer, defval=189)+len2 = input.int(title="Length 2", defval=189)-len3 = input(title="Length 3", type=input.integer, defval=252)+len3 = input.int(title="Length 3", defval=252)-len4 = input(title="Length 4", type=input.integer, defval=378)+len4 = input.int(title="Length 4", defval=378)-len5 = input(title="Length 5", type=input.integer, defval=504)+len5 = input.int(title="Length 5", defval=504)-len6 = input(title="Length 6", type=input.integer, defval=630)+len6 = input.int(title="Length 6", defval=630)-len7 = input(title="Length 7", type=input.integer, defval=756)+len7 = input.int(title="Length 7", defval=756)-len8 = input(title="Length 8", type=input.integer, defval=1008)+len8 = input.int(title="Length 8", defval=1008)-len9 = input(title="Length 9", type=input.integer, defval=1260)+len9 = input.int(title="Length 9", defval=1260)-len10 = input(title="Length 10", type=input.integer, defval=1638)+len10 = input.int(title="Length 10", defval=1638)-len11 = input(title="Length 11", type=input.integer, defval=2016)+len11 = input.int(title="Length 11", defval=2016)-len12 = input(title="Length 12", type=input.integer, defval=2646)+len12 = input.int(title="Length 12", defval=2646)-len13 = input(title="Length 13", type=input.integer, defval=3276)+len13 = input.int(title="Length 13", defval=3276)-len14 = input(title="Length 14", type=input.integer, defval=4284)+len14 = input.int(title="Length 14", defval=4284)-fill_color = input(title="Fill Color", type=input.color, defval=color.rgb(0, 191, 255, 95))+fill_color = input.color(title="Fill Color", defval=color.rgb(0, 191, 255, 95))-mult = input(title="Multiple", type=input.float, defval=1, step=0.2, options=[0.6, 0.8, 1, 1.2, 1.4])+mult = input.float(title="Multiple", defval=1, step=0.2, options=[0.6, 0.8, 1, 1.2, 1.4])-src = input(title="Source", type=input.source, defval=close)+src = input.source(title="Source", defval=close)// Upper band calculation functionupperBand(length) =>- linreg = linreg(src, length, 0)+ linreg = ta.linreg(src, length, 0)- stddev = mult * stdev(src, length)+ stddev = mult * ta.stdev(src, length) upperband = linreg + stddev⋯lowerBand(length) =>- linreg = linreg(src, length, 0)+ linreg = ta.linreg(src, length, 0)- stddev = mult * stdev(src, length)+ stddev = mult * ta.stdev(src, length) lowerband = linreg - stddev⋯// Calculate the trueLevelUpperBand and trueLevelLowerBand-trueLevelUpperBand = max(upperband_1, max(upperband_2, max(upperband_3, max(upperband_4, max(upperband_5, max(upperband_6, max(upperband_7, max(upperband_8, max(upperband_9, max(upperband_10, max(upperband_11, max(upperband_12, max(upperband_13, upperband_14)))))))))))))+trueLevelUpperBand = math.max(upperband_1, math.max(upperband_2, math.max(upperband_3, math.max(upperband_4, math.max(upperband_5, math.max(upperband_6, math.max(upperband_7, math.max(upperband_8, math.max(upperband_9, math.max(upperband_10, math.max(upperband_11, math.max(upperband_12, math.max(upperband_13, upperband_14)))))))))))))-trueLevelLowerBand = min(lowerband_1, min(lowerband_2, min(lowerband_3, min(lowerband_4, min(lowerband_5, min(lowerband_6, min(lowerband_7, min(lowerband_8, min(lowerband_9, min(lowerband_10, min(lowerband_11, min(lowerband_12, min(lowerband_13, lowerband_14)))))))))))))+trueLevelLowerBand = math.min(lowerband_1, math.min(lowerband_2, math.min(lowerband_3, math.min(lowerband_4, math.min(lowerband_5, math.min(lowerband_6, math.min(lowerband_7, math.min(lowerband_8, math.min(lowerband_9, math.min(lowerband_10, math.min(lowerband_11, math.min(lowerband_12, math.min(lowerband_13, lowerband_14)))))))))))))// Update the highest upper band and lowest lower band-highestUpperBand := highest(trueLevelUpperBand, 1)+highestUpperBand := ta.highest(trueLevelUpperBand, 1)-lowestLowerBand := lowest(trueLevelLowerBand, 1)+lowestLowerBand := ta.lowest(trueLevelLowerBand, 1)// Store the upper and lower bands in an array for easy access⋯// Entry conditions-longCondition = crossover(rsi, rsiOversold) or crossover(close, selected_entry_lowerband)+longCondition = ta.crossover(rsi, rsiOversold) or ta.crossover(close, selected_entry_lowerband)-shortCondition = crossunder(rsi, rsiOverbought) or crossunder(close, selected_exit_upperband)+shortCondition = ta.crossunder(rsi, rsiOverbought) or ta.crossunder(close, selected_exit_upperband)if (longCondition)⋯// Exit conditions-exitLongCondition = crossunder(rsi, rsiOverbought) or crossunder(close, selected_exit_upperband)+exitLongCondition = ta.crossunder(rsi, rsiOverbought) or ta.crossunder(close, selected_exit_upperband)-exitShortCondition = crossover(rsi, rsiOversold) or crossover(close, selected_entry_lowerband)+exitShortCondition = ta.crossover(rsi, rsiOversold) or ta.crossover(close, selected_entry_lowerband)strategy.close("Long", when=exitLongCondition)⋯Clean v5
VerifiedSame 10 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 172: when = exitLongCondition → if block
- Line 173: when = long_and_short and exitShortCondition → if block
⋯exitShortCondition = ta.crossover(rsi, rsiOversold) or ta.crossover(close, selected_entry_lowerband)-strategy.close("Long", when=exitLongCondition)+if exitLongCondition+ strategy.close("Long")-strategy.close("Short", when=long_and_short and exitShortCondition)+if long_and_short and exitShortCondition+ strategy.close("Short")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: 10 before, 10 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.
⋯// © Julien_Eche-//@version=5+//@version=6strategy("RSI TrueLevel Strategy", shorttitle="RSI TL", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)⋯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 |
|---|---|---|---|
| RSI Period | 14 | int (written input.integer) | rsi |
| RSI Overbought Level | 65 | int (written input.integer) | shortCondition exitLongCondition |
| RSI Oversold Level | 40 | int (written input.integer) | longCondition exitShortCondition |
| Entry TrueLevel Band | 12 | int (written input.integer) · min 1 · max 14 | selected_entry_lowerband |
| Exit TrueLevel Band | 12 | int (written input.integer) · min 1 · max 14 | selected_exit_upperband |
| Enable Long and Short | false | bool | entry Short line 167 close Short |
| Length 1 | 126 | int (written input.integer) | upperband_1 lowerband_1 |
| Length 2 | 189 | int (written input.integer) | upperband_2 lowerband_2 |
| Length 3 | 252 | int (written input.integer) | upperband_3 lowerband_3 |
| Length 4 | 378 | int (written input.integer) | upperband_4 lowerband_4 |
| Length 5 | 504 | int (written input.integer) | upperband_5 lowerband_5 |
| Length 6 | 630 | int (written input.integer) | upperband_6 lowerband_6 |
| Length 7 | 756 | int (written input.integer) | upperband_7 lowerband_7 |
| Length 8 | 1008 | int (written input.integer) | upperband_8 lowerband_8 |
| Length 9 | 1260 | int (written input.integer) | upperband_9 lowerband_9 |
| Length 10 | 1638 | int (written input.integer) | upperband_10 lowerband_10 |
| Length 11 | 2016 | int (written input.integer) | upperband_11 lowerband_11 |
| Length 12 | 2646 | int (written input.integer) | upperband_12 lowerband_12 |
| Length 13 | 3276 | int (written input.integer) | upperband_13 lowerband_13 |
| Length 14 | 4284 | int (written input.integer) | upperband_14 lowerband_14 |
| Fill Color | color.rgb(0, 191, 255, 95) | color | Fill 1 Fill 2 Fill 3 Fill 4 Fill 5 Fill 6 Fill 7 Fill 8 Fill 9 Fill 10 Fill 11 Fill 12 Fill 13 Fill 14 |
| Multiple | 1 | float · step 0.2 · options [0.6, 0.8, 1, 1.2, 1.4] | stddev |
| Source | close | source | linreg stddev |
What it calculates
- RSI (
rsi,ta.rsiin v5)close, RSI Period (14)rsi · line 15 - Linreg (
linreg,ta.linregin v5)Source (close), length, 0linreg · line 42 - Stdev (
stdev,ta.stdevin v5)Source (close), lengthstddev · line 43 - Highest (
highest,ta.highestin v5)trueLevelUpperBand, 1highestUpperBand · line 126 - Lowest (
lowest,ta.lowestin v5)trueLevelLowerBand, 1lowestLowerBand · line 127
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 95 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 | (longCondition) | On | 166 |
| Open Short short | (long_and_short and shortCondition) | On | 168 |
| Close Long | exitLongConditioncrossunder(rsi, rsiOverbought) or crossunder(close, selected_exit_upperband) | On | 172 |
| Close Short | long_and_short and exitShortCondition | On | 173 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
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 | +2.8% | +102.5% | 10 | 80% | 1.80 | 5.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.2% | +5.5% | 10 | 70% | 1.12 | 2.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.4% | +9.8% | 8 | 50% | 0.85 | 6.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.8% | +8.4% | 9 | 78% | 0.73 | 3.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +5.8% | +16.7% | 11 | 64% | 1.63 | 8.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +2.7% | +21.5% | 8 | 75% | 2.34 | 3.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.4% | +148.3% | 7 | 71% | 0.88 | 4.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +3.9% | +17.1% | 8 | 88% | 15.18 | 1.1% | 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 | 3 | 100% | $2,574.12 |
| Sideways market | 6 | 83% | $1,790.10 |
| Falling market | 1 | 0% | −$1,574.18 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.4%
The 95th percentile of the worst drawdown when the same 10 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.8% to 3.4%; the tick is what actually happened, 3.4%. Order luck alone moves it this much.
All 10 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 19 Apr 2024 63,470.10 | 22 May 2024 70,148.30 | 33 | $1,052.19 |
| long | 1 Jul 2024 62,772.00 | 24 Jul 2024 65,936.00 | 23 | $509.35 |
| long | 9 Aug 2024 61,686.00 | 1 Oct 2024 63,327.60 | 53 | $270.28 |
| long | 3 Mar 2025 94,270.00 | 5 May 2025 94,277.60 | 63 | $0.82 |
| long | 24 Jun 2025 105,334.00 | 21 Jul 2025 117,265.00 | 27 | $1,153.46 |
| long | 4 Aug 2025 114,209.00 | 15 Aug 2025 118,295.00 | 11 | $368.47 |
| long | 27 Aug 2025 111,763.00 | 8 Oct 2025 121,333.00 | 42 | $884.98 |
| long | 13 Oct 2025 114,959.00 | 7 Jan 2026 93,748.00 | 86 | −$1,923.30 |
| long | 22 Jan 2026 89,454.70 | 19 Apr 2026 75,691.80 | 87 | −$1,574.18 |
| long | 15 Jun 2026 65,746.40 | 8 Sep 2026 79,112.00 | 85 | $2,047.98 |
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 |
|---|---|---|---|---|---|
| RSI TrueLevel Strategy this page | — | v4 strategy | +2.8% | 10 | 5.1% |
| VixFixLinReg-Strategy by Trendoscope | highest stdev lowest linreg | v4 strategy | −49.3% | 12 | 51.1% |
| MACD TrueLevel Strategy by Julien_Eche | linreg stdev highest lowest | v4 strategy | +3.6% | 38 | 3.0% |
| BTC 15 min by RafaelZioni | rsi linreg stdev highest lowest | v4 strategy | −25.7% | 119 | 38.9% |
| PlanB Quant Investing 101 v2 by fillippone | rsi highest lowest | v4 strategy | No trades on this market | ||
| MZ SRSI Strategy V1.0 by MightyZinger | highest lowest stdev rsi | v4 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 57 ms | Yes 1,000 bars in 329 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
“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/// © Julien_Eche//@version=4strategy("RSI TrueLevel Strategy", shorttitle="RSI TL", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)// Input parameters for RSIrsiPeriod = input(14, title="RSI Period", type=input.integer)rsiOverbought = input(65, title="RSI Overbought Level", type=input.integer)rsiOversold = input(40, title="RSI Oversold Level", type=input.integer)// Inputs for selecting bandsentry_band = input(12, title="Entry TrueLevel Band", type=input.integer, minval=1, maxval=14)exit_band = input(12, title="Exit TrueLevel Band", type=input.integer, minval=1, maxval=14)// Input for long and short modelong_and_short = input(false, title="Enable Long and Short", type=input.bool)// Calculate the RSIrsi = rsi(close, rsiPeriod)// User inputslen1 = input(title="Length 1", type=input.integer, defval=126)len2 = input(title="Length 2", type=input.integer, defval=189)len3 = input(title="Length 3", type=input.integer, defval=252)len4 = input(title="Length 4", type=input.integer, defval=378)len5 = input(title="Length 5", type=input.integer, defval=504)len6 = input(title="Length 6", type=input.integer, defval=630)len7 = input(title="Length 7", type=input.integer, defval=756)len8 = input(title="Length 8", type=input.integer, defval=1008)len9 = input(title="Length 9", type=input.integer, defval=1260)len10 = input(title="Length 10", type=input.integer, defval=1638)len11 = input(title="Length 11", type=input.integer, defval=2016)len12 = input(title="Length 12", type=input.integer, defval=2646)len13 = input(title="Length 13", type=input.integer, defval=3276)len14 = input(title="Length 14", type=input.integer, defval=4284)fill_color = input(title="Fill Color", type=input.color, defval=color.rgb(0, 191, 255, 95))mult = input(title="Multiple", type=input.float, defval=1, step=0.2, options=[0.6, 0.8, 1, 1.2, 1.4])src = input(title="Source", type=input.source, defval=close)// Upper band calculation functionupperBand(length) => linreg = linreg(src, length, 0) stddev = mult * stdev(src, length) upperband = linreg + stddev upperband// Lower band calculation functionlowerBand(length) => linreg = linreg(src, length, 0) stddev = mult * stdev(src, length) lowerband = linreg - stddev lowerband// Calculate upper and lower bands for each lengthupperband_1 = upperBand(len1)upperband_2 = upperBand(len2)upperband_3 = upperBand(len3)upperband_4 = upperBand(len4)upperband_5 = upperBand(len5)upperband_6 = upperBand(len6)upperband_7 = upperBand(len7)upperband_8 = upperBand(len8)upperband_9 = upperBand(len9)upperband_10 = upperBand(len10)upperband_11 = upperBand(len11)upperband_12 = upperBand(len12)upperband_13 = upperBand(len13)upperband_14 = upperBand(len14)lowerband_1 = lowerBand(len1)lowerband_2 = lowerBand(len2)lowerband_3 = lowerBand(len3)lowerband_4 = lowerBand(len4)lowerband_5 = lowerBand(len5)lowerband_6 = lowerBand(len6)lowerband_7 = lowerBand(len7)lowerband_8 = lowerBand(len8)lowerband_9 = lowerBand(len9)lowerband_10 = lowerBand(len10)lowerband_11 = lowerBand(len11)lowerband_12 = lowerBand(len12)lowerband_13 = lowerBand(len13)lowerband_14 = lowerBand(len14)// Plot envelope bands for each lengthupperband_1_plot = plot(upperband_1, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 1")lowerband_1_plot = plot(lowerband_1, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 1")upperband_2_plot = plot(upperband_2, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 2")lowerband_2_plot = plot(lowerband_2, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 2")upperband_3_plot = plot(upperband_3, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 3")lowerband_3_plot = plot(lowerband_3, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 3")upperband_4_plot = plot(upperband_4, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 4")lowerband_4_plot = plot(lowerband_4, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 4")upperband_5_plot = plot(upperband_5, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 5")lowerband_5_plot = plot(lowerband_5, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 5")upperband_6_plot = plot(upperband_6, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 6")lowerband_6_plot = plot(lowerband_6, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 6")upperband_7_plot = plot(upperband_7, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 7")lowerband_7_plot = plot(lowerband_7, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 7")upperband_8_plot = plot(upperband_8, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 8")lowerband_8_plot = plot(lowerband_8, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 8")upperband_9_plot = plot(upperband_9, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 9")lowerband_9_plot = plot(lowerband_9, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 9")upperband_10_plot = plot(upperband_10, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 10")lowerband_10_plot = plot(lowerband_10, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 10")upperband_11_plot = plot(upperband_11, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 11")lowerband_11_plot = plot(lowerband_11, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 11")upperband_12_plot = plot(upperband_12, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 12")lowerband_12_plot = plot(lowerband_12, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Lower Band 12")upperband_13_plot = plot(upperband_13, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 13")lowerband_13_plot = plot(lowerband_13, color=color.rgb(14, 139, 212, 95), linewidth=1, title="Lower Band 13")upperband_14_plot = plot(upperband_14, color=color.rgb(14, 116, 212, 95), linewidth=1, title="Upper Band 14")lowerband_14_plot = plot(lowerband_14, color=color.rgb(14, 139, 212, 95), linewidth=1, title="Lower Band 14")// Plot fills for each lengthfill(upperband_1_plot, lowerband_1_plot, color=fill_color, title="Fill 1")fill(upperband_2_plot, lowerband_2_plot, color=fill_color, title="Fill 2")fill(upperband_3_plot, lowerband_3_plot, color=fill_color, title="Fill 3")fill(upperband_4_plot, lowerband_4_plot, color=fill_color, title="Fill 4")fill(upperband_5_plot, lowerband_5_plot, color=fill_color, title="Fill 5")fill(upperband_6_plot, lowerband_6_plot, color=fill_color, title="Fill 6")fill(upperband_7_plot, lowerband_7_plot, color=fill_color, title="Fill 7")fill(upperband_8_plot, lowerband_8_plot, color=fill_color, title="Fill 8")fill(upperband_9_plot, lowerband_9_plot, color=fill_color, title="Fill 9")fill(upperband_10_plot, lowerband_10_plot, color=fill_color, title="Fill 10")fill(upperband_11_plot, lowerband_11_plot, color=fill_color, title="Fill 11")fill(upperband_12_plot, lowerband_12_plot, color=fill_color, title="Fill 12")fill(upperband_13_plot, lowerband_13_plot, color=fill_color, title="Fill 13")fill(upperband_14_plot, lowerband_14_plot, color=fill_color, title="Fill 14")// Add variables to store the highest upper band and lowest lower band valuesvar float highestUpperBand = navar float lowestLowerBand = na// Calculate the trueLevelUpperBand and trueLevelLowerBandtrueLevelUpperBand = max(upperband_1, max(upperband_2, max(upperband_3, max(upperband_4, max(upperband_5, max(upperband_6, max(upperband_7, max(upperband_8, max(upperband_9, max(upperband_10, max(upperband_11, max(upperband_12, max(upperband_13, upperband_14)))))))))))))trueLevelLowerBand = min(lowerband_1, min(lowerband_2, min(lowerband_3, min(lowerband_4, min(lowerband_5, min(lowerband_6, min(lowerband_7, min(lowerband_8, min(lowerband_9, min(lowerband_10, min(lowerband_11, min(lowerband_12, min(lowerband_13, lowerband_14)))))))))))))// Update the highest upper band and lowest lower bandhighestUpperBand := highest(trueLevelUpperBand, 1)lowestLowerBand := lowest(trueLevelLowerBand, 1)// Store the upper and lower bands in an array for easy accessupperbands = array.new_float(14)lowerbands = array.new_float(14)array.set(upperbands, 0, upperband_1)array.set(upperbands, 1, upperband_2)array.set(upperbands, 2, upperband_3)array.set(upperbands, 3, upperband_4)array.set(upperbands, 4, upperband_5)array.set(upperbands, 5, upperband_6)array.set(upperbands, 6, upperband_7)array.set(upperbands, 7, upperband_8)array.set(upperbands, 8, upperband_9)array.set(upperbands, 9, upperband_10)array.set(upperbands, 10, upperband_11)array.set(upperbands, 11, upperband_12)array.set(upperbands, 12, upperband_13)array.set(upperbands, 13, upperband_14)array.set(lowerbands, 0, lowerband_1)array.set(lowerbands, 1, lowerband_2)array.set(lowerbands, 2, lowerband_3)array.set(lowerbands, 3, lowerband_4)array.set(lowerbands, 4, lowerband_5)array.set(lowerbands, 5, lowerband_6)array.set(lowerbands, 6, lowerband_7)array.set(lowerbands, 7, lowerband_8)array.set(lowerbands, 8, lowerband_9)array.set(lowerbands, 9, lowerband_10)array.set(lowerbands, 10, lowerband_11)array.set(lowerbands, 11, lowerband_12)array.set(lowerbands, 12, lowerband_13)array.set(lowerbands, 13, lowerband_14)// Get the selected bands for entry and exitselected_entry_lowerband = array.get(lowerbands, entry_band - 1)selected_exit_upperband = array.get(upperbands, exit_band - 1)// Entry conditionslongCondition = crossover(rsi, rsiOversold) or crossover(close, selected_entry_lowerband)shortCondition = crossunder(rsi, rsiOverbought) or crossunder(close, selected_exit_upperband)if (longCondition) strategy.entry("Long", strategy.long)if (long_and_short and shortCondition) strategy.entry("Short", strategy.short)// Exit conditionsexitLongCondition = crossunder(rsi, rsiOverbought) or crossunder(close, selected_exit_upperband)exitShortCondition = crossover(rsi, rsiOversold) or crossover(close, selected_entry_lowerband)strategy.close("Long", when=exitLongCondition)strategy.close("Short", when=long_and_short and exitShortCondition)