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
- 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Calculate position | |||
| Enter initial/current capital | 100 | float | contracts_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 corner | 2.5 | float · step 1 | long_amount_c short_amount_c |
| Leverage X | 10 | float | long_amount_c short_amount_c |
| Time filter | |||
| Initial dateEnter the start date and time of the strategy | timestamp("10 Feb 2014 13:30 +0000") | time | entry 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 strategy | timestamp("01 Jan 2030 19:30 +0000") | time | entry 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 tester | false | bool | bgcolor |
| ATR Length SuperTrend | 9 | int | supertrend direction |
| Factor SuperTrend | 2.5 | float · step 0.05 | supertrend direction |
| Rsi | |||
| Over bought line rsi | 70 | int · min 50 · max 100 | short_rsi_plot short_rsi_entry hline |
| Over sold line rsi | 30 | int · min 0 · max 50 | long_rsi_plot long_rsi_entry hline |
| Rsi Length | 6 | int · min 1 · max 200 | rsi_filter |
| Appearance | |||
| Show Rsi ? | false | bool | Rsi hline |
| Show supertrend ? | false | bool | bodyMiddle Up Trend Down Trend plot |
| Draw stop loss ?Activate to display stop loss price. It appears as a white point on the chart | true | bool | plot |
| Draw position on chart ?Activate to graphically display profit, stop loss and break even | true | bool | position_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 chart | true | bool | plot |
| Draw signals on chart ?Activate to display long, close long, short and close short signals. Green triangle represents long and red triangle represents short | true | bool | plotshape |
| Atr stop loss | |||
| Source | close | source | shortStopLoss longStopLoss |
| Period | 14 | int · min 1 | shortStopLoss longStopLoss |
| Atr multiplierDefines the stop loss distance based on the Atr stop loss indicator | 1.5 | float · min 0.1 · step 0.1 | shortStopLoss longStopLoss |
| Swing highs/lows stop losses | |||
| Highest price bars: | 10 | int | stop_high |
| Lowest price bars: | 10 | int | stop_low |
| Risk management for trades | |||
| Stop loss sourceChange this for getting stop loss based in Atr stop loss indicator or last swing high/low | Atr stop loss | string · options ["Atr stop loss","Swing high/low"] | line 53 line 56 |
| % Account risk per trade for backtestingPercentage of total account to risk per trade | 2.5 | float · step 1 | long_amount short_amount |
| Risk/reward for breakeven long | 0.75 | float · step 0.05 | be_long be_long_plot |
| Risk/reward for take profit long | 0.75 | float · step 0.05 | tp_long |
| Risk/reward for break even short | 0.75 | float · step 0.05 | be_short be_short_plot |
| Risk/reward for take profit short | 0.75 | float · step 0.05 | tp_short |
| % of trade for first take profitClosing percentage of the current position when the first take profit is reached. | 50 | float · step 5 | exit Tp |
| Re-entry filter | |||
| Use Re-entry filter ?Activate to use filter. This will help you to re enter in a trend move | true | bool | entry L exit Tp exit Exit entry S exit Tp exit Exit line 103 line 118 |
| Positions management | |||
| Long positions ? | true | bool | entry L exit Tp exit Exit line 103 |
| Short positions ? | true | bool | entry 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, supertrend
long_supertrend_plot · line 38 - Crossunderclose, supertrend
short_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, supertrend
long_supertrend · line 83 - Crossunderclose, supertrend
short_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
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open L long | 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 | On | 108 |
| Exit Tp stop and limit | 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 | On | 109 |
| Exit Exit stop | 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 | On | 110 |
| Exit Tp stop and limit | bought and high > be_long | On | 113 |
| Exit Exit stop | bought and high > be_long | On | 114 |
| Close L | bought and short_supertrend | On | 116 |
| Open S short | 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 | On | 123 |
| Exit Tp stop and limit | 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 | On | 124 |
| Exit Exit stop | 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 | On | 125 |
| Exit Tp stop and limit | sold and low < be_short | On | 128 |
| Exit Exit stop | sold and low < be_short | On | 129 |
| Close S | sold and long_supertrend | On | 131 |
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 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.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +1.1% | +102.5% | 72 | 39% | 1.02 | 19.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +30.1% | +5.5% | 73 | 41% | 1.67 | 13.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +19.3% | +9.8% | 72 | 42% | 1.47 | 15.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +19.5% | +8.4% | 64 | 34% | 1.41 | 14.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +22.0% | +16.7% | 82 | 41% | 1.64 | 11.9% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +5.0% | +21.5% | 77 | 31% | 1.11 | 22.2% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +13.5% | +148.3% | 76 | 36% | 1.28 | 19.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −18.3% | +17.1% | 85 | 33% | 0.69 | 25.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 21 | 24% | −$12.45 |
| Sideways market | 35 | 46% | $10.55 |
| Falling market | 16 | 44% | $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.
All 72 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 9 Feb 2024 47,132.80 | 12 Feb 2024 48,669.90 | 3 | $0.92 |
| long | 9 Feb 2024 47,132.80 | 19 Mar 2024 61,937.40 | 39 | $9.01 |
| short | 19 Mar 2024 61,937.40 | 20 Mar 2024 67,619.80 | 1 | −$1.38 |
| short | 19 Mar 2024 61,937.40 | 20 Mar 2024 67,619.80 | 1 | −$1.38 |
| long | 15 May 2024 66,206.50 | 20 May 2024 69,385.60 | 5 | $0.99 |
| long | 15 May 2024 66,206.50 | 11 Jun 2024 66,206.50 | 27 | −$0.01 |
| long | 15 Jun 2024 66,228.30 | 18 Jun 2024 65,175.30 | 3 | −$0.96 |
| short | 18 Jun 2024 65,175.30 | 24 Jun 2024 62,889.60 | 6 | $0.99 |
| short | 18 Jun 2024 65,175.30 | 15 Jul 2024 64,724.10 | 27 | $0.18 |
| long | 15 Jul 2024 64,724.10 | 20 Jul 2024 67,500.00 | 5 | $1.20 |
| long | 15 Jul 2024 64,724.10 | 20 Jul 2024 67,500.00 | 5 | $0.80 |
| short | 2 Aug 2024 61,498.30 | 4 Aug 2024 58,569.80 | 2 | $1.02 |
| short | 2 Aug 2024 61,498.30 | 8 Aug 2024 61,498.30 | 6 | −$0.01 |
| long | 23 Aug 2024 64,037.20 | 27 Aug 2024 59,593.70 | 4 | −$1.40 |
| long | 23 Aug 2024 64,037.20 | 27 Aug 2024 59,593.70 | 4 | −$1.40 |
| short | 1 Sep 2024 57,301.90 | 6 Sep 2024 54,456.50 | 5 | $1.01 |
| short | 1 Sep 2024 57,301.90 | 9 Sep 2024 57,301.90 | 8 | −$0.01 |
| long | 13 Sep 2024 60,498.00 | 19 Sep 2024 63,263.00 | 6 | $1.01 |
| long | 13 Sep 2024 60,498.00 | 1 Oct 2024 60,498.00 | 18 | −$0.01 |
| short | 1 Oct 2024 60,805.80 | 7 Oct 2024 64,079.90 | 6 | −$1.40 |
| short | 1 Oct 2024 60,805.80 | 7 Oct 2024 64,079.90 | 6 | −$1.40 |
| long | 14 Oct 2024 66,084.00 | 18 Oct 2024 68,464.30 | 4 | $0.99 |
| long | 14 Oct 2024 66,084.00 | 23 Oct 2024 66,084.00 | 9 | −$0.02 |
| short | 22 Dec 2024 95,186.30 | 6 Jan 2025 101,606.00 | 15 | −$1.37 |
| short | 22 Dec 2024 95,186.30 | 6 Jan 2025 101,606.00 | 15 | −$1.37 |
| short | 16 Jan 2025 99,987.30 | 17 Jan 2025 105,505.00 | 1 | −$1.34 |
| short | 16 Jan 2025 99,987.30 | 17 Jan 2025 105,505.00 | 1 | −$1.34 |
| long | 17 Jan 2025 104,077.00 | 20 Jan 2025 108,396.00 | 3 | $0.95 |
| long | 17 Jan 2025 104,077.00 | 20 Jan 2025 102,260.00 | 3 | −$0.42 |
| long | 3 Feb 2025 101,329.00 | 12 Feb 2025 94,382.10 | 9 | −$1.31 |
| long | 3 Feb 2025 101,329.00 | 12 Feb 2025 94,382.10 | 9 | −$1.31 |
| short | 24 Feb 2025 91,552.90 | 25 Feb 2025 87,980.20 | 1 | $0.93 |
| short | 24 Feb 2025 91,552.90 | 2 Mar 2025 91,552.90 | 6 | −$0.01 |
| long | 2 Mar 2025 94,270.00 | 3 Mar 2025 87,820.10 | 1 | −$1.29 |
| long | 2 Mar 2025 94,270.00 | 3 Mar 2025 87,820.10 | 1 | −$1.29 |
| short | 9 Mar 2025 80,734.40 | 24 Mar 2025 88,037.90 | 15 | −$1.25 |
| short | 9 Mar 2025 80,734.40 | 24 Mar 2025 88,037.90 | 15 | −$1.25 |
| long | 21 Apr 2025 87,516.20 | 22 Apr 2025 91,073.50 | 1 | $0.90 |
| long | 21 Apr 2025 87,516.20 | 5 Jun 2025 101,509.00 | 45 | $3.56 |
| short | 5 Jun 2025 101,509.00 | 7 Jun 2025 105,836.00 | 2 | −$1.29 |
| short | 5 Jun 2025 101,509.00 | 7 Jun 2025 105,836.00 | 2 | −$1.29 |
| short | 30 Jun 2025 107,147.00 | 2 Jul 2025 108,850.00 | 2 | −$1.11 |
| long | 2 Jul 2025 108,850.00 | 9 Jul 2025 111,933.00 | 7 | $0.90 |
| long | 2 Jul 2025 108,850.00 | 1 Aug 2025 113,298.00 | 30 | $1.30 |
| short | 1 Aug 2025 113,298.00 | 7 Aug 2025 117,546.00 | 6 | −$1.36 |
| short | 1 Aug 2025 113,298.00 | 7 Aug 2025 117,546.00 | 6 | −$1.18 |
| long | 12 Aug 2025 120,134.00 | 13 Aug 2025 123,138.00 | 1 | $0.89 |
| long | 12 Aug 2025 120,134.00 | 14 Aug 2025 120,134.00 | 2 | −$0.02 |
| short | 19 Aug 2025 112,873.00 | 22 Aug 2025 117,226.00 | 3 | −$1.25 |
| short | 19 Aug 2025 112,873.00 | 22 Aug 2025 117,226.00 | 3 | −$1.25 |
| short | 14 Sep 2025 115,268.00 | 16 Sep 2025 116,789.00 | 2 | −$1.04 |
| long | 16 Sep 2025 116,789.00 | 22 Sep 2025 113,153.00 | 6 | −$1.21 |
| long | 16 Sep 2025 116,789.00 | 22 Sep 2025 113,153.00 | 6 | −$1.21 |
| short | 25 Sep 2025 108,994.00 | 29 Sep 2025 112,680.00 | 4 | −$1.18 |
| short | 25 Sep 2025 108,994.00 | 29 Sep 2025 112,680.00 | 4 | −$1.18 |
| long | 1 Oct 2025 118,595.00 | 3 Oct 2025 121,436.00 | 2 | $0.82 |
| long | 1 Oct 2025 118,595.00 | 10 Oct 2025 118,595.00 | 9 | −$0.02 |
| short | 10 Oct 2025 112,775.00 | 16 Oct 2025 108,053.00 | 6 | $0.84 |
| short | 10 Oct 2025 112,775.00 | 5 Jan 2026 93,859.70 | 87 | $3.40 |
| long | 5 Jan 2026 93,859.70 | 8 Jan 2026 90,119.80 | 3 | −$1.21 |
| long | 5 Jan 2026 93,859.70 | 8 Jan 2026 90,119.80 | 3 | −$1.21 |
| short | 20 Jan 2026 88,427.70 | 29 Jan 2026 85,644.30 | 9 | $0.85 |
| short | 20 Jan 2026 88,427.70 | 4 Mar 2026 72,666.80 | 43 | $4.91 |
| long | 4 Mar 2026 72,666.80 | 7 Mar 2026 67,178.10 | 3 | −$1.24 |
| long | 4 Mar 2026 72,666.80 | 7 Mar 2026 67,178.10 | 3 | −$1.24 |
| short | 27 Mar 2026 66,407.30 | 7 Apr 2026 70,819.80 | 11 | −$1.21 |
| short | 27 Mar 2026 66,407.30 | 7 Apr 2026 70,819.80 | 11 | −$1.21 |
| long | 10 Apr 2026 72,962.70 | 14 Apr 2026 75,821.80 | 4 | $0.86 |
| long | 10 Apr 2026 72,962.70 | 22 May 2026 75,539.50 | 42 | $0.78 |
| short | 22 May 2026 75,539.50 | 28 May 2026 73,288.90 | 6 | $0.87 |
| short | 22 May 2026 75,539.50 | 14 Jul 2026 65,044.00 | 53 | $4.14 |
| long | 14 Jul 2026 65,044.00 | 19 Aug 2026 67,347.60 | 36 | $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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Simple SuperTrend Strategy for BTCUSD 4H this page | — | v5 strategy | +1.1% | 72 | 19.5% |
| Dynamic Stop Loss Demo by Tindtily | highest lowest rsi atr | v5 strategy | does not run in Zeal Terminal | ||
| M0PB (Momentum Pullback) by Marcns_ | rsi highest lowest atr | v5 strategy | +51.3% | 3 | 17.5% |
| AlignedMA and Cumulative HighLow Strategy V2 by Trendoscope | atr supertrend highest lowest | v4 strategy | −40.8% | 16 | 50.9% |
| Delayed RSI Strategy by tweakerID | lowest highest atr rsi | v4 strategy | −40.4% | 7 | 42.2% |
| Aggresive Scalper-Swing Crypto Strategy by exlux99 | lowest highest atr | v4 strategy | −7.4% | 3 | 16.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.
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
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 108 ms | With a workaround 1,000 bars in 274 ms | Not measured |
| PineTS | Not tested | Not tested | Not 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)