Pine Script v5 · Strategy
RSI+PA+PrTP
A Pine Script v5 strategy built on RMA or Change (14) and Valuewhen (co, close). It has 17 order rules, 8 entries among them, and trades long only with the default settings. Exits use a profit target and no stop loss. Each trade commits 15% of equity and pays 0.075% commission. 18 settings; defaults include Portfolio % Used To Open The 8 Positions 12, 2nd Long Entry % 2 and 3rd Long Entry % 3.
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+PA+PrTP repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 60 of 60 trades.
- How did RSI+PA+PrTP do on BTCUSDT 1D with default settings?
- +20.14%
- From 26 Jan 2024 to 21 Oct 2026, over 97 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does RSI+PA+PrTP 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: 97 before, 97 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 RSI+PA+PrTP use a stop loss?
- No
- Its exits set a profit target but no stop.
- Does RSI+PA+PrTP run in Zeal Terminal?
- Yes
- It compiles in 59 ms and runs 1,000 bars in 130 ms, in the browser.
BTCUSDT · 1D · default settings
- Long entry 1
- Long entry 2
- Long entry 3
- Long entry 4
- Long entry 5
- Long entry 6
- Long entry 7
- Long entry 8
- Take Profit Limit
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +20.14%
- $80.56
- Buy and hold
- +102.5%
- same period
- Closed trades
- 97
- 70 won · 27 lost
- Profitable
- 72.2%
- of closed trades
- Profit factor
- 4.34
- gross profit ÷ gross loss
- Max drawdown
- 7.85%
- −$34.61
One run with the author's default settings, $400 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
strategy.exit is used without a stop, loss or trailing stop.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
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 |
|---|---|---|---|
| Risk | |||
| Portfolio % Used To Open The 8 Positions | 12 | float · min 0.1 · max 100 · step 0.1 | q |
| Long Position Entry Layers | |||
| 2nd Long Entry % | 2 | float · step 0.1 | long2 |
| 3rd Long Entry % | 3 | float · step 0.1 | long3 |
| 4th Long Entry % | 5 | float · step 0.1 | long4 |
| 5th Long Entry % | 10 | float · step 0.1 | long5 |
| 6th Long Entry % | 16 | float · step 0.1 | long6 |
| 7th Long Entry % | 25 | float · step 0.1 | long7 |
| 8th Long Entry % | 40 | float · step 0.1 | long8 |
| Moving Average Filter | |||
| Plot Moving Average | false | bool | plot |
| MA Length | 100 | int | co plot |
| RSI Settings | |||
| RSI Length | 14 | int · min 1 | up down |
| Source | close | source | up down |
| Oversold, Trigger to Enter First Position | 29 | int | co |
| Take Profit Settings | |||
| Take Profit % (Per Position) | 3.0 | float | endVal tpl1 tpl2 tpl3 tpl4 tpl5 tpl6 tpl7 tpl8 |
| Take Profit % (Exit All, Progressive Take Profit Limit | 4.0 | float | tpLimit tpn |
| Take Profit ProgressionProgression is defined by the position size. By default 12% of the start equity (portfolio) is used to open a position, see Risk. This same % percentage is used to calculate the profit amount that will be deducted from the Take Profit Limit. | 12 | float | endVal |
| New entries affect Take Profit limitThis option changes the behaviour of the Progressive Take Profit. When switchted on, the difference between the former and current original Take Profit is deducted from the Progressive Take Profit. When switchted off, the Progressive Take Profit is only affected by the profit deduction or each closed position. | true | bool | line 101 |
| Plot Average Price (FIFO) | false | bool | Average price |
What it calculates
- RMA / Changemath.max(ta.change(rsiSourceInput), 0), RSI Length (14)
up · line 41 - RMA / Change-math.min(ta.change(rsiSourceInput), 0), RSI Length (14)
down · line 42 - Valuewhenco, close, 0
long1 · line 48 - Valuewhenco, close - close / 100 * ps2, 0
long2 · line 49 - Valuewhenco, close - close / 100 * ps3, 0
long3 · line 50 - Valuewhenco, close - close / 100 * ps4, 0
long4 · line 51 - Valuewhenco, close - close / 100 * ps5, 0
long5 · line 52 - Valuewhenco, close - close / 100 * ps6, 0
long6 · line 53 - Valuewhenco, close - close / 100 * ps7, 0
long7 · line 54 - Valuewhenco, close - close / 100 * ps8, 0
long8 · line 55
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 78 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 Long1 long | co and strategy.opentrades == 0 | On | 133 |
| Open Long2 long | strategy.opentrades == 1 | On | 135 |
| Open Long3 long | strategy.opentrades == 2 | On | 137 |
| Open Long4 long | strategy.opentrades == 3 | On | 139 |
| Open Long5 long | strategy.opentrades == 4 | On | 141 |
| Open Long6 long | strategy.opentrades == 5 | On | 143 |
| Open Long7 long | strategy.opentrades == 6 | On | 145 |
| Open Long8 long | strategy.opentrades == 7 | On | 147 |
| Exit Exit 1 limit | strategy.position_size > 0 | On | 150 |
| Exit Exit 2 limit | strategy.position_size > 0 | On | 151 |
| Exit Exit 3 limit | strategy.position_size > 0 | On | 152 |
| Exit Exit 4 limit | strategy.position_size > 0 | On | 153 |
| Exit Exit 5 limit | strategy.position_size > 0 | On | 154 |
| Exit Exit 6 limit | strategy.position_size > 0 | On | 155 |
| Exit Exit 7 limit | strategy.position_size > 0 | On | 156 |
| Exit Exit 8 limit | strategy.position_size > 0 | On | 157 |
| cancel_all | longClose | On | 161 |
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 | +20.1% | +102.5% | 97 | 72% | 4.34 | 7.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +5.0% | +5.5% | 30 | 67% | 1.85 | 10.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +16.7% | +9.8% | 55 | 58% | 2.06 | 26.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +2.4% | +8.4% | 243 | 45% | 1.27 | 13.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +16.8% | +16.7% | 105 | 73% | 1.64 | 24.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +3.3% | +21.5% | 204 | 28% | 1.30 | 9.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +11.8% | +148.3% | 101 | 50% | 1.93 | 12.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +3.8% | +17.1% | 20 | 80% | 13.24 | 2.8% | 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 | 0 | — | $0.00 |
| Sideways market | 25 | 52% | $18.76 |
| Falling market | 72 | 79% | $61.80 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
2.7%
The 95th percentile of the worst drawdown when the same 97 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.7% to 2.7%; the tick is what actually happened, 3.6%. Order luck alone moves it this much.
All 97 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 26 Jun 2024 61,806.00 | 30 Jun 2024 62,399.50 | 4 | $0.39 |
| long | 26 Jun 2024 61,806.00 | 1 Jul 2024 63,689.20 | 5 | $0.01 |
| long | 28 Jun 2024 60,569.90 | 1 Jul 2024 63,689.20 | 3 | $2.39 |
| long | 7 Jul 2024 58,230.10 | 9 Jul 2024 58,184.70 | 2 | −$0.11 |
| long | 8 Jul 2024 55,857.80 | 9 Jul 2024 58,184.70 | 1 | $0.02 |
| long | 8 Jul 2024 55,857.80 | 10 Jul 2024 58,807.00 | 2 | $2.33 |
| long | 9 Jul 2024 56,483.20 | 10 Jul 2024 58,807.00 | 1 | $0.02 |
| long | 9 Jul 2024 56,483.20 | 11 Jul 2024 58,797.30 | 2 | $1.87 |
| long | 9 Jul 2024 56,483.20 | 14 Jul 2024 59,931.60 | 5 | $0.05 |
| long | 11 Jul 2024 57,065.50 | 14 Jul 2024 59,931.60 | 3 | $2.33 |
| long | 7 Aug 2024 56,022.00 | 8 Aug 2024 56,555.60 | 1 | $0.39 |
| long | 7 Aug 2024 56,022.00 | 8 Aug 2024 57,676.00 | 1 | $0.05 |
| long | 8 Aug 2024 54,901.60 | 8 Aug 2024 57,676.00 | 0 | $2.35 |
| long | 2 Mar 2025 86,064.50 | 3 Mar 2025 94,270.00 | 1 | $4.73 |
| long | 19 Nov 2025 92,960.80 | 27 Nov 2025 91,027.30 | 8 | −$1.12 |
| long | 20 Nov 2025 91,101.60 | 27 Nov 2025 91,027.30 | 7 | −$0.00 |
| long | 20 Nov 2025 91,101.60 | 28 Nov 2025 92,912.00 | 8 | $0.86 |
| long | 21 Nov 2025 86,637.20 | 28 Nov 2025 92,912.00 | 7 | $0.15 |
| long | 21 Nov 2025 86,637.20 | 2 Dec 2025 90,901.40 | 11 | $2.12 |
| long | 22 Nov 2025 85,129.40 | 2 Dec 2025 90,901.40 | 10 | $0.24 |
| long | 22 Nov 2025 85,129.40 | 3 Dec 2025 92,910.40 | 11 | $3.99 |
| long | 29 Nov 2025 90,172.00 | 3 Dec 2025 92,910.40 | 4 | $0.08 |
| long | 29 Nov 2025 90,172.00 | 3 Dec 2025 93,841.60 | 4 | $1.84 |
| long | 1 Dec 2025 88,312.80 | 3 Dec 2025 93,841.60 | 2 | $0.08 |
| long | 1 Dec 2025 88,312.80 | 7 Dec 2025 90,989.90 | 6 | $1.38 |
| long | 4 Dec 2025 91,101.60 | 7 Dec 2025 90,989.90 | 3 | −$0.01 |
| long | 4 Dec 2025 91,101.60 | 9 Dec 2025 92,891.00 | 5 | $0.85 |
| long | 5 Dec 2025 90,172.00 | 9 Dec 2025 92,891.00 | 4 | $0.09 |
| long | 5 Dec 2025 90,172.00 | 9 Dec 2025 93,820.60 | 4 | $1.81 |
| long | 5 Dec 2025 88,312.80 | 9 Dec 2025 93,820.60 | 4 | $0.26 |
| long | 5 Dec 2025 88,312.80 | 4 Jan 2026 91,031.60 | 30 | $1.40 |
| long | 11 Dec 2025 91,101.60 | 4 Jan 2026 91,031.60 | 24 | −$0.01 |
| long | 11 Dec 2025 91,101.60 | 5 Jan 2026 92,917.90 | 25 | $0.89 |
| long | 12 Dec 2025 90,172.00 | 5 Jan 2026 92,917.90 | 24 | $0.09 |
| long | 12 Dec 2025 90,172.00 | 5 Jan 2026 93,847.50 | 24 | $1.87 |
| long | 14 Dec 2025 88,312.80 | 5 Jan 2026 93,847.50 | 22 | $0.16 |
| long | 14 Dec 2025 88,312.80 | 13 Jan 2026 92,910.90 | 30 | $2.42 |
| long | 7 Jan 2026 91,101.60 | 13 Jan 2026 92,910.90 | 6 | $0.03 |
| long | 7 Jan 2026 91,101.60 | 13 Jan 2026 93,840.50 | 6 | $1.41 |
| long | 8 Jan 2026 90,172.00 | 13 Jan 2026 93,840.50 | 5 | $0.05 |
| long | 8 Jan 2026 90,172.00 | 13 Jan 2026 95,675.40 | 5 | $2.91 |
| long | 3 Feb 2026 78,738.60 | 12 Feb 2026 68,152.90 | 9 | −$7.35 |
| long | 4 Feb 2026 75,770.20 | 12 Feb 2026 68,152.90 | 8 | −$0.36 |
| long | 4 Feb 2026 75,770.20 | 13 Feb 2026 68,128.60 | 9 | −$4.82 |
| long | 5 Feb 2026 73,165.80 | 13 Feb 2026 68,128.60 | 8 | −$0.67 |
| long | 5 Feb 2026 73,165.80 | 20 Feb 2026 68,150.60 | 15 | −$2.86 |
| long | 6 Feb 2026 62,909.90 | 20 Feb 2026 68,150.60 | 14 | $0.95 |
| long | 6 Feb 2026 62,909.90 | 25 Feb 2026 68,062.20 | 19 | $2.50 |
| long | 7 Feb 2026 70,580.30 | 25 Feb 2026 68,062.20 | 18 | −$0.69 |
| long | 7 Feb 2026 70,580.30 | 1 Mar 2026 68,149.60 | 22 | −$1.44 |
| long | 11 Feb 2026 66,140.40 | 1 Mar 2026 68,149.60 | 18 | $0.37 |
| long | 11 Feb 2026 66,140.40 | 2 Mar 2026 68,113.70 | 19 | $1.02 |
| long | 13 Feb 2026 66,140.40 | 2 Mar 2026 68,113.70 | 17 | $0.41 |
| long | 13 Feb 2026 66,140.40 | 4 Mar 2026 72,873.90 | 19 | $3.54 |
| long | 18 Feb 2026 66,140.40 | 4 Mar 2026 72,873.90 | 14 | $1.96 |
| long | 18 Feb 2026 66,140.40 | 9 Mar 2026 68,119.60 | 19 | $0.84 |
| long | 23 Feb 2026 66,140.40 | 9 Mar 2026 68,119.60 | 14 | $0.59 |
| long | 23 Feb 2026 66,140.40 | 13 Mar 2026 72,981.00 | 18 | $2.97 |
| long | 27 Feb 2026 66,140.40 | 13 Mar 2026 72,981.00 | 14 | $2.37 |
| long | 27 Feb 2026 66,140.40 | 15 Mar 2026 73,001.10 | 16 | $2.75 |
| long | 2 Mar 2026 65,776.50 | 15 Mar 2026 73,001.10 | 13 | $2.55 |
| long | 2 Mar 2026 65,776.50 | 30 Mar 2026 68,120.80 | 28 | $0.90 |
| long | 5 Mar 2026 70,864.70 | 30 Mar 2026 68,120.80 | 25 | −$0.98 |
| long | 5 Mar 2026 70,864.70 | 31 Mar 2026 68,144.40 | 26 | −$1.27 |
| long | 8 Mar 2026 66,140.40 | 31 Mar 2026 68,144.40 | 23 | $0.66 |
| long | 8 Mar 2026 66,140.40 | 5 Apr 2026 68,159.40 | 28 | $0.80 |
| long | 14 Mar 2026 70,864.70 | 5 Apr 2026 68,159.40 | 22 | −$1.02 |
| long | 14 Mar 2026 70,864.70 | 9 Apr 2026 72,996.90 | 26 | $0.81 |
| long | 18 Mar 2026 70,864.70 | 9 Apr 2026 72,996.90 | 22 | $0.68 |
| long | 18 Mar 2026 70,864.70 | 13 Apr 2026 72,987.00 | 26 | $0.81 |
| long | 27 Mar 2026 66,140.40 | 13 Apr 2026 72,987.00 | 17 | $2.43 |
| long | 27 Mar 2026 66,140.40 | 17 Apr 2026 76,723.40 | 21 | $4.04 |
| long | 31 Mar 2026 66,140.40 | 17 Apr 2026 76,723.40 | 17 | $3.07 |
| long | 31 Mar 2026 66,140.40 | 19 Apr 2026 74,801.70 | 19 | $4.30 |
| long | 2 Apr 2026 66,140.40 | 19 Apr 2026 74,801.70 | 17 | $1.99 |
| long | 2 Apr 2026 66,140.40 | 20 Apr 2026 73,801.80 | 18 | $4.15 |
| long | 12 Apr 2026 70,864.70 | 20 Apr 2026 73,801.80 | 8 | $0.55 |
| long | 12 Apr 2026 70,864.70 | 22 Apr 2026 78,386.60 | 10 | $4.20 |
| long | 19 Apr 2026 74,801.70 | 22 Apr 2026 78,386.60 | 3 | $0.42 |
| long | 19 Apr 2026 74,801.70 | 22 Apr 2026 79,152.00 | 3 | $2.60 |
| long | 20 Apr 2026 73,801.80 | 22 Apr 2026 79,152.00 | 2 | $0.29 |
| long | 20 Apr 2026 73,801.80 | 23 Apr 2026 77,163.80 | 3 | $2.23 |
| long | 23 Apr 2026 77,163.80 | 23 Apr 2026 77,163.80 | 0 | −$0.01 |
| long | 23 Apr 2026 77,163.80 | 25 Apr 2026 77,163.80 | 2 | −$0.08 |
| long | 25 Apr 2026 77,163.80 | 25 Apr 2026 77,163.80 | 0 | −$0.01 |
| long | 25 Apr 2026 77,163.80 | 27 Apr 2026 77,163.80 | 2 | −$0.08 |
| long | 27 Apr 2026 77,163.80 | 27 Apr 2026 77,163.80 | 0 | −$0.01 |
| long | 27 Apr 2026 77,163.80 | 28 Apr 2026 77,163.80 | 1 | −$0.08 |
| long | 28 Apr 2026 77,163.80 | 28 Apr 2026 77,163.80 | 0 | −$0.00 |
| long | 28 Apr 2026 77,163.80 | 29 Apr 2026 76,342.80 | 1 | −$0.64 |
| long | 29 Apr 2026 76,342.80 | 29 Apr 2026 76,342.80 | 0 | −$0.01 |
| long | 29 Apr 2026 76,342.80 | 30 Apr 2026 75,780.00 | 1 | −$0.47 |
| long | 30 Apr 2026 75,780.00 | 30 Apr 2026 75,780.00 | 0 | −$0.01 |
| long | 30 Apr 2026 75,780.00 | 1 May 2026 76,346.60 | 1 | $0.31 |
| long | 1 May 2026 76,346.60 | 1 May 2026 76,346.60 | 0 | −$0.01 |
| long | 1 May 2026 76,346.60 | 4 May 2026 80,751.10 | 3 | $2.95 |
| long | 12 Jun 2026 63,626.00 | 14 Jun 2026 65,559.70 | 2 | $1.72 |
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+PA+PrTP this page | — | v5 strategy | +20.1% | 97 | 7.9% |
| HiLo Extension by Saravanan_Ragavan | valuewhen rma | v4 strategy | No trades on this market | ||
| ADX + RSI Strat by tweakerID | valuewhen rma | v4 strategy | −3.5% | 6 | 36.3% |
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.
- 1 setting uses the untyped input() (line 36). 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 97 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 36: input() → input.int()
⋯plotMA = input.bool(group = "Moving Average Filter", title='Plot Moving Average', defval=false)-movingaverage_signal = ta.sma(close, input(100, group = "Moving Average Filter", title='MA Length'))+movingaverage_signal = ta.sma(close, input.int(100, group = "Moving Average Filter", title='MA Length'))plot (plotMA ? movingaverage_signal : na, color = color.new (color.green, 0))⋯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: 97 before, 97 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.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=5+//@version=6// © A3Sh⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 59 ms | Yes 1,000 bars in 130 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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///@version=5// © A3Sh// RSI Strategy that buys the dips, uses Price Averaging and Pyramiding.// When the price drops below specified percentages of the price (8 PA layers), new entries are openend to average the price of the assets.// Open entries are closed by a specified take profit.// Entries can be reopened, after closing and consequently crossing a PA layer again.// This strategy is based on the RSI+PA+DCA strategy I created earlier. The difference is the way the Take Profit is calculated.// Instead of directly connecting the take profit limit to the decreasing average price level with an X percent above the average price, // the take profit is calculated for a part on the decreasing average price and for another part on the deduction // of the profits of the individual closed positions.// The Take Profit Limit drop less significant then the average price level and the full position only completely exits // when enough individual closed positions made up for the losses.// This makes it less risky and more conservative and great for a long term trading strategy// RSI code is adapted from the build in Relative Strength Index indicator// MA Filter and RSI concept adapted from the Optimized RSI Buy the Dips strategy, by Coinrule// https://www.tradingview.com/script/Pm1WAtyI-Optimized-RSI-Strategy-Buy-The-Dips-by-Coinrule/// Pyramiding entries code adapted from Pyramiding Entries on Early Trends startegy, by Coinrule// Pyramiding entries code adapted from Pyramiding Entries on Early Trends startegy, by Coinrule// https://www.tradingview.com/script/7NNJ0sXB-Pyramiding-Entries-On-Early-Trends-by-Coinrule/// Plot entry layers code adapted from HOWTO Plot Entry Price by vitvlkv// https://www.tradingview.com/script/bHTnipgY-HOWTO-Plot-Entry-Price/strategy(title='RSI+PA+PTP', pyramiding=16, overlay=true, initial_capital=400, default_qty_type=strategy.percent_of_equity, default_qty_value=15, commission_type=strategy.commission.percent, commission_value=0.075, close_entries_rule='FIFO')port = input.float(12, group = "Risk", title='Portfolio % Used To Open The 8 Positions', step=0.1, minval=0.1, maxval=100)q = strategy.equity / 100 * port / open// Long position PA entry layers. Percentage from the entry price of the the first longps2 = input.float(2, group = "Long Position Entry Layers", title='2nd Long Entry %', step=0.1)ps3 = input.float(3, group = "Long Position Entry Layers", title='3rd Long Entry %', step=0.1)ps4 = input.float(5, group = "Long Position Entry Layers", title='4th Long Entry %', step=0.1)ps5 = input.float(10, group = "Long Position Entry Layers", title='5th Long Entry %', step=0.1)ps6 = input.float(16, group = "Long Position Entry Layers", title='6th Long Entry %', step=0.1)ps7 = input.float(25, group = "Long Position Entry Layers" ,title='7th Long Entry %', step=0.1)ps8 = input.float(40, group = "Long Position Entry Layers", title='8th Long Entry %', step=0.1)// Calculate Moving AveragesplotMA = input.bool(group = "Moving Average Filter", title='Plot Moving Average', defval=false)movingaverage_signal = ta.sma(close, input(100, group = "Moving Average Filter", title='MA Length'))plot (plotMA ? movingaverage_signal : na, color = color.new (color.green, 0))// RSI inputs and calculationsrsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")rsiSourceInput = input.source(close, "Source", group="RSI Settings")up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))overSold = input.int(29, title="Oversold, Trigger to Enter First Position", group = "RSI Settings")// Long trigger (co)co = ta.crossover(rsi, overSold) and close < movingaverage_signal// Store values to create and plot the different PA layerslong1 = ta.valuewhen(co, close, 0)long2 = ta.valuewhen(co, close - close / 100 * ps2, 0)long3 = ta.valuewhen(co, close - close / 100 * ps3, 0)long4 = ta.valuewhen(co, close - close / 100 * ps4, 0)long5 = ta.valuewhen(co, close - close / 100 * ps5, 0)long6 = ta.valuewhen(co, close - close / 100 * ps6, 0)long7 = ta.valuewhen(co, close - close / 100 * ps7, 0)long8 = ta.valuewhen(co, close - close / 100 * ps8, 0)eps1 = 0.00eps1 := na(eps1[1]) ? na : eps1[1]eps2 = 0.00eps2 := na(eps2[1]) ? na : eps2[1]eps3 = 0.00eps3 := na(eps3[1]) ? na : eps3[1]eps4 = 0.00eps4 := na(eps4[1]) ? na : eps4[1]eps5 = 0.00eps5 := na(eps5[1]) ? na : eps5[1]eps6 = 0.00eps6 := na(eps6[1]) ? na : eps6[1]eps7 = 0.00eps7 := na(eps7[1]) ? na : eps7[1]eps8 = 0.00eps8 := na(eps8[1]) ? na : eps8[1]plot(strategy.position_size > 0 ? eps1 : na, title='Long entry 1', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps2 : na, title='Long entry 2', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps3 : na, title='Long entry 3', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps4 : na, title='Long entry 4', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps5 : na, title='Long entry 5', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps6 : na, title='Long entry 6', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps7 : na, title='Long entry 7', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps8 : na, title='Long entry 8', style=plot.style_linebr)// Take Profit SettingsProfitTarget_Percent = input.float(3.0, group = "Take Profit Settings", title='Take Profit % (Per Position)')ProfitTarget_Percent_All = input.float(4.0, group = "Take Profit Settings", title='Take Profit % (Exit All, Progressive Take Profit Limit')TakeProfitProgression = input.float(12, group = "Take Profit Settings", title='Take Profit Progression', tooltip = 'Progression is defined by the position size. By default 12% of the start equity (portfolio) is used to open a position, see Risk. This same % percentage is used to calculate the profit amount that will be deducted from the Take Profit Limit.')entryOn = input.bool (true, group = "Take Profit Settings", title='New entries affect Take Profit limit', tooltip = 'This option changes the behaviour of the Progressive Take Profit. When switchted on, the difference between the former and current original Take Profit is deducted from the Progressive Take Profit. When switchted off, the Progressive Take Profit is only affected by the profit deduction or each closed position.')avPricePlot = input.bool (false, group = "Take Profit Settings", title='Plot Average Price (FIFO)')// Original Take Profit LimittpLimit = strategy.position_avg_price + (strategy.position_avg_price / 100 * ProfitTarget_Percent_All) // Create variables to calculate the Take Profit Limit Progresssionvar endVal = 0.0 var startVal = 0.0// The value at the the start of the loop is the value of the end of the previous loopstartVal := endVal // Set variable to the original Take Profit Limit when the first position opens.if strategy.position_size > 0 and strategy.position_size[1] ==0 endVal := tpLimit // Everytime a specific position opens, the difference of the previous (original) Take Profit price and the current (original) Take Profit price will be deducted from the Progressive Take Profit Limit// This feature can be toggled on and off in the settings panel. By default it is toggled on.entryAmount = 0.0for i = 1 to strategy.opentrades entryAmount := i if entryOn and strategy.position_size > 0 and strategy.opentrades[1] == (entryAmount) and strategy.opentrades == (entryAmount + 1) endVal := startVal - (tpLimit[1] - tpLimit)// Everytime a specific position closes, the amount of profit from that specific position will be deducted from the Progressive Take Profit Limit.exitAmount = 0.0for id = 1 to strategy.opentrades exitAmount := id if strategy.opentrades[1] ==(exitAmount + 1) and strategy.opentrades == (exitAmount) endVal := startVal - (TakeProfitProgression / 100 * strategy.opentrades.entry_price (id - 1) / 100 * ProfitTarget_Percent )// The Final Take Profit Pricetpn = (strategy.position_avg_price + (strategy.position_avg_price / 100 * ProfitTarget_Percent_All)) - (strategy.position_avg_price + (strategy.position_avg_price / 100 * ProfitTarget_Percent_All) - endVal)plot (strategy.position_size > 0 ? tpn : na, title = "Take Profit Limit", color=color.new(color.red, 0), style = plot.style_linebr, linewidth = 1) // Plot position average price as referenceplot (avPricePlot ? strategy.position_avg_price : na, title= "Average price", color = color.new(color.white, 0), style = plot.style_linebr, linewidth = 1) // When to trigger the Take Profit per position or the Progressive Take Profittpl1 = close < tpn ? eps1 + close * (ProfitTarget_Percent / 100) : tpntpl2 = close < tpn ? eps2 + close * (ProfitTarget_Percent / 100) : tpntpl3 = close < tpn ? eps3 + close * (ProfitTarget_Percent / 100) : tpntpl4 = close < tpn ? eps4 + close * (ProfitTarget_Percent / 100) : tpntpl5 = close < tpn ? eps5 + close * (ProfitTarget_Percent / 100) : tpntpl6 = close < tpn ? eps6 + close * (ProfitTarget_Percent / 100) : tpntpl7 = close < tpn ? eps7 + close * (ProfitTarget_Percent / 100) : tpntpl8 = close < tpn ? eps8 + close * (ProfitTarget_Percent / 100) : tpn// Submit Entry Ordersif co and strategy.opentrades == 0 eps1 := long1 eps2 := long2 eps3 := long3 eps4 := long4 eps5 := long5 eps6 := long6 eps7 := long7 eps8 := long8 strategy.entry('Long1', strategy.long, q)if strategy.opentrades == 1 strategy.entry('Long2', strategy.long, q, limit=eps2)if strategy.opentrades == 2 strategy.entry('Long3', strategy.long, q, limit=eps3)if strategy.opentrades == 3 strategy.entry('Long4', strategy.long, q, limit=eps4)if strategy.opentrades == 4 strategy.entry('Long5', strategy.long, q, limit=eps5)if strategy.opentrades == 5 strategy.entry('Long6', strategy.long, q, limit=eps6)if strategy.opentrades == 6 strategy.entry('Long7', strategy.long, q, limit=eps7)if strategy.opentrades == 7 strategy.entry('Long8', strategy.long, q, limit=eps8)// Submit Exit ordersif strategy.position_size > 0 strategy.exit(id='Exit 1', from_entry='Long1', limit=tpl1) strategy.exit(id='Exit 2', from_entry='Long2', limit=tpl2) strategy.exit(id='Exit 3', from_entry='Long3', limit=tpl3) strategy.exit(id='Exit 4', from_entry='Long4', limit=tpl4) strategy.exit(id='Exit 5', from_entry='Long5', limit=tpl5) strategy.exit(id='Exit 6', from_entry='Long6', limit=tpl6) strategy.exit(id='Exit 7', from_entry='Long7', limit=tpl7) strategy.exit(id='Exit 8', from_entry='Long8', limit=tpl8)// Make sure that all open limit orders are canceled after exiting all the positions longClose = strategy.position_size[1] > 0 and strategy.position_size == 0 ? 1 : 0if longClose strategy.cancel_all()