Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

Simple SuperTrend Strategy for BTCUSD 4H

A Pine Script v5 strategy built on RSI (close, 6), Supertrend (2.5, 9) and Crossover (close, supertrend). It has 12 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 commits 1 in cash and pays 0.03 commission. 32 settings; defaults include Enter initial/current capital 100, % account risk per trade 2.5 and Over bought line rsi 70.

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 Simple SuperTrend Strategy for BTCUSD 4H repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 13 of 13 trades.
How did Simple SuperTrend Strategy for BTCUSD 4H do on BTCUSDT 1D with default settings?
+1.12%
From 26 Jan 2024 to 21 Oct 2026, over 72 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Does Simple SuperTrend Strategy for BTCUSD 4H 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: 72 before, 72 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
Does Simple SuperTrend Strategy for BTCUSD 4H use a stop loss?
Yes
strategy.exit sets a stop on line 109, 110, 113, 114, 124, 125, 128, 129.
Does Simple SuperTrend Strategy for BTCUSD 4H run in Zeal Terminal?
Yes
It compiles in 108 ms and runs 1,000 bars in 274 ms, in the browser.

BTCUSDT · 1D · default settings

  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Shape
  • Shape
  • Shape
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (32)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$109$203Equity from $100
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots. Zeal Terminal's engine currently merges fill() calls that share a title, so it would draw 7 of this script's 8 fills; this page gives each call its own title to draw all of them. 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.
Net profit
+1.12%
$1.12
Buy and hold
+102.5%
same period
Closed trades
72
28 won · 44 lost
Profitable
38.9%
of closed trades
Profit factor
1.02
gross profit ÷ gross loss
Max drawdown
19.46%
−$21.67

One run with the author's default settings, $100 starting capital, commission as the script sets it. A long 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.

Nothing to point out.

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Calculate position
Enter initial/current capital100floatcontracts_amount_c
% account risk per tradePercentage of total account to risk per trade. The USD value that should be used to risk the inserted percentage of the account. Appears green in the upper left corner2.5float · step 1long_amount_c short_amount_c
Leverage X10floatlong_amount_c short_amount_c
Time filter
Initial dateEnter the start date and time of the strategytimestamp("10 Feb 2014 13:30 +0000")timeentry L exit Tp exit Exit entry S exit Tp exit Exit bgcolor line 103 line 118
Final dateEnter the end date and time of the strategytimestamp("01 Jan 2030 19:30 +0000")timeentry L exit Tp exit Exit entry S exit Tp exit Exit bgcolor line 103 line 118
Date backgroundAdd color to the period of time of the strategy testerfalseboolbgcolor
ATR Length SuperTrend9intsupertrend direction
Factor SuperTrend2.5float · step 0.05supertrend direction
Rsi
Over bought line rsi70int · min 50 · max 100short_rsi_plot short_rsi_entry hline
Over sold line rsi30int · min 0 · max 50long_rsi_plot long_rsi_entry hline
Rsi Length6int · min 1 · max 200rsi_filter
Appearance
Show Rsi ?falseboolRsi hline
Show supertrend ?falseboolbodyMiddle Up Trend Down Trend plot
Draw stop loss ?Activate to display stop loss price. It appears as a white point on the charttrueboolplot
Draw position on chart ?Activate to graphically display profit, stop loss and break eventrueboolposition_price sl_long_price sl_short_price tp_long_price tp_short_price breakeven_long breakeven_short position_profit_long position_profit_short plot
Draw first take profit/breakeven price on chart ?Activate to display take profit and breakeven price. It appears as a green point in the charttrueboolplot
Draw signals on chart ?Activate to display long, close long, short and close short signals. Green triangle represents long and red triangle represents shorttrueboolplotshape
Atr stop loss
SourceclosesourceshortStopLoss longStopLoss
Period14int · min 1shortStopLoss longStopLoss
Atr multiplierDefines the stop loss distance based on the Atr stop loss indicator1.5float · min 0.1 · step 0.1shortStopLoss longStopLoss
Swing highs/lows stop losses
Highest price bars: 10intstop_high
Lowest price bars: 10intstop_low
Risk management for trades
Stop loss sourceChange this for getting stop loss based in Atr stop loss indicator or last swing high/lowAtr stop lossstring · options ["Atr stop loss","Swing high/low"]line 53 line 56
% Account risk per trade for backtestingPercentage of total account to risk per trade2.5float · step 1long_amount short_amount
Risk/reward for breakeven long0.75float · step 0.05be_long be_long_plot
Risk/reward for take profit long0.75float · step 0.05tp_long
Risk/reward for break even short0.75float · step 0.05be_short be_short_plot
Risk/reward for take profit short0.75float · step 0.05tp_short
% of trade for first take profitClosing percentage of the current position when the first take profit is reached.50float · step 5exit Tp
Re-entry filter
Use Re-entry filter ?Activate to use filter. This will help you to re enter in a trend movetrueboolentry L exit Tp exit Exit entry S exit Tp exit Exit line 103 line 118
Positions management
Long positions ?trueboolentry L exit Tp exit Exit line 103
Short positions ?trueboolentry S exit Tp exit Exit line 118

What it calculates

  • RSIclose, Rsi Length (6)rsi_filter · line 18
  • SupertrendFactor SuperTrend (2.5), ATR Length SuperTrend (9)supertrend · line 26
  • SupertrendFactor SuperTrend (2.5), ATR Length SuperTrend (9)direction · line 26
  • Crossoverclose, supertrendlong_supertrend_plot · line 38
  • Crossunderclose, supertrendshort_supertrend_plot · line 39
  • ATRPeriod (14)shortStopLoss · line 43
  • ATRPeriod (14)longStopLoss · line 44
  • Highesthigh, Highest price bars: (10)stop_high · line 49
  • Lowestlow, Lowest price bars: (10)stop_low · line 50
  • Crossoverclose, supertrendlong_supertrend · line 83
  • Crossunderclose, supertrendshort_supertrend · line 84

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

OrderFires whenBy defaultLine
Open L longnot bought and long_supertrend and dateFilter(initial_date, final_date) and long_positions or not bought and long_rsi_entry and dateFilter(initial_date, final_date) and use_rsi_filter and long_positionsOn108
Exit Tp stop and limitnot bought and long_supertrend and dateFilter(initial_date, final_date) and long_positions or not bought and long_rsi_entry and dateFilter(initial_date, final_date) and use_rsi_filter and long_positionsOn109
Exit Exit stopnot bought and long_supertrend and dateFilter(initial_date, final_date) and long_positions or not bought and long_rsi_entry and dateFilter(initial_date, final_date) and use_rsi_filter and long_positionsOn110
Exit Tp stop and limitbought and high > be_longOn113
Exit Exit stopbought and high > be_longOn114
Close Lbought and short_supertrendOn116
Open S shortnot sold and short_supertrend and dateFilter(initial_date, final_date) and short_positions or not sold and short_rsi_entry and dateFilter(initial_date, final_date) and use_rsi_filter and short_positionsOn123
Exit Tp stop and limitnot sold and short_supertrend and dateFilter(initial_date, final_date) and short_positions or not sold and short_rsi_entry and dateFilter(initial_date, final_date) and use_rsi_filter and short_positionsOn124
Exit Exit stopnot sold and short_supertrend and dateFilter(initial_date, final_date) and short_positions or not sold and short_rsi_entry and dateFilter(initial_date, final_date) and use_rsi_filter and short_positionsOn125
Exit Tp stop and limitsold and low < be_shortOn128
Exit Exit stopsold and low < be_shortOn129
Close Ssold and long_supertrendOn131

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.

13/13trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.

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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+1.1%+102.5%7239%1.0219.5%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+30.1%+5.5%7341%1.6713.9%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+19.3%+9.8%7242%1.4715.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+19.5%+8.4%6434%1.4114.7%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+22.0%+16.7%8241%1.6411.9%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+5.0%+21.5%7731%1.1122.2%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+13.5%+148.3%7636%1.2819.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−18.3%+17.1%8533%0.6925.5%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 barsTradesWonP&L
Rising market2124%−$12.45
Sideways market3546%$10.55
Falling market1644%$3.03

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

18.2%

The 95th percentile of the worst drawdown when the same 72 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 8.4% to 18.2%; the tick is what actually happened, 18.9%. Order luck alone moves it this much.

0%22%
All 72 trades
SideEntryExitBarsP&L
long9 Feb 2024 47,132.8012 Feb 2024 48,669.903$0.92
long9 Feb 2024 47,132.8019 Mar 2024 61,937.4039$9.01
short19 Mar 2024 61,937.4020 Mar 2024 67,619.801−$1.38
short19 Mar 2024 61,937.4020 Mar 2024 67,619.801−$1.38
long15 May 2024 66,206.5020 May 2024 69,385.605$0.99
long15 May 2024 66,206.5011 Jun 2024 66,206.5027−$0.01
long15 Jun 2024 66,228.3018 Jun 2024 65,175.303−$0.96
short18 Jun 2024 65,175.3024 Jun 2024 62,889.606$0.99
short18 Jun 2024 65,175.3015 Jul 2024 64,724.1027$0.18
long15 Jul 2024 64,724.1020 Jul 2024 67,500.005$1.20
long15 Jul 2024 64,724.1020 Jul 2024 67,500.005$0.80
short2 Aug 2024 61,498.304 Aug 2024 58,569.802$1.02
short2 Aug 2024 61,498.308 Aug 2024 61,498.306−$0.01
long23 Aug 2024 64,037.2027 Aug 2024 59,593.704−$1.40
long23 Aug 2024 64,037.2027 Aug 2024 59,593.704−$1.40
short1 Sep 2024 57,301.906 Sep 2024 54,456.505$1.01
short1 Sep 2024 57,301.909 Sep 2024 57,301.908−$0.01
long13 Sep 2024 60,498.0019 Sep 2024 63,263.006$1.01
long13 Sep 2024 60,498.001 Oct 2024 60,498.0018−$0.01
short1 Oct 2024 60,805.807 Oct 2024 64,079.906−$1.40
short1 Oct 2024 60,805.807 Oct 2024 64,079.906−$1.40
long14 Oct 2024 66,084.0018 Oct 2024 68,464.304$0.99
long14 Oct 2024 66,084.0023 Oct 2024 66,084.009−$0.02
short22 Dec 2024 95,186.306 Jan 2025 101,606.0015−$1.37
short22 Dec 2024 95,186.306 Jan 2025 101,606.0015−$1.37
short16 Jan 2025 99,987.3017 Jan 2025 105,505.001−$1.34
short16 Jan 2025 99,987.3017 Jan 2025 105,505.001−$1.34
long17 Jan 2025 104,077.0020 Jan 2025 108,396.003$0.95
long17 Jan 2025 104,077.0020 Jan 2025 102,260.003−$0.42
long3 Feb 2025 101,329.0012 Feb 2025 94,382.109−$1.31
long3 Feb 2025 101,329.0012 Feb 2025 94,382.109−$1.31
short24 Feb 2025 91,552.9025 Feb 2025 87,980.201$0.93
short24 Feb 2025 91,552.902 Mar 2025 91,552.906−$0.01
long2 Mar 2025 94,270.003 Mar 2025 87,820.101−$1.29
long2 Mar 2025 94,270.003 Mar 2025 87,820.101−$1.29
short9 Mar 2025 80,734.4024 Mar 2025 88,037.9015−$1.25
short9 Mar 2025 80,734.4024 Mar 2025 88,037.9015−$1.25
long21 Apr 2025 87,516.2022 Apr 2025 91,073.501$0.90
long21 Apr 2025 87,516.205 Jun 2025 101,509.0045$3.56
short5 Jun 2025 101,509.007 Jun 2025 105,836.002−$1.29
short5 Jun 2025 101,509.007 Jun 2025 105,836.002−$1.29
short30 Jun 2025 107,147.002 Jul 2025 108,850.002−$1.11
long2 Jul 2025 108,850.009 Jul 2025 111,933.007$0.90
long2 Jul 2025 108,850.001 Aug 2025 113,298.0030$1.30
short1 Aug 2025 113,298.007 Aug 2025 117,546.006−$1.36
short1 Aug 2025 113,298.007 Aug 2025 117,546.006−$1.18
long12 Aug 2025 120,134.0013 Aug 2025 123,138.001$0.89
long12 Aug 2025 120,134.0014 Aug 2025 120,134.002−$0.02
short19 Aug 2025 112,873.0022 Aug 2025 117,226.003−$1.25
short19 Aug 2025 112,873.0022 Aug 2025 117,226.003−$1.25
short14 Sep 2025 115,268.0016 Sep 2025 116,789.002−$1.04
long16 Sep 2025 116,789.0022 Sep 2025 113,153.006−$1.21
long16 Sep 2025 116,789.0022 Sep 2025 113,153.006−$1.21
short25 Sep 2025 108,994.0029 Sep 2025 112,680.004−$1.18
short25 Sep 2025 108,994.0029 Sep 2025 112,680.004−$1.18
long1 Oct 2025 118,595.003 Oct 2025 121,436.002$0.82
long1 Oct 2025 118,595.0010 Oct 2025 118,595.009−$0.02
short10 Oct 2025 112,775.0016 Oct 2025 108,053.006$0.84
short10 Oct 2025 112,775.005 Jan 2026 93,859.7087$3.40
long5 Jan 2026 93,859.708 Jan 2026 90,119.803−$1.21
long5 Jan 2026 93,859.708 Jan 2026 90,119.803−$1.21
short20 Jan 2026 88,427.7029 Jan 2026 85,644.309$0.85
short20 Jan 2026 88,427.704 Mar 2026 72,666.8043$4.91
long4 Mar 2026 72,666.807 Mar 2026 67,178.103−$1.24
long4 Mar 2026 72,666.807 Mar 2026 67,178.103−$1.24
short27 Mar 2026 66,407.307 Apr 2026 70,819.8011−$1.21
short27 Mar 2026 66,407.307 Apr 2026 70,819.8011−$1.21
long10 Apr 2026 72,962.7014 Apr 2026 75,821.804$0.86
long10 Apr 2026 72,962.7022 May 2026 75,539.5042$0.78
short22 May 2026 75,539.5028 May 2026 73,288.906$0.87
short22 May 2026 75,539.5014 Jul 2026 65,044.0053$4.14
long14 Jul 2026 65,044.0019 Aug 2026 67,347.6036$0.92

Similar scripts

Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.

ScriptSharesTypeNetTradesMax drawdown
Simple SuperTrend Strategy for BTCUSD 4H this page—v5 strategy+1.1%7219.5%
Dynamic Stop Loss Demo by Tindtilyhighest lowest rsi atrv5 strategydoes not run in Zeal Terminal
M0PB (Momentum Pullback) by Marcns_rsi highest lowest atrv5 strategy+51.3%317.5%
AlignedMA and Cumulative HighLow Strategy V2 by Trendoscopeatr supertrend highest lowestv4 strategy−40.8%1650.9%
Delayed RSI Strategy by tweakerIDlowest highest atr rsiv4 strategy−40.4%742.2%
Aggresive Scalper-Swing Crypto Strategy by exlux99lowest highest atrv4 strategy−7.4%316.0%

Upgrade and export

Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.

  • 2 settings use the untyped input() (line 24, 40). It still compiles in v6; input.int, input.float or input.source state the type.
  • No v6-only features. v6 is not needed to run this script.

Clean v5

VerifiedDownload the v5 script

Same 72 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 24: input() → input.int()
  • Line 40: input() → input.source()
⋯// ------------ Super Trend -----------atrPeriod = input(9, "ATR Length SuperTrend")+atrPeriod = input.int(9, "ATR Length SuperTrend")factor = input.float(2.5, "Factor SuperTrend", step = 0.05)⋯short_supertrend_plot=ta.crossunder(close, supertrend)-source_atr = input(close, title='Source', group = "Atr stop loss", inline = "A")+source_atr = input.source(close, title='Source', group = "Atr stop loss", inline = "A")length_atr = input.int(14, minval=1, title='Period', group = "Atr stop loss" , inline = "A")⋯

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: 72 before, 72 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.

⋯// Developed by © StrategiesForEveryone-//@version=5+//@version=6strategy("SuperTrend Strategy with Rsi filter for BTCUSD 4H", overlay=true, process_orders_on_close = true, initial_capital = 100, default_qty_type = strategy.cash, precision = 2, slippage = 0, commission_value = 0.03, backtest_fill_limits_assumption = 0)⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 108 msWith a workaround 1,000 bars in 274 msNot measured
PineTSNot testedNot testedNot measured

Zeal Terminal's engine currently merges fill() calls that share a title, so it would draw 7 of this script's 8 fills; this page gives each call its own title to draw all of them. 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.

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/// Developed by © StrategiesForEveryone//@version=5strategy("SuperTrend Strategy with Rsi filter for BTCUSD 4H", overlay=true, process_orders_on_close = true, initial_capital = 100, default_qty_type = strategy.cash, precision = 2, slippage = 0, commission_value = 0.03, backtest_fill_limits_assumption = 0)// ------ Inputs for calculating position --------initial_actual_capital = input.float(defval=100, title = "Enter initial/current capital", group = "Calculate position")risk_c = input.float(2.5, '% account risk per trade', step=1, group = "Calculate position", tooltip = "Percentage of total account to risk per trade. The USD value that should be used to risk the inserted percentage of the account. Appears green in the upper left corner")// ------ Date filter (obtained from ZenAndTheArtOfTrading) ---------initial_date = input.time(title="Initial date", defval=timestamp("10 Feb 2014 13:30 +0000"), group="Time filter", tooltip="Enter the start date and time of the strategy")final_date   = input.time(title="Final date", defval=timestamp("01 Jan 2030 19:30 +0000"), group="Time filter", tooltip="Enter the end date and time of the strategy")dateFilter(int st, int et) => time >= st and time <= etcolorDate = input.bool(defval=false, title="Date background", tooltip = "Add color to the period of time of the strategy tester")bgcolor(colorDate and dateFilter(initial_date, final_date) ? color.new(color.blue, transp=90) : na)// --------------- Rsi filter------------------overbought = input.int(70, title='Over bought line rsi', minval=50, maxval=100, group = "Rsi", inline = "b")oversold = input.int(30, title='Over sold line rsi', minval=0, maxval=50, group = "Rsi", inline = "b")periods = input.int(6, title='Rsi Length', minval=1, maxval=200, group = "Rsi", inline = "b")rsi_filter = ta.rsi(close, periods)show_rsi = input.bool(defval = false, title = "Show Rsi ?", group = "Appearance")plot(show_rsi ? rsi_filter : na, title = "Rsi", color = color.rgb(255, 255, 255), linewidth = 2, display = display.all - display.status_line)hline(show_rsi ? overbought : na, color=color.rgb(0, 132, 255))hline(show_rsi ? oversold : na, color=color.rgb(0, 132, 255))// ------------ Super Trend ----------atrPeriod = input(9, "ATR Length SuperTrend")factor = input.float(2.5, "Factor SuperTrend", step = 0.05)[supertrend, direction] = ta.supertrend(factor, atrPeriod)show_supertrend = input.bool(defval = false, title="Show supertrend ?", group = "Appearance")bodyMiddle = plot(show_supertrend ? ((open + close) / 2) : na, display=display.none)upTrend = plot(show_supertrend and direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr, display = display.all - display.status_line)downTrend = plot(show_supertrend and direction > 0 ? supertrend : na, "Down Trend", color = color.red, style=plot.style_linebr, display = display.all - display.status_line)fill(bodyMiddle, upTrend, color.new(color.green, 95), fillgaps=false, title = "Supertrend background")fill(bodyMiddle, downTrend, color.new(color.red, 95), fillgaps=false, title = "Supertrend background")up_trend_plot = direction < 0down_trend_plot = direction > 0// ---------- Atr stop loss (obtained from garethyeo)long_rsi_plot = ta.crossover(rsi_filter, oversold) and up_trend_plotshort_rsi_plot = ta.crossunder(rsi_filter, overbought) and down_trend_plotlong_supertrend_plot=ta.crossover(close, supertrend)short_supertrend_plot=ta.crossunder(close, supertrend)source_atr = input(close, title='Source', group = "Atr stop loss", inline = "A")length_atr = input.int(14, minval=1, title='Period', group = "Atr stop loss" , inline = "A")multiplier = input.float(1.5, minval=0.1, step=0.1, title='Atr multiplier', group = "Atr stop loss", inline = "A", tooltip = "Defines the stop loss distance based on the Atr stop loss indicator")shortStopLoss = source_atr + ta.atr(length_atr) * multiplierlongStopLoss = source_atr - ta.atr(length_atr) * multipliershow_stoploss = input.bool(defval=true, title="Draw stop loss ?", group = "Appearance", tooltip = "Activate to display stop loss price. It appears as a white point on the chart")// --------- Stop loss based in last swing high/low --------high_bars = input.int(defval = 10, title = "Highest price bars: ", group = "Swing highs/lows stop losses")low_bars = input.int(defval = 10, title = "Lowest price bars: ", group = "Swing highs/lows stop losses")stop_high = ta.highest(high, high_bars)stop_low = ta.lowest(low, low_bars)// --------- Stop loss source selection ---------------stoploss_type = input.string(defval = "Atr stop loss", title = "Stop loss source", group = "Risk management for trades", options = ["Atr stop loss","Swing high/low"], tooltip = "Change this for getting stop loss based in Atr stop loss indicator or last swing high/low")if stoploss_type == "Atr stop loss"    shortStopLoss := source_atr + ta.atr(length_atr) * multiplier    longStopLoss := source_atr - ta.atr(length_atr) * multiplierif stoploss_type == "Swing high/low"    shortStopLoss := stop_high    longStopLoss := stop_low// Ploting stop lossesplot(show_stoploss and long_supertrend_plot or show_stoploss and long_rsi_plot ? longStopLoss : na, color=color.rgb(255, 255, 255), style = plot.style_circles, linewidth = 2, display = display.all - display.price_scale)plot(show_stoploss and short_supertrend_plot or show_stoploss and short_rsi_plot ? shortStopLoss : na, color=color.rgb(255, 255, 255), style = plot.style_circles, linewidth = 2, display = display.all - display.price_scale)// ------------- Money management --------------strategy_contracts = strategy.equity / closedistance_sl_atr_long = -1 * (longStopLoss - close) / closedistance_sl_atr_short = (shortStopLoss - close) / closerisk = input.float(2.5, '% Account risk per trade for backtesting', step=1, group = "Risk management for trades", tooltip = "Percentage of total account to risk per trade")long_amount = strategy_contracts * (risk / 100) / distance_sl_atr_longshort_amount = strategy_contracts * (risk / 100) / distance_sl_atr_short// ---------- Risk management ---------------risk_reward_breakeven_long= input.float(title="Risk/reward for breakeven long", defval=0.75, step=0.05, group = "Risk management for trades")risk_reward_take_profit_long= input.float(title="Risk/reward for take profit long", defval=0.75, step=0.05, group = "Risk management for trades")risk_reward_breakeven_short= input.float(title="Risk/reward for break even short", defval=0.75, step=0.05, group = "Risk management for trades")risk_reward_take_profit_short= input.float(title="Risk/reward for take profit short", defval=0.75, step=0.05, group = "Risk management for trades")tp_percent=input.float(title="% of trade for first take profit", defval=50, step=5, group = "Risk management for trades", tooltip = "Closing percentage of the current position when the first take profit is reached.")// ------------ Trade conditions ---------------use_rsi_filter = input.bool(defval=true, title="Use Re-entry filter ?", group = "Re-entry filter", tooltip = "Activate to use filter. This will help you to re enter in a trend move")bought = strategy.position_size > 0sold = strategy.position_size < 0up_trend = direction < 0down_trend = direction > 0long_rsi_entry = ta.crossover(rsi_filter, oversold) and up_trend and strategy.openprofit[5]>=0 short_rsi_entry = ta.crossunder(rsi_filter, overbought) and down_trend and strategy.openprofit[5]>=0long_supertrend=ta.crossover(close, supertrend)short_supertrend=ta.crossunder(close, supertrend)var float sl_long = navar float sl_short = na var float be_long = navar float be_short = navar float tp_long = navar float tp_short = naif not bought    be_long:=na    sl_long:=na    tp_long:=naif not sold    be_short    sl_short:=na    tp_shortlong_positions = input.bool(defval = true, title = "Long positions ?", group = "Positions management")short_positions = input.bool(defval = true, title = "Short positions ?", group = "Positions management")// ---------- Strategy -----------// Long position if not bought and long_supertrend and dateFilter(initial_date, final_date) and long_positions or not bought and long_rsi_entry and dateFilter(initial_date, final_date) and use_rsi_filter and long_positions    sl_long:=longStopLoss               long_stoploss_distance = close - longStopLoss    be_long := close + long_stoploss_distance * risk_reward_breakeven_long    tp_long:=close+(long_stoploss_distance*risk_reward_take_profit_long)    strategy.entry('L', strategy.long, long_amount, alert_message = "Long")    strategy.exit("Tp", "L", stop=sl_long, limit=tp_long, qty_percent=tp_percent)    strategy.exit('Exit', 'L', stop=sl_long)if bought and high > be_long    sl_long := strategy.position_avg_price    strategy.exit("Tp", "L", stop=sl_long, limit=tp_long, qty_percent=tp_percent)    strategy.exit('Exit', 'L', stop=sl_long)if bought and short_supertrend    strategy.close("L", comment="CL")// Short positionif not sold and short_supertrend and dateFilter(initial_date, final_date) and short_positions or not sold and short_rsi_entry and dateFilter(initial_date, final_date) and use_rsi_filter and short_positions    sl_short:=shortStopLoss    short_stoploss_distance=shortStopLoss - close      be_short:=((short_stoploss_distance*risk_reward_breakeven_short)-close)*-1    tp_short:=((short_stoploss_distance*risk_reward_take_profit_short)-close)*-1    strategy.entry("S", strategy.short, short_amount, alert_message = "Short")    strategy.exit("Tp", "S", stop=sl_short, limit=tp_short, qty_percent=tp_percent)    strategy.exit("Exit", "S", stop=sl_short)if sold and low < be_short     sl_short:=strategy.position_avg_price    strategy.exit("Tp", "S", stop=sl_short, limit=tp_short, qty_percent=tp_percent)    strategy.exit("Exit", "S", stop=sl_short)    if sold and long_supertrend    strategy.close("S", comment="CS") // ---------- Draw positions and signals on chart (strategy as an indicator) -------------if high>tp_long    tp_long:=naif low<tp_short    tp_short:=naif high>be_long    be_long:=naif low<be_short    be_short:=nashow_position_on_chart = input.bool(defval=true, title="Draw position on chart ?", group = "Appearance", tooltip = "Activate to graphically display profit, stop loss and break even")position_price = plot(show_position_on_chart? strategy.position_avg_price : na, style=plot.style_linebr, color = color.new(#ffffff, 10), linewidth = 1, display = display.all - display.status_line - display.price_scale)sl_long_price = plot(show_position_on_chart and bought ? sl_long : na, style = plot.style_linebr, color = color.new(color.red, 10), linewidth = 1, display = display.all - display.status_line - display.price_scale)sl_short_price = plot(show_position_on_chart and sold ? sl_short : na, style = plot.style_linebr, color = color.new(color.red, 10), linewidth = 1, display = display.all - display.status_line - display.price_scale)tp_long_price = plot(strategy.position_size>0 and show_position_on_chart? tp_long : na, style = plot.style_linebr, color = color.new(#1fc9fd, 10), linewidth = 1, display = display.all - display.status_line - display.price_scale)tp_short_price = plot(strategy.position_size<0 and show_position_on_chart? tp_short : na, style = plot.style_linebr, color = color.new(#1fc9fd, 10), linewidth = 1, display = display.all - display.status_line - display.price_scale)breakeven_long = plot(strategy.position_size>0 and high<be_long and show_position_on_chart ? be_long : na , style = plot.style_linebr, color = color.new(#00ff40, 60), linewidth = 1, display = display.all - display.status_line - display.price_scale)breakeven_short = plot(strategy.position_size<0 and low>be_short and show_position_on_chart ? be_short : na , style = plot.style_linebr, color = color.new(#00ff40, 60), linewidth = 1, display = display.all - display.status_line - display.price_scale)show_break_even_on_chart = input.bool(defval=true, title="Draw first take profit/breakeven price on chart ?", group = "Appearance", tooltip = "Activate to display take profit and breakeven price. It appears as a green point in the chart")long_stoploss_distance = close - longStopLossshort_stoploss_distance=shortStopLoss - closebe_long_plot = close + long_stoploss_distance * risk_reward_breakeven_longbe_short_plot =((short_stoploss_distance*risk_reward_breakeven_short)-close)*-1plot(show_break_even_on_chart and long_supertrend_plot or show_break_even_on_chart and long_rsi_plot ? be_long_plot : na, color=color.new(#1fc9fd, 10), style = plot.style_circles, linewidth = 2, display = display.all - display.price_scale)plot(show_break_even_on_chart and short_supertrend_plot or show_break_even_on_chart and short_rsi_plot ? be_short_plot : na, color=color.new(#1fc9fd, 10), style = plot.style_circles, linewidth = 2, display = display.all - display.price_scale)position_profit_long = plot(bought and show_position_on_chart and strategy.openprofit>0 ? close : na, style = plot.style_linebr, color = color.new(#4cd350, 10), linewidth = 1, display = display.all - display.status_line - display.price_scale)position_profit_short = plot(sold and show_position_on_chart and strategy.openprofit>0 ? close : na, style = plot.style_linebr, color = color.new(#4cd350, 10), linewidth = 1, display = display.all - display.status_line - display.price_scale)fill(plot1 = position_price, plot2 = position_profit_long, color = color.new(color.green,90))fill(plot1 = position_price, plot2 = position_profit_short, color = color.new(color.green,90))fill(plot1 = position_price, plot2 = sl_long_price, color = color.new(color.red,90))fill(plot1 = position_price, plot2 = sl_short_price, color = color.new(color.red,90))fill(plot1 = position_price, plot2 = tp_long_price, color = color.new(color.green,90))fill(plot1 = position_price, plot2 = tp_short_price, color = color.new(color.green,90))show_signals = input.bool(defval=true, title="Draw signals on chart ?", group = "Appearance", tooltip = "Activate to display long, close long, short and close short signals. Green triangle represents long and red triangle represents short")plotshape(show_signals ? long_supertrend : na, style = shape.triangleup, location = location.belowbar, color = color.new(color.green,0), size = size.tiny, display = display.all - display.status_line - display.price_scale)plotshape(show_signals ? short_supertrend : na, style = shape.triangledown, location = location.abovebar, color = color.new(color.red,0), size = size.tiny, display = display.all - display.status_line - display.price_scale)plotshape(show_signals ? long_rsi_entry : na, style = shape.triangleup, location = location.belowbar, color = color.new(color.green,0), size = size.tiny, display = display.all - display.status_line - display.price_scale)plotshape(show_signals ? short_supertrend : na, style = shape.triangledown, location = location.abovebar, color = color.new(color.red,0), size = size.tiny, display = display.all - display.status_line - display.price_scale)// --------------- Position amount calculator -------------leverage_x = input.float(defval = 10, title = "Leverage X", group = "Calculate position")contracts_amount_c = initial_actual_capital / closedistance_sl_long_c = -1 * (longStopLoss - close) / close distance_sl_short_c = (shortStopLoss - close) / closelong_amount_c = (close * (contracts_amount_c * (risk_c / 100) / distance_sl_long_c))/leverage_xshort_amount_c = (close * (contracts_amount_c * (risk_c / 100) / distance_sl_short_c))/leverage_xplot(long_supertrend or long_rsi_entry ? long_amount_c : na, color = color.rgb(136, 255, 0), display = display.all - display.pane - display.price_scale)plot(short_supertrend or short_rsi_entry ? short_amount_c : na, color = color.rgb(136, 255, 0), display = display.all - display.pane - display.price_scale)