Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

RSI+PA+DCA Strategy

A Pine Script v4 strategy built on RSI (price, 14), Valuewhen (co, close) and Change ("W"). It has 15 order rules, 7 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. 20 settings; defaults include Portfolio % 15, +++++ Long Positions VA Layers +++++ ++++ and 2nd Long Entry % 2.

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+DCA Strategy repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 14 of 14 trades.
How did RSI+PA+DCA Strategy do on BTCUSDT 1D with default settings?
+22.29%
From 26 Jan 2024 to 21 Oct 2026, over 46 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can RSI+PA+DCA Strategy be converted to Pine Script v5?
Yes
The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
Does RSI+PA+DCA Strategy use a stop loss?
No
Its exits set a profit target but no stop.
Does RSI+PA+DCA Strategy run in Zeal Terminal?
Yes
It compiles in 40 ms and runs 1,000 bars in 91 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
  • Average price
  • Take Profit All
  • Winning trade
  • Losing trade
  • Equity
  • Buy and hold
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (20)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$489$810Equity from $400
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots.
Net profit
+22.29%
$89.15
Buy and hold
+102.5%
same period
Closed trades
46
35 won · 11 lost
Profitable
76.1%
of closed trades
Profit factor
11.36
gross profit ÷ gross loss
Max drawdown
5.12%
−$20.58

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

    6 settings do nothing

    “+++++ Long Positions VA Layers +++++”, “+++++ Moving Average Filter +++++”, “+++++ RSI Inputs +++++”, “overbought, has no specific function”, “+++++ Take Profit +++++”, “+++++ Open DCA order once every week +++++” are declared but never read.

  • Check

    No stop loss

    strategy.exit is used without a stop, loss or trailing stop.

  • Note

    2 settings have no label

    In the settings dialog movingaverage_signal (line 32), length (line 36) show an empty name. The labels on this page come from the variable names.

  • Note

    No slippage

    Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.

  • Note

    Uses transp =, which v5 removed line 90

    transp = on line 90. v5 folds the transparency into the colour with color.new(c, transp).

Upgrade and export

Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.

  • Upgrading to v5 renames sma → ta.sma, rsi → ta.rsi, crossover → ta.crossover, valuewhen → ta.valuewhen, change → ta.change.

v4 → v5

Verified

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

  • Line 2: //@version=4 → //@version=5
  • Line 20: input(type = input.float) → input.float()
  • Line 32: sma → ta.sma
  • Line 40: rsi → ta.rsi
  • Line 42: crossover → ta.crossover
  • Line 48: valuewhen → ta.valuewhen
7 more edits
  • Line 49: valuewhen → ta.valuewhen
  • Line 50: valuewhen → ta.valuewhen
  • Line 51: valuewhen → ta.valuewhen
  • Line 52: valuewhen → ta.valuewhen
  • Line 53: valuewhen → ta.valuewhen
  • Line 85: change → ta.change
  • Line 90: transp = 50 → color.new(dcatime and newWeek ? color.white : na, 50)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=4+//@version=5// © A3Sh⋯strategy(title = "RSI+PA+DCA", 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(15, title = "Portfolio %", type = input.float, step = 0.1, minval = 0.1, maxval = 100)+port = input.float(15, title = "Portfolio %", step = 0.1, minval = 0.1, maxval = 100)q = (strategy.equity / 100 * port) / open⋯plotMA = input(title = "Plot Moving Average", defval = false)-movingaverage_signal = sma(close, input(100))+movingaverage_signal = ta.sma(close, input(100))plot (plotMA ? movingaverage_signal : na, color = color.green)⋯price = close-vrsi = rsi(price, length)+vrsi = ta.rsi(price, length)// Long trigger (co)-co = crossover(vrsi, overSold) and close < movingaverage_signal+co = ta.crossover(vrsi, overSold) and close < movingaverage_signal// Take profit⋯// Store values to create and plot the different PA layers-long1 = valuewhen(co, close, 0)+long1 = ta.valuewhen(co, close, 0)-long2 = valuewhen(co, close - (close / 100 * ps2), 0)+long2 = ta.valuewhen(co, close - (close / 100 * ps2), 0)-long3 = valuewhen(co, close - (close / 100 * ps3), 0)+long3 = ta.valuewhen(co, close - (close / 100 * ps3), 0)-long4 = valuewhen(co, close - (close / 100 * ps4), 0)+long4 = ta.valuewhen(co, close - (close / 100 * ps4), 0)-long5 = valuewhen(co, close - (close / 100 * ps5), 0)+long5 = ta.valuewhen(co, close - (close / 100 * ps5), 0)-long6 = valuewhen(co, close - (close / 100 * ps6), 0)+long6 = ta.valuewhen(co, close - (close / 100 * ps6), 0)eps1 = 0.00⋯dcaWeek = input("++++", title = "+++++ Open DCA order once every week +++++")-newWeek = change(time("W"))+newWeek = ta.change(time("W"))dcatime = input(title = "Buy a fixed amount every Monday", defval = false)⋯plotchar (dcatime ? newWeek : na, "buy at Week start", "▼", location.top, size = size.tiny, color = color.white)-bgcolor (dcatime and newWeek ? color.white : na, transp = 50)+bgcolor (color.new(dcatime and newWeek ? color.white : na, 50))// Submit Entry Orders⋯

Clean v5

VerifiedDownload the v5 script

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

  • Line 23: input() → input.string()
  • Line 24: input() → input.int()
  • Line 25: input() → input.int()
  • Line 26: input() → input.int()
  • Line 27: input() → input.int()
  • Line 28: input() → input.int()
13 more edits
  • Line 30: input() → input.string()
  • Line 31: input() → input.bool()
  • Line 32: input() → input.int()
  • Line 35: input() → input.string()
  • Line 36: input() → input.int()
  • Line 37: input() → input.int()
  • Line 38: input() → input.int()
  • Line 44: input() → input.string()
  • Line 45: input() → input.int()
  • Line 46: input() → input.int()
  • Line 84: input() → input.string()
  • Line 86: input() → input.bool()
  • Line 87: input() → input.int()
⋯// Long position entry layers. Percentage from the entry price of the the first long-PositionInputs = input("++++", title = "+++++ Long Positions VA Layers +++++")+PositionInputs = input.string("++++", title = "+++++ Long Positions VA Layers +++++")-ps2 = input(2,  title = "2nd Long Entry %", step = 0.1)+ps2 = input.int(2,  title = "2nd Long Entry %", step = 0.1)-ps3 = input(3,  title = "3rd Long Entry %", step = 0.1)+ps3 = input.int(3,  title = "3rd Long Entry %", step = 0.1)-ps4 = input(5,  title = "4th Long Entry %", step = 0.1)+ps4 = input.int(5,  title = "4th Long Entry %", step = 0.1)-ps5 = input(10, title = "5th Long Entry %", step = 0.1)+ps5 = input.int(10, title = "5th Long Entry %", step = 0.1)-ps6 = input(16, title = "6th Long Entry %", step = 0.1)+ps6 = input.int(16, title = "6th Long Entry %", step = 0.1)// Calculate Moving Averages-maInput = input("++++", title = "+++++ Moving Average Filter +++++")+maInput = input.string("++++", title = "+++++ Moving Average Filter +++++")-plotMA = input(title = "Plot Moving Average", defval = false)+plotMA = input.bool(title = "Plot Moving Average", defval = false)-movingaverage_signal = ta.sma(close, input(100))+movingaverage_signal = ta.sma(close, input.int(100))plot (plotMA ? movingaverage_signal : na, color = color.green)// RSI inputs and calculations-rsiInput = input( "++++", title = "+++++ RSI Inputs +++++" )+rsiInput = input.string( "++++", title = "+++++ RSI Inputs +++++" )-length =     input( 14 )+length =     input.int( 14 )-overSold =   input( 30, title = "oversold, entry trigger long position" )+overSold =   input.int( 30, title = "oversold, entry trigger long position" )-overBought = input( 70, title = "overbought, has no specific function")+overBought = input.int( 70, title = "overbought, has no specific function")price = close⋯// Take profit-takeprofit = input("++++", title = "+++++ Take Profit +++++")+takeprofit = input.string("++++", title = "+++++ Take Profit +++++")-ProfitTarget_Percent     = input(5, title = "Take Profit % (Per Position)")+ProfitTarget_Percent     = input.int(5, title = "Take Profit % (Per Position)")-ProfitTarget_Percent_All = input(7, title = "Take Profit % (Exit All, above % of price average, white line)")+ProfitTarget_Percent_All = input.int(7, title = "Take Profit % (Exit All, above % of price average, white line)")// Store values to create and plot the different PA layers⋯// Open DCA order once at the start of the week-dcaWeek = input("++++", title = "+++++ Open DCA order once every week +++++")+dcaWeek = input.string("++++", title = "+++++ Open DCA order once every week +++++")newWeek = ta.change(time("W"))-dcatime = input(title = "Buy a fixed amount every Monday", defval = false)+dcatime = input.bool(title = "Buy a fixed amount every Monday", defval = false)-fixedAmount = input(40, title = "Fixed amount currency for DCA orders", step = 0.1)+fixedAmount = input.int(40, title = "Fixed amount currency for DCA orders", step = 0.1)dcaq = fixedAmount / open⋯

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: 46 before, 46 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⋯

Settings

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

SettingDefaultType and rangeFeeds
Portfolio %15float · min 0.1 · max 100 · step 0.1q
+++++ Long Positions VA Layers +++++++++stringunused
2nd Long Entry %2int · step 0.1long2
3rd Long Entry %3int · step 0.1long3
4th Long Entry %5int · step 0.1long4
5th Long Entry %10int · step 0.1long5
6th Long Entry %16int · step 0.1long6
+++++ Moving Average Filter +++++++++stringunused
Plot Moving Averagefalseboolplot
Movingaverage signal (no label in the script)100intco plot
+++++ RSI Inputs +++++++++stringunused
Length (no label in the script)14intvrsi
oversold, entry trigger long position30intco
overbought, has no specific function70intunused
+++++ Take Profit +++++++++stringunused
Take Profit % (Per Position)5inttpl1 tpl2 tpl3 tpl4 tpl5 tpl6
Take Profit % (Exit All, above % of price average, white line)7inttpv
+++++ Open DCA order once every week +++++++++stringunused
Buy a fixed amount every Mondayfalseboolentry DCA plotchar bgcolor line 112
Fixed amount currency for DCA orders40int · step 0.1dcaq

What it calculates

  • RSI (rsi, ta.rsi in v5)price, Length (14)vrsi · line 40
  • Valuewhen (valuewhen, ta.valuewhen in v5)co, close, 0long1 · line 48
  • Valuewhen (valuewhen, ta.valuewhen in v5)co, close - (close / 100 * ps2), 0long2 · line 49
  • Valuewhen (valuewhen, ta.valuewhen in v5)co, close - (close / 100 * ps3), 0long3 · line 50
  • Valuewhen (valuewhen, ta.valuewhen in v5)co, close - (close / 100 * ps4), 0long4 · line 51
  • Valuewhen (valuewhen, ta.valuewhen in v5)co, close - (close / 100 * ps5), 0long5 · line 52
  • Valuewhen (valuewhen, ta.valuewhen in v5)co, close - (close / 100 * ps6), 0long6 · line 53
  • Change (change, ta.change in v5)time("W")newWeek · line 85

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 66 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.

Order rules

OrderFires whenBy defaultLine
Open Long1 long(co and strategy.opentrades == 0)On99
Open Long2 long(strategy.opentrades == 1)On101
Open Long3 long(strategy.opentrades == 2)On103
Open Long4 long(strategy.opentrades == 3)On105
Open Long5 long(strategy.opentrades == 4)On107
Open Long6 long(strategy.opentrades == 5)On109
Open DCA long(dcatime and newWeek and strategy.position_size > 0 and close < strategy.position_avg_price)On113
Exit Exit 1 limit(strategy.position_size > 0)On116
Exit Exit 2 limit(strategy.position_size > 0)On117
Exit Exit 3 limit(strategy.position_size > 0)On118
Exit Exit 4 limit(strategy.position_size > 0)On119
Exit Exit 5 limit(strategy.position_size > 0)On120
Exit Exit 6 limit(strategy.position_size > 0)On121
Exit Exit DCA limit(strategy.position_size > 0)On122
cancel_all longCloseOn127

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.

14/14trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
  • ClearFuture-bar referencesNo negative history offsets such as close[-1].
  • ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
  • ClearOrder 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+22.3%+102.5%4676%11.365.1%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+5.6%+5.5%1963%2.4313.4%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+30.5%+9.8%6386%11.9028.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+3.9%+8.4%1942%1.6316.9%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+41.2%+16.7%8083%8.1026.4%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+9.4%+21.5%3269%2.6918.3%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+10.1%+148.3%1995%305.272.5%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+6.4%+17.1%1675%39.723.5%8 May 2026 – 21 Oct 2026

Backtests are simulations on historical data and are not investment advice.

The trades

By market direction

At entry, last 90 barsTradesWonP&L
Rising market0—$0.00
Sideways market1267%$21.15
Falling market3479%$68.00

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

How bad could the drawdown be?

1.1%

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

0%1%
All 46 trades
SideEntryExitBarsP&L
long26 Jun 2024 61,806.001 Jul 2024 63,708.505$1.78
long26 Jun 2024 61,806.006 Jul 2024 58,456.8010−$0.03
long28 Jun 2024 60,569.906 Jul 2024 58,456.808−$2.14
long28 Jun 2024 60,569.9010 Jul 2024 58,527.9012−$0.03
long3 Jul 2024 59,951.8010 Jul 2024 58,527.907−$1.45
long4 Jul 2024 58,715.7010 Jul 2024 58,527.906−$0.01
long4 Jul 2024 58,715.7015 Jul 2024 61,755.6011$2.75
long5 Jul 2024 55,625.4015 Jul 2024 61,755.6010$0.22
long5 Jul 2024 55,625.4015 Jul 2024 61,755.6010$5.71
long8 Jul 2024 55,625.4015 Jul 2024 61,755.607$0.56
long8 Jul 2024 55,625.4015 Jul 2024 62,854.307$6.57
long11 Jul 2024 57,725.8015 Jul 2024 62,854.304$0.21
long11 Jul 2024 57,725.8015 Jul 2024 64,637.404$6.79
long7 Aug 2024 56,022.008 Aug 2024 57,658.301$1.74
long7 Aug 2024 56,022.008 Aug 2024 58,778.701$0.12
long8 Aug 2024 54,901.608 Aug 2024 58,778.700$4.26
long2 Mar 2025 86,064.503 Mar 2025 94,270.001$6.13
long19 Nov 2025 92,960.8028 Nov 2025 92,879.509−$0.16
long20 Nov 2025 91,101.6028 Nov 2025 92,879.508$0.04
long20 Nov 2025 91,101.603 Dec 2025 92,876.7013$1.10
long21 Nov 2025 86,637.203 Dec 2025 92,876.7012$0.33
long21 Nov 2025 86,637.204 Dec 2025 93,429.9013$4.28
long22 Nov 2025 85,129.404 Dec 2025 93,429.9012$0.44
long22 Nov 2025 85,129.409 Dec 2025 92,844.5017$5.13
long1 Dec 2025 88,312.809 Dec 2025 92,844.508$0.26
long1 Dec 2025 88,312.805 Jan 2026 92,889.3035$2.95
long5 Dec 2025 90,172.005 Jan 2026 92,889.3031$0.20
long5 Dec 2025 90,172.005 Jan 2026 94,748.5031$2.83
long5 Dec 2025 88,312.805 Jan 2026 94,748.5031$0.49
long5 Dec 2025 88,312.8013 Jan 2026 94,736.8039$4.14
long14 Dec 2025 88,312.8013 Jan 2026 94,736.8030$0.44
long14 Dec 2025 88,312.8013 Jan 2026 95,665.5030$4.86
long8 Jan 2026 90,172.0013 Jan 2026 95,665.505$0.13
long8 Jan 2026 90,172.0014 Jan 2026 97,527.506$5.08
long7 Feb 2026 70,580.3014 Feb 2026 70,493.907−$0.19
long7 Feb 2026 70,580.3025 Feb 2026 66,725.1018−$0.47
long8 Feb 2026 69,168.6025 Feb 2026 66,725.1017−$2.26
long8 Feb 2026 69,168.6028 Feb 2026 66,815.8020−$0.24
long9 Feb 2026 68,462.9028 Feb 2026 66,815.8019−$1.62
long9 Feb 2026 68,462.904 Mar 2026 71,879.8023$0.28
long11 Feb 2026 67,051.204 Mar 2026 71,879.8021$4.37
long11 Feb 2026 67,051.204 Mar 2026 72,462.3021$0.35
long24 Feb 2026 63,522.204 Mar 2026 72,462.308$8.14
long24 Feb 2026 63,522.204 Mar 2026 73,783.208$0.87
long28 Feb 2026 63,522.204 Mar 2026 73,783.204$10.41
long12 Jun 2026 63,626.0015 Jun 2026 66,913.303$3.78

Similar scripts

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

ScriptSharesTypeNetTradesMax drawdown
RSI+PA+DCA Strategy this page—v4 strategy+22.3%465.1%
Reinforced RSI - The Quant Science by thequantsciencersi valuewhenv5 strategy−3.9%73.9%
Tendency EMA + RSI [Alorse] by alorsersi valuewhenv5 strategy+4.3%41.9%
Fibonacci + RSI - Strategy by MohamedYAbdelazizrsi valuewhenv4 strategy+3.4%219.2%
Momentum Strategy (BTC-USDT; 30m) - STOCH RSI (with source code) by Drun30rsi valuewhenv4 strategy+13.3%1213.6%
Impulse Strategy Signals V2 by danieljordirsi valuewhenv5 strategyNo trades on this market

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 40 msYes 1,000 bars in 91 msNot measured
PineTSNo v5 and v6 only—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. PineTS runs Pine Script v5 and v6 only.

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=4// © A3Sh// RSI Strategy that buys the dips, works with Price Averaging and has a Dollar Cost Average option.// When the price drops below specified percentages of the price (6 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.// The idea is to lower the average position price to a point that when the market rises, the current price crosses over the average position price.// When the current price crosses the average position size and reaches the specified take profit, all entries are closed at once.// In case the market drops significantly, there is an option to activate DCA to lower the average price further.// RSI code 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// 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/// Buy every week code based on the following question in Stack Overflow// https://stackoverflow.com/questions/59870411/in-pine-script-how-can-you-do-something-once-per-day-or-keep-track-if-somethinstrategy(title = "RSI+PA+DCA", 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(15, title = "Portfolio %", type = input.float, step = 0.1, minval = 0.1, maxval = 100)q = (strategy.equity / 100 * port) / open// Long position entry layers. Percentage from the entry price of the the first longPositionInputs = input("++++", title = "+++++ Long Positions VA Layers +++++")ps2 = input(2,  title = "2nd Long Entry %", step = 0.1)ps3 = input(3,  title = "3rd Long Entry %", step = 0.1)ps4 = input(5,  title = "4th Long Entry %", step = 0.1)ps5 = input(10, title = "5th Long Entry %", step = 0.1)ps6 = input(16, title = "6th Long Entry %", step = 0.1)// Calculate Moving AveragesmaInput = input("++++", title = "+++++ Moving Average Filter +++++")plotMA = input(title = "Plot Moving Average", defval = false)movingaverage_signal = sma(close, input(100))plot (plotMA ? movingaverage_signal : na, color = color.green)// RSI inputs and calculationsrsiInput = input( "++++", title = "+++++ RSI Inputs +++++" )length =     input( 14 )overSold =   input( 30, title = "oversold, entry trigger long position" )overBought = input( 70, title = "overbought, has no specific function")price = closevrsi = rsi(price, length)// Long trigger (co)co = crossover(vrsi, overSold) and close < movingaverage_signal// Take profittakeprofit = input("++++", title = "+++++ Take Profit +++++")ProfitTarget_Percent     = input(5, title = "Take Profit % (Per Position)")ProfitTarget_Percent_All = input(7, title = "Take Profit % (Exit All, above % of price average, white line)")// Store values to create and plot the different PA layerslong1 = valuewhen(co, close, 0)long2 = valuewhen(co, close - (close / 100 * ps2), 0)long3 = valuewhen(co, close - (close / 100 * ps3), 0)long4 = valuewhen(co, close - (close / 100 * ps4), 0)long5 = valuewhen(co, close - (close / 100 * ps5), 0)long6 = valuewhen(co, close - (close / 100 * ps6), 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]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 position average priceplot (strategy.position_avg_price, title = "Average price", style = plot.style_linebr, color = color.white, linewidth = 2)// Take profit and exit all on take profit above position average price + plot a red take profit linetpv = strategy.position_avg_price + (strategy.position_avg_price / 100 * ProfitTarget_Percent_All)plot (tpv, title = "Take Profit All", style = plot.style_linebr, color = color.red, linewidth = 1)tpl1 = close < tpv ? eps1 + close * (ProfitTarget_Percent / 100) : tpvtpl2 = close < tpv ? eps2 + close * (ProfitTarget_Percent / 100) : tpvtpl3 = close < tpv ? eps3 + close * (ProfitTarget_Percent / 100) : tpvtpl4 = close < tpv ? eps4 + close * (ProfitTarget_Percent / 100) : tpvtpl5 = close < tpv ? eps5 + close * (ProfitTarget_Percent / 100) : tpvtpl6 = close < tpv ? eps6 + close * (ProfitTarget_Percent / 100) : tpv// Open DCA order once at the start of the weekdcaWeek = input("++++", title = "+++++ Open DCA order once every week +++++")newWeek = change(time("W"))dcatime = input(title = "Buy a fixed amount every Monday", defval = false)fixedAmount = input(40, title = "Fixed amount currency for DCA orders", step = 0.1)dcaq = fixedAmount / openplotchar (dcatime ? newWeek : na, "buy at Week start", "▼", location.top, size = size.tiny, color = color.white)bgcolor (dcatime and newWeek ? color.white : na, transp = 50)// Submit Entry Ordersif (co and strategy.opentrades == 0)    eps1 := long1    eps2 := long2    eps3 := long3    eps4 := long4    eps5 := long5    eps6 := long6    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)    // Submit Weekly DCA order, only when price is below position average price and when a position is openif (dcatime and newWeek and strategy.position_size > 0 and close < strategy.position_avg_price)     strategy.entry("DCA", strategy.long, dcaq)// 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 DCA", from_entry = "DCA", limit = tpv) // 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 : 0   if longClose    strategy.cancel_all()