Pine Script v5 · Strategy
Simple_RSI+PA+DCA Strategy
A Pine Script v5 strategy built on SMA (close, 100) and RMA or Change (14). It has 7 order rules, 2 entries among them, and trades long only with the default settings. Exits use a profit target and no stop loss. Each trade commits 1% of equity and pays 0.075% commission. 13 settings; defaults include Take Profit % 3, Close All % 6 and Max Amount of DCA Layers 8.
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_RSI+PA+DCA Strategy repaint?
- Yes
- In a live replay, it did not match history on 19 of 20 trades, and it produced 67 trades that history does not have.
- How did Simple_RSI+PA+DCA Strategy do on BTCUSDT 1D with default settings?
- +9.57%
- From 26 Jan 2024 to 21 Oct 2026, over 71 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Simple_RSI+PA+DCA Strategy 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: 71 before, 71 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_RSI+PA+DCA Strategy use a stop loss?
- No
- Its exits set a profit target but no stop.
- Does Simple_RSI+PA+DCA Strategy run in Zeal Terminal?
- Yes
- It compiles in 57 ms and runs 1,000 bars in 138 ms, in the browser.
BTCUSDT · 1D · default settings
- SMA
- RMA
- RMA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +9.57%
- $47.84
- Buy and hold
- +102.5%
- same period
- Closed trades
- 71
- 46 won · 25 lost
- Profitable
- 64.8%
- of closed trades
- Profit factor
- 2.23
- gross profit ÷ gross loss
- Max drawdown
- 12.32%
- −$67.57
One run with the author's default settings, $500 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.
-
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.
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 |
|---|---|---|---|
| Backtest Window | |||
| Start Time | timestamp("01 April 2021 20:00") | time | line 25 |
| End Time | timestamp("01 Aug 2030 20:00") | time | line 25 |
| Risk | |||
| Take Profit % | 3 | float · step 0.1 | take_profit_price |
| Close All % | 6 | float · step 0.1 | stop_level |
| DCA Settings | |||
| Switch to linear DCA Layers | false | bool | dca_level_mode |
| Max Amount of DCA Layers | 8 | int | linesArray q base_number entry entry_id line 80 line 97 line 104 |
| Base % to calculate DCA layers | 2 | float · min 1.1 · step 0.5 | dca_level_calc |
| Lines Bar Lookback | 4999 | int · max 4999 | max_bar line 71 |
| Moving Average | |||
| Plot Moving Average | false | bool | plot |
| MA Length | 100 | int | movingaverage_signal |
| RSI Settings | |||
| RSI Length | 14 | int · min 1 | up down |
| Source | close | source | up down |
| Oversold, Trigger to Enter First Position | 29 | int | co |
What it calculates
- SMAclose, MA Length (100)
movingaverage_signal · line 52 - RMA / Changemath.max(ta.change(rsiSourceInput), 0), RSI Length (14)
up · line 55 - RMA / Change-math.min(ta.change(rsiSourceInput), 0), RSI Length (14)
down · line 56
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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open FE1 long | co and strategy.position_size == 0 and window() | On | 86 |
| Open entry_id long | strategy.opentrades == i and i < posCount | On | 100 |
| Exit Exit FE limit | strategy.opentrades > 0 and window() | On | 114 |
| Exit exit_id limit | strategy.opentrades > 0 and window() | On | 120 |
| Close | strategy.opentrades > 0 and ta.crossover(close,stop_level) and window() | On | 123 |
| cancel_all | strategy.position_size[1] > 0 and strategy.position_size == 0 | On | 126 |
| Close | strategy.position_size[1] > 0 and strategy.position_size == 0 | On | 127 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
The live replay did not match 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 | +9.6% | +102.5% | 71 | 65% | 2.23 | 12.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −1.1% | +5.5% | 17 | 47% | 0.78 | 12.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −6.3% | +9.8% | 54 | 56% | 0.58 | 16.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +1.3% | +8.4% | 22 | 55% | 1.32 | 13.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −6.5% | +16.7% | 28 | 46% | 0.40 | 14.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.7% | +21.5% | 23 | 52% | 1.18 | 7.2% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +2.3% | +148.3% | 43 | 67% | 1.39 | 12.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +4.5% | +17.1% | 22 | 77% | 7.92 | 1.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 | 0 | — | $0.00 |
| Sideways market | 13 | 100% | $17.34 |
| Falling market | 58 | 57% | $30.50 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.2%
The 95th percentile of the worst drawdown when the same 71 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.6% to 3.2%; the tick is what actually happened, 4.3%. Order luck alone moves it this much.
All 71 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 26 Jun 2024 61,806.00 | 30 Jun 2024 62,395.60 | 4 | $0.52 |
| long | 26 Jun 2024 61,806.00 | 1 Jul 2024 63,689.20 | 5 | $0.02 |
| long | 28 Jun 2024 60,566.00 | 1 Jul 2024 63,689.20 | 3 | $3.16 |
| long | 7 Jul 2024 58,230.10 | 8 Jul 2024 56,309.90 | 1 | −$2.19 |
| long | 7 Jul 2024 58,230.10 | 8 Jul 2024 57,564.20 | 1 | −$0.03 |
| long | 7 Jul 2024 57,061.70 | 8 Jul 2024 57,564.20 | 1 | $0.44 |
| long | 7 Jul 2024 57,061.70 | 10 Jul 2024 58,803.20 | 3 | $0.07 |
| long | 7 Jul 2024 55,862.70 | 10 Jul 2024 58,803.20 | 3 | $3.00 |
| long | 7 Jul 2024 55,862.70 | 14 Jul 2024 59,204.00 | 7 | $0.13 |
| long | 8 Jul 2024 54,608.50 | 14 Jul 2024 59,204.00 | 6 | $4.91 |
| long | 7 Aug 2024 56,022.00 | 7 Aug 2024 56,551.80 | 0 | $0.51 |
| long | 7 Aug 2024 56,022.00 | 7 Aug 2024 57,676.00 | 0 | $0.07 |
| long | 7 Aug 2024 54,897.70 | 7 Aug 2024 57,676.00 | 0 | $3.11 |
| long | 6 Sep 2024 53,743.90 | 9 Sep 2024 55,390.10 | 3 | $1.81 |
| long | 6 Sep 2024 53,743.90 | 9 Sep 2024 55,390.10 | 3 | $1.82 |
| long | 6 Sep 2024 53,743.90 | 9 Sep 2024 55,390.10 | 3 | $1.82 |
| long | 2 Mar 2025 86,064.50 | 2 Mar 2025 88,892.60 | 0 | $2.11 |
| long | 4 Mar 2025 84,339.20 | 4 Mar 2025 85,189.50 | 0 | $0.56 |
| long | 4 Mar 2025 84,339.20 | 4 Mar 2025 86,957.60 | 0 | $0.00 |
| long | 4 Mar 2025 84,339.20 | 4 Mar 2025 86,957.60 | 0 | $1.92 |
| long | 4 Mar 2025 82,571.10 | 4 Mar 2025 86,957.60 | 0 | $0.01 |
| long | 4 Mar 2025 82,571.10 | 4 Mar 2025 86,957.60 | 0 | $3.28 |
| long | 9 Mar 2025 84,339.20 | 10 Mar 2025 81,058.70 | 1 | −$2.61 |
| long | 9 Mar 2025 84,339.20 | 10 Mar 2025 83,088.00 | 1 | −$0.01 |
| long | 9 Mar 2025 84,339.20 | 10 Mar 2025 83,088.00 | 1 | −$1.04 |
| long | 9 Mar 2025 84,339.20 | 11 Mar 2025 81,058.70 | 2 | −$0.06 |
| long | 9 Mar 2025 82,571.10 | 11 Mar 2025 81,058.70 | 2 | −$1.25 |
| long | 9 Mar 2025 80,730.10 | 11 Mar 2025 81,058.70 | 2 | $0.01 |
| long | 9 Mar 2025 80,730.10 | 12 Mar 2025 83,240.60 | 3 | $1.75 |
| long | 10 Mar 2025 78,700.90 | 12 Mar 2025 83,240.60 | 2 | $0.54 |
| long | 10 Mar 2025 78,700.90 | 14 Mar 2025 83,163.60 | 4 | $2.80 |
| long | 10 Mar 2025 78,700.90 | 14 Mar 2025 83,163.60 | 4 | $0.75 |
| long | 10 Mar 2025 78,700.90 | 14 Mar 2025 84,929.00 | 4 | $3.92 |
| long | 12 Mar 2025 80,730.10 | 14 Mar 2025 84,929.00 | 2 | $0.51 |
| long | 12 Mar 2025 80,730.10 | 19 Mar 2025 85,052.50 | 7 | $3.07 |
| long | 13 Mar 2025 80,730.10 | 19 Mar 2025 85,052.50 | 6 | $0.34 |
| long | 13 Mar 2025 80,730.10 | 19 Mar 2025 85,052.50 | 6 | $3.09 |
| long | 16 Mar 2025 82,571.10 | 19 Mar 2025 85,052.50 | 3 | $1.91 |
| long | 19 Nov 2025 92,960.80 | 20 Nov 2025 91,935.10 | 1 | −$0.86 |
| long | 19 Nov 2025 92,960.80 | 21 Nov 2025 84,697.80 | 2 | −$0.06 |
| long | 19 Nov 2025 91,097.50 | 21 Nov 2025 84,697.80 | 2 | −$4.81 |
| long | 19 Nov 2025 89,188.40 | 21 Nov 2025 84,697.80 | 2 | −$0.16 |
| long | 19 Nov 2025 89,188.40 | 23 Nov 2025 87,240.00 | 4 | −$1.46 |
| long | 20 Nov 2025 87,202.40 | 23 Nov 2025 87,240.00 | 3 | −$0.01 |
| long | 20 Nov 2025 87,202.40 | 23 Nov 2025 87,623.00 | 3 | $0.19 |
| long | 21 Nov 2025 85,018.10 | 23 Nov 2025 87,623.00 | 2 | $0.19 |
| long | 21 Nov 2025 85,018.10 | 26 Nov 2025 89,823.50 | 5 | $3.10 |
| long | 21 Nov 2025 82,143.90 | 26 Nov 2025 89,823.50 | 5 | $0.58 |
| long | 21 Nov 2025 82,143.90 | 26 Nov 2025 89,823.50 | 5 | $5.29 |
| long | 21 Nov 2025 84,697.80 | 26 Nov 2025 89,823.50 | 5 | $3.90 |
| long | 23 Nov 2025 87,202.40 | 26 Nov 2025 89,823.50 | 3 | $2.01 |
| long | 3 Feb 2026 78,738.60 | 4 Feb 2026 76,127.90 | 1 | −$2.39 |
| long | 3 Feb 2026 78,738.60 | 6 Feb 2026 66,584.60 | 3 | −$0.32 |
| long | 3 Feb 2026 77,159.80 | 6 Feb 2026 66,584.60 | 3 | −$8.96 |
| long | 3 Feb 2026 77,159.80 | 6 Feb 2026 71,631.90 | 3 | −$0.23 |
| long | 3 Feb 2026 75,541.50 | 6 Feb 2026 71,631.90 | 3 | −$3.47 |
| long | 3 Feb 2026 75,541.50 | 8 Feb 2026 71,593.20 | 5 | −$0.05 |
| long | 3 Feb 2026 73,854.80 | 8 Feb 2026 71,593.20 | 5 | −$2.08 |
| long | 4 Feb 2026 71,990.50 | 8 Feb 2026 71,593.20 | 4 | −$0.10 |
| long | 4 Feb 2026 71,990.50 | 25 Feb 2026 66,619.10 | 21 | −$3.92 |
| long | 5 Feb 2026 69,514.50 | 25 Feb 2026 66,619.10 | 20 | −$0.77 |
| long | 5 Feb 2026 69,514.50 | 28 Feb 2026 66,706.50 | 23 | −$1.89 |
| long | 5 Feb 2026 64,697.30 | 28 Feb 2026 66,706.50 | 23 | $0.60 |
| long | 5 Feb 2026 64,697.30 | 4 Mar 2026 71,564.60 | 27 | $3.71 |
| long | 7 Feb 2026 69,514.50 | 4 Mar 2026 71,564.60 | 25 | $0.84 |
| long | 7 Feb 2026 69,514.50 | 4 Mar 2026 71,564.60 | 25 | $1.26 |
| long | 8 Feb 2026 71,593.20 | 4 Mar 2026 71,564.60 | 24 | −$0.13 |
| long | 9 Feb 2026 69,514.50 | 4 Mar 2026 71,564.60 | 23 | $1.90 |
| long | 23 Feb 2026 64,697.30 | 4 Mar 2026 71,564.60 | 9 | $6.58 |
| long | 28 Feb 2026 64,697.30 | 4 Mar 2026 71,564.60 | 4 | $6.53 |
| long | 12 Jun 2026 63,626.00 | 14 Jun 2026 65,559.80 | 2 | $2.07 |
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_RSI+PA+DCA Strategy this page | — | v5 strategy | +9.6% | 71 | 12.3% |
| Infiten Slope Strategy by spiritualhealer117 | sma rma | v4 strategy | +47.2% | 26 | 35.5% |
| DMI Modified Strategy by dman103 | sma rma | v4 strategy | +42.4% | 39 | 45.7% |
| Optimized Keltner Channels SL-TP Strategy for BTC by chanu_lev10k | sma rma | v4 strategy | −19.4% | 36 | 70.0% |
| volume rsi strategy by pro trader123 by VSlip | rma sma | v5 strategy | −0.0% | 20 | 0.0% |
| Flawless Victory Strategy - 15min BTC Machine Learning Strategy by Bunghole | rma sma | v4 strategy | +21.3% | 6 | 35.4% |
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.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
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: 71 before, 71 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.
⋯// © A3Sh-//@version=5+//@version=6// Study of a Simple RSI based, PA (priceaveraging) and DCA strategy that opens a new position everytime it hits a specified price level below the first entry.⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 57 ms | With a workaround 1,000 bars in 138 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
Zeal Terminal's engine currently reads an order side written as direction = strategy.long as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 86, 100) so the engine trades the way the script says.
“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///// © A3Sh//@version=5// Study of a Simple RSI based, PA (priceaveraging) and DCA strategy that opens a new position everytime it hits a specified price level below the first entry.// The first entry is opened when the specified rsi and moving average conditions are met. // The following DCA levels are calculated exponentially and set, starting with a specified % of price drop. // The disctance between the dca levels can be changed with the exponential scale.// Each position closes individually when it reaches a specified take profit.// The position can re-open again when it hits the price level again.// Each time a position is closed and reopened, the average price drops a little.// The position stays open until the first entry closes or when the price reaches the Stop level.// When the price reaches the Stop level, all positions will close at once.// The RSI and MA code for opening the entry is adapted from the Optimized RSI Buy the Dips strategy, by Coinrule.// This code is used for study purposes, but any other low/ dip finding indicator can be used.// https://www.tradingview.com/script/Pm1WAtyI-Optimized-RSI-Strategy-Buy-The-Dips-by-Coinrule/// Dynamic DCA layers are inspired by the Backtesting 3commas DCA Bot v2, by rouxam// This logic gives more flexibility because you can dyanically change the amount of dca entries.// https://www.tradingview.com/script/8d6Auyst-Backtesting-3commas-DCA-Bot-v2/// The use of for loops to (re)open and close different entries separately is based on the Simple_Pyramiding strategy.// https://www.tradingview.com/script/t6cNLqDN-Simple-Pyramiding/strategy('Simple_RSI+PA+DCA', overlay=true, pyramiding=99, initial_capital=500, calc_on_order_fills=true, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.075, close_entries_rule='FIFO')// Backtest Window //start_time = input.time(defval=timestamp("01 April 2021 20:00"), group = "Backtest Window", title="Start Time")end_time = input.time(defval=timestamp("01 Aug 2030 20:00"), group = "Backtest Window", title="End Time")window() => time >= start_time and time <= end_time// Inputs //takeProfit = input.float (3, group = 'Risk', title = 'Take Profit %', step=0.1)takeProfitAll = input.float (6, group = "Risk", title = 'Close All %', step=0.1)linear_mode = input.bool (false, group = 'DCA Settings', title = 'Switch to linear DCA Layers')posCount = input.int (8, group = 'DCA Settings', title = 'Max Amount of DCA Layers')increment = input.float (2, group = 'DCA Settings', title = 'Base % to calculate DCA layers', step=0.5, minval=1.1)bar_lookback = input.int (4999, group = 'DCA Settings', title = 'Lines Bar Lookback', maxval = 4999)plotMA = input.bool (false, group = 'Moving Average', title = 'Plot Moving Average')moving_average = input.int (100, group = 'Moving Average', title = 'MA Length' )rsiLengthInput = input.int (14, group = 'RSI Settings', title = "RSI Length", minval=1)rsiSourceInput = input.source (close, group = 'RSI Settings', title = 'Source')overSold = input.int (29, group = 'RSI Settings', title = 'Oversold, Trigger to Enter First Position')// variables //var open_position = true // true when there are open positionsvar entry_price = 0.0 // the entry price of the first entryvar dca_price = 0.0 // the price of the different dca layersvar int count = 0 // bar counter since first open positionvar int max_bar = 0 // max bar buffer variable for DCA lines, stop lines, average pricevar line dca_line = na // lines variable for creating dca lines// arrays //linesArray = array.new_float(posCount,na) // array to store different dca price levels for creating the dca lines// Create max bar buffer for DCA lines, Stop and average price lines ///max_bar := count >= bar_lookback ? bar_lookback : count// Order size based on first entry and amount of DCA layersq = (strategy.equity / posCount + 1) / open// Calculate Moving Averagesmovingaverage_signal = ta.sma(close ,moving_average)plot (plotMA ? movingaverage_signal : na, color = color.new(#f5ff35, 0))// RSI calculations //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))// Buy Signal (co)co = ta.crossover(rsi, overSold) and close < movingaverage_signal// Create a white line for average price, since the last opened position //average_price = line.new(x1 = bar_index - max_bar, y1 = strategy.position_avg_price, x2 = bar_index, y2 = strategy.position_avg_price, color = color.white) // Stop //// Create a red Stop level line based on a specified % above the average price //stop_level = strategy.position_avg_price + (strategy.position_avg_price / 100 * takeProfitAll)stop_line = line.new(x1 = bar_index - max_bar, y1 = stop_level, x2 = bar_index, y2 = stop_level, color = color.red) // Take profit definition per open position //take_profit_price = close * takeProfit / 100 / syminfo.mintick// Make sure the Stop level and average price level don't excied the bar buffer to avoid errors //if count <= bar_lookback line.set_x1(stop_line, strategy.opentrades.entry_bar_index(strategy.opentrades - 1)) line.set_x1(average_price, strategy.opentrades.entry_bar_index(strategy.opentrades - 1))// Exponential DCA Layer Calculation fucntion --> New caclulation, the base number is calculated based the range between the entry_price and 0 and the amount of DCA layers //dca_exponent_level(index) => base_number = math.pow(entry_price, (1 / posCount)) dca_level_calc = (entry_price - (math.pow(base_number,index))) * (1 - ((math.pow(1 + (increment / 100),index)) - 1))// Linear DCA Layer Calculation // Added option to switchdca_linear_level(index) => entry_price - ((entry_price / posCount) * index)// Set Entries //// Open the first entry and set the entry price //if co and strategy.position_size == 0 and window() open_position := true entry_price := close strategy.entry(id = 'FE1', direction = strategy.long, qty = q) first_entry_line = line.new(x1 = bar_index - max_bar, y1 = entry_price, x2 = bar_index, y2 = entry_price, color = color.blue)// Start bar counting since the position is open //if open_position == true count := count + 1// Set the DCA entries //// Prices below 1 are not set to avoid negative prices //if strategy.position_size > 0 and window() for i = 0 to strategy.opentrades dca_level_mode = linear_mode ? dca_linear_level(i) : dca_exponent_level(i) if strategy.opentrades == i and i < posCount dca_price := dca_level_mode entry_id = 'DCA' + str.tostring(i + 1) strategy.entry(id = entry_id, direction = strategy.long, limit = dca_price, qty = q) // Store the values of the different dca price levels in an array and create the dca lines // // Prices below 1 are not stored//if open_position==true and window() for i = 1 to posCount -1 dca_level_mode = linear_mode ? dca_linear_level(i) : dca_exponent_level(i) array.push(linesArray, dca_level_mode) for i = 1 to array.size(linesArray) - 1 dca_line := line.new(x1 = bar_index - max_bar, y1 = array.get(linesArray, i), x2 = bar_index, y2 = array.get(linesArray, i),color = color.blue)// Create thick line to show the last Entry price //last_entry_price = line.new(x1 = bar_index[5], y1 = strategy.opentrades.entry_price(strategy.opentrades - 1), x2 = bar_index, y2 = strategy.opentrades.entry_price(strategy.opentrades - 1),color = color.rgb(255, 0, 204), width = 5)// Exit the first entry when the take profit triggered // if strategy.opentrades > 0 and window() strategy.exit(id = 'Exit FE', from_entry = 'FE1', profit = take_profit_price)// Exit DCA entries when take profit is triggered //if strategy.opentrades > 0 and window() for i = 1 to strategy.opentrades exit_from = 'DCA' + str.tostring(i + 1) exit_id = 'Exit_' + str.tostring(i + 1) strategy.exit(id = exit_id, from_entry = exit_from, profit = take_profit_price)// Close all positions at once when Stop is crossed //if strategy.opentrades > 0 and ta.crossover(close,stop_level) and window() strategy.close_all()// Make sure nothing is open after alle positions are closed and set the condiftion back to be open for new entries //if strategy.position_size[1] > 0 and strategy.position_size == 0 strategy.cancel_all() strategy.close_all() line.delete(average_price) line.delete(stop_line) line.delete(dca_line) open_position := false // All position are closed, so back to false count := 0 // Reset bar counter