Pine Script v5 · Strategy
ROC_PA_Strategy (A3Sh)
A Pine Script v5 strategy built on Valuewhen (rocCrossed, condforValue), Barssince (rocCrossed) and TR. It has 6 order rules, 4 entries among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays 0.075% commission. 19 settings; defaults include Portfolio Percentage 25, Leverage 3 and Percentage 3.5.
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 ROC_PA_Strategy (A3Sh) repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 36 of 36 trades.
- How did ROC_PA_Strategy (A3Sh) do on BTCUSDT 1D with default settings?
- +56.33%
- From 26 Jan 2024 to 21 Oct 2026, over 88 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does ROC_PA_Strategy (A3Sh) 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: 88 before, 24 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 ROC_PA_Strategy (A3Sh) use a stop loss?
- Yes
- strategy.exit sets a stop on line 148.
- Does ROC_PA_Strategy (A3Sh) run in Zeal Terminal?
- Yes
- It compiles in 49 ms and runs 1,000 bars in 96 ms, in the browser.
BTCUSDT · 1D · default settings
- take profit
- stopp loss
- roc threshold
- Long Entry Threshold
- Close Long Threshold
- Long 1 Layer
- Long 2 Layer
- Long 3 Layer
- Long 4 Layer
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +56.33%
- $563.31
- Buy and hold
- +102.5%
- same period
- Closed trades
- 88
- 46 won · 42 lost
- Profitable
- 52.3%
- of closed trades
- Profit factor
- 1.49
- gross profit ÷ gross loss
- Max drawdown
- 63.31%
- −$965.94
One run with the author's default settings, $1,000 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.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 148. The upgraded version below moves each call inside an if block.
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 Percentage | 25 | float · min 0.1 · max 200 · step 0.1 | c |
| Leverage | 3 | int · min 1 · max 100 | c |
| Take Profit | true | bool | closeCondition take profit |
| Percentage | 3.5 | float · min 0.1 · step 0.1 | tp |
| Stop Lossss | true | bool | stopLoss stopp loss |
| Percentage | 8.0 | float · min 0.1 · step 0.1 | sl |
| Price Averaging Layers | |||
| 2nd Layer Long Entry % | 3 | float · step 0.1 | long2 |
| 3rd Layer Long Entry % | 7 | float · step 0.1 | long3 |
| 4th Layer Long Entry % | 15 | float · step 0.1 | long4 |
| ROC Logic to OPEN Long Entry | |||
| Rate of Change bar lookback | 3 | int | roc |
| ROC Threshold % to Setup Long Entry | 2.2 | float · step 0.1 | roc |
| Price Drop Threshold % to OPEN Long Entry | 0.4 | float · step 0.1 | condforValue |
| Duration of Long Entry Threshold Line in bars | 6 | int | plotLineOpen endLineOpen |
| Min % of Price Drop to OPEN Long Entry | 0.4 | float · step 0.1 | minThres |
| ROC Logic to CLOSE Long Entry | |||
| Rate of Change bar lookback | 3 | int | roc_CL |
| ROC Threshold % to Setup Close Threshold | 0.4 | float · step 0.1 | roc_CL |
| Price Rise Threshold % to CLOSE Long Entry | 1.0 | float · step 0.1 | condforValue_CL |
| Duration of Entry Limit in bars | 3 | int | plotLineClose endLineClose |
| Activate Continues Close Limit Line | false | bool | whenCondition Close Long Threshold |
What it calculates
- ValuewhenrocCrossed, condforValue, 0
openValue · line 57 - BarssincerocCrossed
sincerocCrossed · line 59 - TR
return_1 · line 75 - ValuewhenrocCrossed_CL, condforValue_CL, 0
closeValue · line 100 - BarssincerocCrossed_CL
sincerocCrossed_CL · line 102 - ValuewhenopenLong, close, 0
long1 · line 114 - ValuewhenopenLong, close - close / 100 * ps2, 0
long2 · line 115 - ValuewhenopenLong, close - close / 100 * ps3, 0
long3 · line 116 - ValuewhenopenLong, close - close / 100 * ps4, 0
long4 · line 117
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 70 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 | openLong and strategy.opentrades == 0 | On | 140 |
| Open Long2 long | strategy.opentrades == 1 | On | 142 |
| Open Long3 long | strategy.opentrades == 2 | On | 144 |
| Open Long4 long | strategy.opentrades == 3 | On | 146 |
| Exit Exit stop and limit | whenCondition | On | 148 |
| cancel_all | longClose | On | 152 |
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 | +56.3% | +102.5% | 88 | 52% | 1.49 | 63.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +15.4% | +5.5% | 11 | 73% | 2.37 | 26.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +231.9% | +9.8% | 70 | 70% | 3.20 | 137.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −26.9% | +8.4% | 27 | 52% | 0.30 | 39.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +237.3% | +16.7% | 80 | 65% | 2.13 | 171.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −14.6% | +21.5% | 34 | 56% | 0.68 | 37.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +58.2% | +148.3% | 145 | 52% | 1.63 | 93.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −17.5% | +17.1% | 15 | 47% | 0.36 | 24.9% | 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 | 41 | 46% | $316.03 |
| Sideways market | 26 | 54% | −$41.24 |
| Falling market | 21 | 62% | $290.98 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
30.0%
The 95th percentile of the worst drawdown when the same 88 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 13.2% to 30.0%; the tick is what actually happened, 21.8%. Order luck alone moves it this much.
All 88 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 17 Mar 2024 65,300.60 | 18 Mar 2024 68,393.50 | 1 | $32.32 |
| long | 20 Mar 2024 61,937.40 | 21 Mar 2024 67,840.50 | 1 | $66.77 |
| long | 13 Apr 2024 67,116.50 | 17 Apr 2024 60,272.70 | 4 | −$81.88 |
| long | 14 Apr 2024 63,924.50 | 17 Apr 2024 60,272.70 | 3 | −$44.57 |
| long | 15 Apr 2024 62,418.40 | 17 Apr 2024 60,272.70 | 2 | −$28.87 |
| long | 1 May 2024 60,672.00 | 4 May 2024 62,882.00 | 3 | $23.50 |
| long | 2 May 2024 58,365.00 | 4 May 2024 62,882.00 | 2 | $50.02 |
| long | 12 Jun 2024 67,314.20 | 24 Jun 2024 60,995.90 | 12 | −$70.56 |
| long | 14 Jun 2024 65,294.80 | 24 Jun 2024 60,995.90 | 10 | −$48.77 |
| long | 24 Jun 2024 62,602.20 | 24 Jun 2024 60,995.90 | 0 | −$18.86 |
| long | 25 Jun 2024 60,293.30 | 30 Jun 2024 63,006.30 | 5 | $27.25 |
| long | 5 Jul 2024 57,050.00 | 14 Jul 2024 59,204.00 | 9 | $23.35 |
| long | 8 Jul 2024 55,338.50 | 14 Jul 2024 59,204.00 | 6 | $43.47 |
| long | 3 Aug 2024 61,498.30 | 5 Aug 2024 56,578.50 | 2 | −$56.09 |
| long | 4 Aug 2024 59,653.40 | 5 Aug 2024 56,578.50 | 1 | −$37.31 |
| long | 5 Aug 2024 57,193.40 | 6 Aug 2024 54,018.80 | 1 | −$35.51 |
| long | 5 Aug 2024 57,193.40 | 9 Aug 2024 61,686.00 | 4 | $1.76 |
| long | 6 Aug 2024 54,018.80 | 9 Aug 2024 61,686.00 | 3 | $82.63 |
| long | 7 Aug 2024 56,022.00 | 9 Aug 2024 61,686.00 | 2 | $66.91 |
| long | 2 Sep 2024 57,301.80 | 6 Sep 2024 52,717.60 | 4 | −$59.27 |
| long | 6 Sep 2024 55,582.80 | 6 Sep 2024 52,717.60 | 0 | −$37.42 |
| long | 2 Oct 2024 60,804.90 | 7 Oct 2024 63,105.70 | 5 | $23.40 |
| long | 5 Nov 2024 67,850.00 | 7 Nov 2024 75,572.00 | 2 | $76.91 |
| long | 27 Nov 2024 91,965.20 | 28 Nov 2024 95,863.10 | 1 | $30.43 |
| long | 20 Dec 2024 97,461.90 | 26 Dec 2024 99,429.60 | 6 | $14.01 |
| long | 22 Dec 2024 94,538.00 | 26 Dec 2024 99,429.60 | 4 | $37.49 |
| long | 31 Dec 2024 92,792.10 | 2 Jan 2025 96,097.10 | 2 | $27.52 |
| long | 26 Feb 2025 88,680.40 | 2 Mar 2025 87,418.60 | 4 | −$12.68 |
| long | 27 Feb 2025 84,250.10 | 2 Mar 2025 87,418.60 | 3 | $27.36 |
| long | 28 Feb 2025 82,472.80 | 2 Mar 2025 87,418.60 | 2 | $46.26 |
| long | 10 Mar 2025 80,734.50 | 12 Mar 2025 82,933.00 | 2 | $21.17 |
| long | 11 Mar 2025 78,312.30 | 12 Mar 2025 82,933.00 | 1 | $48.15 |
| long | 9 Apr 2025 76,322.40 | 10 Apr 2025 82,615.20 | 1 | $72.23 |
| long | 30 May 2025 105,590.00 | 9 Jun 2025 106,956.00 | 10 | $10.98 |
| long | 5 Jun 2025 102,422.00 | 9 Jun 2025 106,956.00 | 4 | $40.70 |
| long | 18 Jun 2025 104,551.00 | 25 Jun 2025 107,128.00 | 7 | $23.21 |
| long | 21 Jun 2025 101,415.00 | 25 Jun 2025 107,128.00 | 4 | $53.90 |
| long | 19 Aug 2025 116,227.00 | 11 Sep 2025 114,045.00 | 23 | −$21.66 |
| long | 20 Aug 2025 112,740.00 | 11 Sep 2025 114,045.00 | 22 | $10.33 |
| long | 29 Aug 2025 108,091.00 | 11 Sep 2025 114,045.00 | 13 | $54.98 |
| long | 26 Sep 2025 108,994.00 | 29 Sep 2025 113,472.00 | 3 | $42.24 |
| long | 11 Oct 2025 112,774.00 | 17 Oct 2025 103,753.00 | 6 | −$86.33 |
| long | 16 Oct 2025 109,391.00 | 17 Oct 2025 103,753.00 | 1 | −$57.99 |
| long | 17 Oct 2025 104,880.00 | 21 Oct 2025 110,532.00 | 4 | $53.26 |
| long | 17 Oct 2025 104,880.00 | 21 Oct 2025 110,532.00 | 4 | $1.10 |
| long | 18 Oct 2025 106,432.00 | 21 Oct 2025 110,532.00 | 3 | $38.20 |
| long | 31 Oct 2025 108,323.00 | 4 Nov 2025 99,657.00 | 4 | −$88.22 |
| long | 4 Nov 2025 105,073.00 | 4 Nov 2025 99,657.00 | 0 | −$55.14 |
| long | 14 Nov 2025 99,692.00 | 15 Jan 2026 96,951.80 | 62 | −$28.30 |
| long | 15 Nov 2025 94,594.00 | 15 Jan 2026 96,951.80 | 61 | $21.26 |
| long | 17 Nov 2025 92,713.60 | 15 Jan 2026 96,951.80 | 59 | $40.96 |
| long | 21 Nov 2025 84,738.20 | 15 Jan 2026 96,951.80 | 55 | $105.07 |
| long | 21 Jan 2026 88,427.70 | 30 Jan 2026 81,353.40 | 9 | −$85.70 |
| long | 29 Jan 2026 85,774.80 | 30 Jan 2026 81,353.40 | 1 | −$56.38 |
| long | 30 Jan 2026 82,237.70 | 31 Jan 2026 81,353.40 | 1 | −$11.88 |
| long | 31 Jan 2026 84,260.50 | 31 Jan 2026 81,353.40 | 0 | −$0.61 |
| long | 31 Jan 2026 84,260.50 | 1 Feb 2026 78,741.10 | 1 | −$62.62 |
| long | 31 Jan 2026 84,260.50 | 22 Sep 2026 86,620.00 | 234 | $1.56 |
| long | 1 Feb 2026 78,741.10 | 22 Sep 2026 86,620.00 | 233 | $85.30 |
| long | 1 Feb 2026 78,741.10 | 22 Sep 2026 86,620.00 | 233 | $0.78 |
| long | 2 Feb 2026 76,968.20 | 22 Sep 2026 86,620.00 | 232 | $111.49 |
| long | 3 Feb 2026 75,163.50 | 22 Sep 2026 86,620.00 | 231 | $6.80 |
| long | 3 Feb 2026 75,163.50 | 23 Sep 2026 85,774.80 | 232 | $123.79 |
| long | 23 Sep 2026 85,774.80 | 23 Sep 2026 85,774.80 | 0 | −$0.26 |
| long | 23 Sep 2026 85,774.80 | 24 Sep 2026 84,397.60 | 1 | −$17.76 |
| long | 24 Sep 2026 84,397.60 | 24 Sep 2026 84,397.60 | 0 | −$0.23 |
| long | 24 Sep 2026 84,397.60 | 25 Sep 2026 84,410.20 | 1 | −$1.34 |
| long | 25 Sep 2026 84,410.20 | 25 Sep 2026 84,410.20 | 0 | −$0.27 |
| long | 25 Sep 2026 84,410.20 | 26 Sep 2026 84,100.00 | 1 | −$5.15 |
| long | 26 Sep 2026 84,100.00 | 26 Sep 2026 84,100.00 | 0 | −$0.25 |
| long | 26 Sep 2026 84,100.00 | 27 Sep 2026 84,433.10 | 1 | $2.44 |
| long | 27 Sep 2026 84,433.10 | 27 Sep 2026 84,433.10 | 0 | −$0.27 |
| long | 27 Sep 2026 84,433.10 | 28 Sep 2026 84,472.00 | 1 | −$1.04 |
| long | 28 Sep 2026 84,472.00 | 28 Sep 2026 84,472.00 | 0 | −$0.25 |
| long | 28 Sep 2026 84,472.00 | 29 Sep 2026 83,500.00 | 1 | −$12.96 |
| long | 29 Sep 2026 83,500.00 | 29 Sep 2026 83,500.00 | 0 | −$0.24 |
| long | 29 Sep 2026 83,500.00 | 30 Sep 2026 83,663.70 | 1 | $0.45 |
| long | 30 Sep 2026 83,663.70 | 30 Sep 2026 83,663.70 | 0 | −$0.26 |
| long | 30 Sep 2026 83,663.70 | 1 Oct 2026 83,623.60 | 1 | −$1.95 |
| long | 1 Oct 2026 83,623.60 | 1 Oct 2026 83,623.60 | 0 | −$0.25 |
| long | 1 Oct 2026 83,623.60 | 2 Oct 2026 84,880.10 | 1 | $13.34 |
| long | 2 Oct 2026 84,880.10 | 2 Oct 2026 84,880.10 | 0 | −$0.28 |
| long | 2 Oct 2026 84,880.10 | 3 Oct 2026 84,518.00 | 1 | −$5.77 |
| long | 3 Oct 2026 84,518.00 | 3 Oct 2026 84,518.00 | 0 | −$0.24 |
| long | 3 Oct 2026 84,518.00 | 4 Oct 2026 84,753.60 | 1 | $1.28 |
| long | 4 Oct 2026 84,753.60 | 4 Oct 2026 84,753.60 | 0 | −$0.25 |
| long | 4 Oct 2026 84,753.60 | 6 Oct 2026 86,122.50 | 2 | $14.64 |
| long | 5 Oct 2026 85,774.80 | 6 Oct 2026 86,122.50 | 1 | $0.46 |
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 |
|---|---|---|---|---|---|
| ROC_PA_Strategy (A3Sh) this page | — | v5 strategy | +56.3% | 88 | 63.3% |
| Momentum Strategy (BTC-USDT; 30m) - STOCH RSI (with source code) by Drun30 | valuewhen barssince | v4 strategy | +13.3% | 12 | 13.6% |
| Twin Range Filter Algo by OrcChieftain | tr barssince | v4 strategy | +48.4% | 18 | 6.8% |
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 strategy call uses the when = argument (line 148). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 88 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 148: when = whenCondition → if block
⋯// Setup Limit Close / Take Profit / Stop Loss order -strategy.exit('Exit', stop=stopLoss, limit=closeCondition, when=whenCondition, comment='Close All')+if whenCondition+ strategy.exit('Exit', stop=stopLoss, limit=closeCondition, comment='Close All')// Make sure that all open limit orders are canceled after exiting all the positions ⋯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: 88 before, 24 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
-//@version=5+//@version=6// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 49 ms | Yes 1,000 bars in 96 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.
//@version=5// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © A3Sh// Rate of price change / Price averaging strategy //// When the price drops to a specified percentage, a Long Entry Threshold is setup.// The Long Entry Threshold is only active for a specified number of bars and will de-activate when not crossed. // When the price drops further and crosses the Entry Threshold with a minimum of a specified percentage, a Long Position is entered. // The same reverse logic used to close the Long Position.// Stop loss and take profit are active by default. With proper tweaking of the settings it is possible to de-activate SL and TP.// The strategy is inspired by the following strategies:// Price Change Scalping Strategy developed by Prosum Solutions, https://www.tradingview.com/script/ue7Uc3sN-Price-Change-Scalping-Strategy-v1-0-0/// Scalping Dips On Trend Strategy developed by Coinrule, https://www.tradingview.com/script/iHHO0PJA-Scalping-Dips-On-Trend-by-Coinrule/strategy(title='ROC_PA_Strategy_@A3Sh', overlay=true, pyramiding=4, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.075)// Portfolio & Leverage Example// credit: @RafaelZioni, https://www.tradingview.com/script/xGk5K4DE-BTC-15-min/ge(value, precision) => math.round(value * math.pow(10, precision)) / math.pow(10, precision)port = input.float(25, group='Risk', title='Portfolio Percentage', step=0.1, minval=0.1, maxval=200)leverage = input.int(3, group='Risk', title='Leverage', minval=1, maxval=100)c = ge(strategy.equity * leverage / open * (port / 100), 4)// Take Profittpa = input.bool(true, title='Take Profit', group='Risk', inline='Take Profit')tpp = input.float(3.5, title='Percentage', group='Risk', step=0.1, minval=0.1, inline='Take Profit')tp = strategy.position_avg_price + strategy.position_avg_price / 100 * tppplot(tpa and strategy.position_size > 0 ? tp : na, color=color.new(color.gray, 0), title='take profit', style=plot.style_linebr, linewidth=1)// Stop Losssla = input.bool(true, title='Stop Lossss ', group='Risk', inline='Stop Loss')slp = input.float(8.0, title='Percentage', group='Risk', step=0.1, minval=0.1, inline='Stop Loss')sl = strategy.position_avg_price - strategy.position_avg_price / 100 * slpplot(sla and strategy.position_size > 0 ? sl : na, color=color.new(color.red, 0), title='stopp loss', style=plot.style_linebr, linewidth=1)stopLoss = sla ? sl : na// Long position entry layers. Percentage from the entry price of the the first longps2 = input.float(3, group='Price Averaging Layers', title='2nd Layer Long Entry %', step=0.1)ps3 = input.float(7, group='Price Averaging Layers', title='3rd Layer Long Entry %', step=0.1)ps4 = input.float(15, group='Price Averaging Layers', title='4th Layer Long Entry %', step=0.1)// ROC_Trigger Logic to open Long PositionrocLookBack = input.int(3, group='ROC Logic to OPEN Long Entry', title='Rate of Change bar lookback')rocThreshold = input.float(2.2, group='ROC Logic to OPEN Long Entry', title='ROC Threshold % to Setup Long Entry', step=0.1)entryLimit = input.float(0.4, group='ROC Logic to OPEN Long Entry', title='Price Drop Threshold % to OPEN Long Entry', step=0.1)entryTime = input.int(6, group='ROC Logic to OPEN Long Entry', title='Duration of Long Entry Threshold Line in bars')minLimit = input.float(0.4, group='ROC Logic to OPEN Long Entry', title='Min % of Price Drop to OPEN Long Entry', step=0.1)//ROC calculation based to the price level of previous X barsroc = close[rocLookBack] - close / 100 * rocThresholdplot(roc, color=color.new(color.gray, 20), title='roc threshold', linewidth=1)rocT1 = open > roc and close < roc ? 1 : 0 // When the price CROSSES the Entry LimitrocT2 = open < roc and close < roc ? 1 : 0 // When the price is BELOW the Entry LimitrocTrigger = rocT1 or rocT2// Condition for Setting Up a Long Entry Thershold LinerocCrossed = falsevar SetUpLong = falseif rocTrigger and not SetUpLong rocCrossed := true SetUpLong := true SetUpLong// Defining the Value of the Long Entry ThersholdcondforValue = rocCrossed and (open - low) / (open / 100) > 0 or open < roc and close < roc ? low - close / 100 * entryLimit : roc - close / 100 * entryLimitopenValue = ta.valuewhen(rocCrossed, condforValue, 0)// Defining the length of the Long Entry Thershold in bars, specified with an input parametersincerocCrossed = ta.barssince(rocCrossed)plotLineOpen = sincerocCrossed <= entryTime ? openValue : naendLineOpen = sincerocCrossed == entryTime ? 1 : 0// Set the conditions back to false when the Entry Limit Threshold Line ends after specied number of barsif endLineOpen and SetUpLong rocCrossed := false SetUpLong := false SetUpLong// Set minimum percentage of price drop to open a Long Position.minThres = (open - close) / (open / 100) > minLimit ? 1 : 0// Open Long TriggeropenLong = ta.crossunder(close, plotLineOpen) and strategy.position_size == 0 and minThresplot(strategy.position_size == 0 ? plotLineOpen : na, title='Long Entry Threshold', color=color.new(color.yellow, 0), style=plot.style_linebr, linewidth=2)// Show vertical dashed line when long condition is triggered // credit: @midtownsk8rguy, https://www.tradingview.com/script/EmTkvfCM-vline-Function-for-Pine-Script-v4-0/vline(BarIndex, Color, LineStyle, LineWidth) => return_1 = line.new(BarIndex, low - ta.tr, BarIndex, high + ta.tr, xloc.bar_index, extend.both, Color, LineStyle, LineWidth) return_1if openLong vline(bar_index, color.blue, line.style_dashed, 1)// ROC_Trigger Logic to close Long PositionrocLookBackL = input.int(3, group='ROC Logic to CLOSE Long Entry', title='Rate of Change bar lookback')entryThresholdL = input.float(0.4, group='ROC Logic to CLOSE Long Entry', title='ROC Threshold % to Setup Close Threshold', step=0.1) // Percentage from close priceentryLimit_CL = input.float(1.0, group='ROC Logic to CLOSE Long Entry', title='Price Rise Threshold % to CLOSE Long Entry', step=0.1) // Percentage from roc thresholdentryTime_CL = input.int(3, group='ROC Logic to CLOSE Long Entry', title='Duration of Entry Limit in bars')changeLogic = input.bool(false, group='ROC Logic to CLOSE Long Entry', title='Activate Continues Close Limit Line')roc_CL = close[rocLookBackL] + close / 100 * entryThresholdL//plot(rocL, color=color.gray, linewidth=1, transp=20)rocT1_CL = open < roc_CL and close > roc_CL ? 1 : 0rocT2_CL = open > roc_CL and close > roc_CL ? 1 : 0rocTrigger_CL = rocT1_CL or rocT2_CL// Condition for Setting Up a Long CLOSE Thershold LinerocCrossed_CL = falsevar SetUpClose = falseif rocTrigger_CL and not SetUpClose // The trigger for condA occurs and the last condition set was condB. rocCrossed_CL := true SetUpClose := true SetUpClose// Defining the Value of the Long CLOSE ThersholdcondforValue_CL = rocCrossed_CL and (high - open) / (open / 100) > 0 or open > roc_CL and close > roc_CL ? high + close / 100 * entryLimit_CL : roc_CL + close / 100 * entryLimit_CLcloseValue = ta.valuewhen(rocCrossed_CL, condforValue_CL, 0)// Defining the length of the Long CLOSE Thershold in bars, specified with an input parametersincerocCrossed_CL = ta.barssince(rocCrossed_CL)plotLineClose = sincerocCrossed_CL <= entryTime_CL ? closeValue : naendLineClose = sincerocCrossed_CL == entryTime_CL ? 1 : 0// Set the conditions back to false when the CLOSE Limit Threshold Line ends after specied number of barsif endLineClose and SetUpClose rocCrossed_CL := false SetUpClose := false SetUpClose// ROC Close + Take Profit combinedcloseCondition = close < tp ? plotLineClose : tpa ? tp : plotLineCloseplot(changeLogic and strategy.position_size > 0 ? closeValue : strategy.position_size > 0 ? plotLineClose : na, color=color.new(color.white, 0), title='Close Long Threshold', style=plot.style_linebr, linewidth=2)// Store values to create and plot the different PA layerslong1 = ta.valuewhen(openLong, close, 0)long2 = ta.valuewhen(openLong, close - close / 100 * ps2, 0)long3 = ta.valuewhen(openLong, close - close / 100 * ps3, 0)long4 = ta.valuewhen(openLong, close - close / 100 * ps4, 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]plot(strategy.position_size > 0 ? eps1 : na, title='Long 1 Layer', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps2 : na, title='Long 2 Layer', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps3 : na, title='Long 3 Layer', style=plot.style_linebr)plot(strategy.position_size > 0 ? eps4 : na, title='Long 4 Layer', style=plot.style_linebr)// Option to choose between old and new logic// There was a flaw in the original script that caused positions to close even after the close limit line ended. // Now you can choose between a logic where the line is continues(the orginal script) and one where the close limit is active an X amount of bars(the inteded logic)whenCondition = changeLogic ? rocTrigger_CL and strategy.position_size > 0 : strategy.position_size > 0 and close > eps2// Ener Long Positionsif openLong and strategy.opentrades == 0 eps1 := long1 eps2 := long2 eps3 := long3 eps4 := long4 strategy.entry('Long1', strategy.long, c, comment='Enter Long1')if strategy.opentrades == 1 strategy.entry('Long2', strategy.long, c, limit=eps2, comment='Enter Long2')if strategy.opentrades == 2 strategy.entry('Long3', strategy.long, c, limit=eps3, comment='Enter Long3')if strategy.opentrades == 3 strategy.entry('Long4', strategy.long, c, limit=eps4, comment='Enter Long4')// Setup Limit Close / Take Profit / Stop Loss order strategy.exit('Exit', stop=stopLoss, limit=closeCondition, when=whenCondition, comment='Close All')// 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()