Pine Script v5 · Strategy
LowFinder_PyraMider_V2
A Pine Script v5 strategy built on RMA (change_mtf, 0, len*mtf), EMA (vrsi, 26) and Crossover (cc, 0). It has 5 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 10% of equity and pays 0.06% commission. 35 settings; defaults include Precision 4, Portfolio % 100 and Leverage 1.
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 LowFinder_PyraMider_V2 repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 10 of 10 trades.
- How did LowFinder_PyraMider_V2 do on BTCUSDT 1D with default settings?
- +1.90%
- From 26 Jan 2024 to 21 Oct 2026, over 15 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does LowFinder_PyraMider_V2 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: 15 before, 15 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 LowFinder_PyraMider_V2 use a stop loss?
- No
- Its exits set a profit target but no stop.
- Does LowFinder_PyraMider_V2 run in Zeal Terminal?
- Yes
- It compiles in 57 ms and runs 1,000 bars in 154 ms, in the browser.
BTCUSDT · 1D · default settings
- Average Price
- Stop Level
- Take Profit Level
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.90%
- $189.69
- Buy and hold
- +102.5%
- same period
- Closed trades
- 15
- 13 won · 2 lost
- Profitable
- 86.7%
- of closed trades
- Profit factor
- 13.41
- gross profit ÷ gross loss
- Max drawdown
- 0.81%
- −$81.87
One run with the author's default settings, $10,000 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
2 settings have no label
In the settings dialog stopLoss (line 39), exit_mode (line 46) show an empty name. The labels on this page come from the variable names.
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 29 |
| End Time | timestamp("01 Aug 2030 20:00") | time | line 29 |
| Strtaegy Properties | |||
| PrecisionSpecifies the number of digits after the floating point of the script's displayed values. | 4 | int · min 0 · max 16 | line 87 entry L_1 |
| Risk - Portfolio | |||
| Portfolio % | 100 | float · step 1.0 | iq q |
| Leverage | 1 | int · min 1 | iq q |
| Risk - Order Size | |||
| Order Size ModeBase mode: the base quantiy for each sequential order. Multiply mode: each quantity is multiplied by order number | multiply | string · options ['base', 'multiply'] | entry_comment line 76 line 87 entry entry_id |
| Order Size Divider (Multiply Mode)Divide Multiply by this number to lower the sequential order sizes | 1 | int | nq line 87 |
| Fixed Order SizeUse with caution! Overrides all Risk calculations | false | bool | line 74 |
| . . Base Currency: | 1 | float | q |
| Risk - Stop Loss | |||
| StopLoss of | false | bool | close_all StopLoss Stop Loss line 184 |
| Stop Loss (no label in the script) | 1.5 | float · step 0.1 | loss_equity loss_avg_price |
| % of | equity | string · options ['avg_price', 'equity'] | stop_loss |
| Risk - Stop Level | |||
| Stop Level LengthLookback most recent highest high | 100 | int | stop_long1_level stop_long2_level stop_long3_level stop_long4_level |
| Deviatation % above Stop Level | 0.3 | float · step 0.1 | stop_long1_level stop_long2_level stop_long3_level |
| Risk - Take Profit | |||
| Take Profit/Trailing Stop | true | bool | exit_conditions plot_trailing_stop plot_tp_level close_all Exit All line 181 |
| ..........% | 1.0 | float · step 0.1 | exit_condition_2 tp_exit plot_trailing_stop plot_tp_level |
| Exit Crossover Take Profit and ..... | true | bool | close_all Exit All line 181 |
| Exit mode (no label in the script) | stoplevel | string · options ['close', 'stoplevel'] | ex_m |
| Take Profit % per OrderEach separate order exits when hit | 10.0 | float · step 0.1 | take_profit_price |
| Pyramiding Settings | |||
| Max Number of Orders | 12 | int | iq q entry entry_id line 79 line 167 |
| Next Order % below Avg. Price | 0.2 | float · step 0.1 | below_avg |
| Next Order after X candlesPrevents opening too much orders in a Row | 0 | int | entry entry_id line 167 |
| MTF LowFinder Settings | |||
| Lookback of RSI | 5 | int | vrsi |
| Higher TimeFrame Multiplier RSIMultiplies the current timeframe by specified value | 1 | int | vrsi |
| MA Length / Sensitivity | 26 | int | pp |
| First Order % below LowOpen % lower then the found low | 0.1 | float · step 0.1 | entry_price |
| Moving Average Filter | |||
| Moving Average Length | 100 | int | moving_average_signal |
| MTF Stochastic Filter | |||
| K | 14 | int · min 1 | k mtfK mtfD |
| D | 3 | int · min 1 | d |
| Smooth | 3 | int · min 1 | k |
| MTF Stoch Filter (above gets filtered) | 30 | int | buy_signal |
| Higher TimeFrame MultiplierMultiplies the current timeframe by specified value | 10 | int | mtfK mtfD |
| Plots | |||
| Plot Average Price | true | bool | Average Price |
| Plot Moving Average | false | bool | Moving Average |
| Plot Trailing Stop Level | false | bool | plot_trailing_stop |
What it calculates
- RMAmath.max(change_mtf, 0), len*mtf
up_mtf · line 92 - RMA-math.min(change_mtf, 0), len*mtf
down_mtf · line 93 - EMAvrsi, MA Length / Sensitivity (26)
pp · line 97 - Crossovercc, 0
lows · line 100 - SMA / Stochta.stoch(source, high, low, periodK * mtf), smoothK * mtf
k · line 106 - SMAk, periodD * mtf
d · line 107 - SMAclose, Moving Average Length (100)
moving_average_signal · line 114 - Highesthigh, Stop Level Length (100)
stop_long1_level · line 127 - Highesthigh, Stop Level Length (100)
stop_long2_level · line 128 - Highesthigh, Stop Level Length (100)
stop_long3_level · line 129 - Highesthigh, Stop Level Length (100)
stop_long4_level · line 130 - Crossoverclose, stop_long1_level
stop_long1 · line 132 - Crossoverclose, stop_long2_level
stop_long2 · line 133 - Crossunderclose, stop_long4_level
stop_long4 · line 134
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 80 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open L_1 long | ta.crossunder(close,entry_price) and window() and strategy.position_size == 0 | On | 162 |
| Open entry_id long | strategy.opentrades == i and i < posCount and order_after > oa_lookback | On | 170 |
| Exit exit_id limit | strategy.opentrades > 0 and window() | On | 178 |
| Close Exit All | exit_conditions or (tp_exit and tp_on and cond2_toggle) and window() | On | 182 |
| Close StopLoss | ta.crossunder(close,stop_loss) and sl_on and window() | On | 185 |
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 | +1.9% | +102.5% | 15 | 87% | 13.41 | 0.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.5% | +5.5% | 3 | 100% | — | 0.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +3.8% | +9.8% | 28 | 71% | 3.17 | 2.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.3% | +8.4% | 3 | 100% | 74.81 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +3.4% | +16.7% | 27 | 78% | 3.37 | 2.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.6% | +21.5% | 5 | 100% | 856.29 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +3.8% | +148.3% | 23 | 96% | 17.79 | 1.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +1.4% | +17.1% | 7 | 86% | 155.02 | 1.4% | 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 | 4 | 100% | $73.63 |
| Falling market | 11 | 82% | $116.06 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
0.2%
The 95th percentile of the worst drawdown when the same 15 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.1% to 0.2%; the tick is what actually happened, 0.1%. Order luck alone moves it this much.
All 15 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 20 Nov 2025 91,555.00 | 13 Jan 2026 95,959.60 | 54 | $5.15 |
| long | 24 Nov 2025 86,830.00 | 13 Jan 2026 95,959.60 | 50 | $12.63 |
| long | 24 Nov 2025 86,830.00 | 14 Jan 2026 97,404.80 | 51 | $12.56 |
| long | 17 Dec 2025 87,863.40 | 14 Jan 2026 97,404.80 | 28 | $24.52 |
| long | 17 Dec 2025 87,863.40 | 17 Mar 2026 75,476.50 | 90 | −$14.98 |
| long | 7 Feb 2026 70,580.30 | 17 Mar 2026 75,476.50 | 38 | $16.83 |
| long | 26 Feb 2026 67,988.00 | 17 Mar 2026 75,476.50 | 19 | $2.96 |
| long | 26 Feb 2026 67,988.00 | 13 Apr 2026 73,871.50 | 46 | $19.13 |
| long | 26 Feb 2026 67,988.00 | 17 Apr 2026 78,095.70 | 50 | $14.03 |
| long | 31 Mar 2026 66,797.40 | 17 Apr 2026 78,095.70 | 17 | $23.54 |
| long | 31 Mar 2026 66,797.40 | 21 Jul 2026 66,622.80 | 112 | −$0.31 |
| long | 27 Jun 2026 60,097.30 | 21 Jul 2026 66,622.80 | 24 | $16.12 |
| long | 27 Jun 2026 60,097.30 | 21 Jul 2026 66,785.80 | 24 | $7.93 |
| long | 30 Jun 2026 60,260.20 | 21 Jul 2026 66,785.80 | 21 | $27.73 |
| long | 30 Jun 2026 60,260.20 | 26 Aug 2026 78,539.10 | 57 | $21.83 |
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 |
|---|---|---|---|---|---|
| LowFinder_PyraMider_V2 this page | — | v5 strategy | +1.9% | 15 | 0.8% |
| PB Trend Scalper by PatrickGwynBuckley | ema rma stoch highest | v5 strategy | −16.9% | 10 | 68.3% |
| Bagheri IG Ether by Bagheri_IG | ema highest rma sma | v4 strategy | +0.0% | 3 | 0.0% |
| Bagheri IG Ether v2 by Bagheri_IG | ema highest rma sma | v4 strategy | −0.0% | 2 | 0.0% |
| Optimized Keltner Channels SL-TP Strategy for BTC by chanu_lev10k | sma ema rma | v4 strategy | −19.4% | 36 | 70.0% |
| Slight Swing Momentum Strategy. by fzj20020403 | sma highest ema | v5 strategy | +3.1% | 7 | 0.6% |
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: 15 before, 15 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// Strategy that finds potential lows in the price action and spreads the risk by entering multiple positions at these lows.⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 57 ms | With a workaround 1,000 bars in 154 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 (line 170) 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// Strategy that finds potential lows in the price action and spreads the risk by entering multiple positions at these lows.// The low is detected based on the difference between MTF RSI and EMA based RSI, Moving Average and MTF Stochastic indicators.// The size of each next position that is entered is multiplied by the sequential number of the position.// Each separate position can exit when a specified take profit is triggered and re-open when detecting a new potential low.// All positions are closed when the price action crosses over the dynamic blue stop level line.// Additionally a take profit / trailing stop and a stop loss can be activated to lower the risk.// This strategy combines open-source code developed by fellow Tradingview community members: // The Lowfinder code is developed by RafaelZioni// https://www.tradingview.com/script/GzKq2RVl-Low-finder/// Both the MTF RSI code and the MTF Stochastic code are adapted from the MTFindicators libary written by Peter_O// https://www.tradingview.com/script/UUVWSpXR-MTFindicators/// The Stop Level calculation is inspired by the syminfo-mintick tutorial on Kodify.net// https://kodify.net/tradingview/info/syminfo-mintick/strategy("LowFinder_PyraMider", overlay=true, pyramiding=99, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.06, slippage=1 )// Backtest Windowstart_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// Inputsprec = input.int (4, group = 'Strtaegy Properties', title = 'Precision', minval = 0, maxval = 16, tooltip = "Specifies the number of digits after the floating point of the script's displayed values.")portfolio_size = input.float (100, group = 'Risk - Portfolio', title = 'Portfolio %', step=1.0) / 100leverage = input.int (1, group = 'Risk - Portfolio', title = 'Leverage', minval = 1)q_mode = input.string ('multiply', group = 'Risk - Order Size', title = 'Order Size Mode', options = ['base', 'multiply'], tooltip = 'Base mode: the base quantiy for each sequential order. Multiply mode: each quantity is multiplied by order number')q_mode_m = input.int (1, group = 'Risk - Order Size', title = 'Order Size Divider (Multiply Mode)', tooltip = 'Divide Multiply by this number to lower the sequential order sizes')fixed_q = input.bool (false, group = 'Risk - Order Size', title = 'Fixed Order Size', inline = '01', tooltip = 'Use with caution! Overrides all Risk calculations')amount_q = input.float (1, group = 'Risk - Order Size', title = '. . Base Currency:', inline = '01')sl_on = input.bool (false, group = 'Risk - Stop Loss', title = 'StopLoss of', inline = '03')stopLoss = input.float (1.5, group = 'Risk - Stop Loss', title = '', step=0.1, inline = '03') / 100sl_mode = input.string ('equity', group = 'Risk - Stop Loss', title = '% of', options = ['avg_price', 'equity'], inline = '03')stop_len = input.int (100, group = 'Risk - Stop Level', title = 'Stop Level Length', tooltip = 'Lookback most recent highest high')stop_deviation = input.float (0.3, group = 'Risk - Stop Level', title = 'Deviatation % above Stop Level', step=0.1) / 100cond2_toggle = input.bool (true , group = 'Risk - Take Profit', title = 'Take Profit/Trailing Stop', inline = '04')tp_all = input.float (1.0, group = 'Risk - Take Profit', title = '..........%', step=0.1, inline = '04') / 100tp_on = input.bool (true, group = 'Risk - Take Profit', title = 'Exit Crossover Take Profit and .....', inline = '02')exit_mode = input.string ('stoplevel', group = 'Risk - Take Profit', title = '', options = ['close', 'stoplevel'], inline = '02')takeProfit = input.float (10.0, group = 'Risk - Take Profit', title = 'Take Profit % per Order', tooltip = 'Each separate order exits when hit', step=0.1)posCount = input.int (12, group = 'Pyramiding Settings', title = 'Max Number of Orders')next_entry = input.float (0.2, group = 'Pyramiding Settings', title = 'Next Order % below Avg. Price', step=0.1)oa_lookback = input.int (0, group = 'Pyramiding Settings', title = 'Next Order after X candles', tooltip = 'Prevents opening too much orders in a Row')len_rsi = input.int (5, group = 'MTF LowFinder Settings', title = 'Lookback of RSI')mtf_rsi = input.int (1, group = 'MTF LowFinder Settings', title = 'Higher TimeFrame Multiplier RSI', tooltip='Multiplies the current timeframe by specified value')ma_length = input.int (26, group = 'MTF LowFinder Settings', title = 'MA Length / Sensitivity')new_entry = input.float (0.1, group = 'MTF LowFinder Settings', title = 'First Order % below Low',step=0.1, tooltip = 'Open % lower then the found low')/100ma_signal = input.int (100, group = 'Moving Average Filter', title = 'Moving Average Length')periodK = input.int (14, group = 'MTF Stochastic Filter', title = 'K', minval=1)periodD = input.int (3, group = 'MTF Stochastic Filter', title = 'D', minval=1)smoothK = input.int (3, group = 'MTF Stochastic Filter', title = 'Smooth', minval=1)lower = input.int (30, group = 'MTF Stochastic Filter', title = 'MTF Stoch Filter (above gets filtered)')mtf_stoch = input.int (10, group = 'MTF Stochastic Filter', title = 'Higher TimeFrame Multiplier', tooltip='Multiplies the current timeframe by specified value')avg_on = input.bool (true, group = 'Plots', title = 'Plot Average Price')plot_ma = input.bool (false, group = 'Plots', title = 'Plot Moving Average')plot_ts = input.bool (false, group = 'Plots', title = 'Plot Trailing Stop Level')// variables //var entry_price = 0.0 // The entry price of the first entryvar iq = 0.0 // Inititial order quantity before risk calculationvar nq = 0.0 // Updated new quantity after the loopvar oq = 0.0 // Old quantity at the beginning or the loopvar q = 0.0 // Final calculated quantity used as base order sizevar int order_after = 0 // Candle counter. Prevent opening too much orders in a row// Order size calaculations // // Order size based on max amount of pyramiding orders or fixed by user input ///// Order size calculation based on 'base' mode or ' multiply' mode //if fixed_q q := amount_qelse if q_mode == 'multiply' iq := (math.abs(strategy.equity * portfolio_size / posCount) / open) * leverage oq := iq for i = 0 to posCount nq := oq + (iq * ( i/ q_mode_m + 1)) oq := nq q := (iq * posCount / oq) * iqelse q := (math.abs(strategy.equity * portfolio_size / posCount) / open) * leverage// Function to calcaulate final order size based on order size modes and round the result with 1 decimal //quantity_mode(index,string q_mode) => q_mode == 'base' ? math.round(q,prec) : q_mode == 'multiply' ? math.round(q * (index/q_mode_m + 1),prec) : na// LowFinder Calculations //// MTF RSI by Peter_O //rsi_mtf(float source, simple int mtf,simple int len) => change_mtf=source-source[mtf] up_mtf = ta.rma(math.max(change_mtf, 0), len*mtf) down_mtf = ta.rma(-math.min(change_mtf, 0), len*mtf) rsi_mtf = down_mtf == 0 ? 100 : up_mtf == 0 ? 0 : 100 - (100 / (1 + up_mtf / down_mtf))// Lowfinder by RafaelZioni //vrsi = rsi_mtf(close,mtf_rsi,len_rsi)pp=ta.ema(vrsi,ma_length)dd=(vrsi-pp)*5cc=(vrsi+dd+pp)/2lows=ta.crossover(cc,0) // MTF Stoch Calcualation // MTF Stoch adapted from Peter_O //stoch_mtfK(source, mtf, len) => k = ta.sma(ta.stoch(source, high, low, periodK * mtf), smoothK * mtf) stoch_mtfD(source, mtf, len) => k = ta.sma(ta.stoch(source, high, low, periodK * mtf), smoothK * mtf) d = ta.sma(k, periodD * mtf) mtfK = stoch_mtfK(close, mtf_stoch, periodK)mtfD = stoch_mtfD(close, mtf_stoch, periodK)// Open next position % below average position price //below_avg = close < (strategy.position_avg_price * (1 - (next_entry / 100)))// Moving Average Filter //moving_average_signal = ta.sma(close, ma_signal)plot (plot_ma ? moving_average_signal : na, title = 'Moving Average', color = color.rgb(154, 255, 72))// Buy Signal //buy_signal = lows and close < moving_average_signal and mtfK < lower// First Entry % Below lows //if buy_signal entry_price := close * (1 - new_entry)// Plot Average Price of Position//plot (avg_on ? strategy.position_avg_price : na, title = 'Average Price', style = plot.style_linebr, color = color.new(color.white,0), linewidth = 1)// Take profit per Open Order //take_profit_price = close * takeProfit / 100 / syminfo.mintick// Calculate different Stop Level conditions to exit All //// Stop Level Caculation //stop_long1_level = ta.highest (high, stop_len)[1] * (1 + stop_deviation)stop_long2_level = ta.highest (high, stop_len)[2] * (1 + stop_deviation)stop_long3_level = ta.highest (high, stop_len)[3] * (1 + stop_deviation)stop_long4_level = ta.highest (high, stop_len)[1] * (1 - 0.008) // Stop triggers //stop_long1 = ta.crossover(close,stop_long1_level)stop_long2 = ta.crossover(close,stop_long2_level)stop_long4 = ta.crossunder(close,stop_long4_level)// Exit Conditions, cond 1 only Stop Level, cond2 Trailing Stop option //exit_condition_1 = close < strategy.position_avg_price ? stop_long1 : close > strategy.position_avg_price ? stop_long2 : naexit_condition_2 = close < strategy.position_avg_price * (1 + tp_all) ? stop_long2 : close > strategy.position_avg_price * (1 + tp_all) ? stop_long4 : close < strategy.position_avg_price ? stop_long1 : na// Switch between conditions //exit_conditions = cond2_toggle ? exit_condition_2 : exit_condition_1// Exit when take profit //ex_m = exit_mode == 'close' ? close : stop_long2_leveltp_exit = ta.crossover(ex_m, strategy.position_avg_price * (1 + tp_all)) and close > strategy.position_avg_price * 1.002// Plot stoplevel, take profit level //plot_stop_level = strategy.position_size > 0 ? stop_long2_level : naplot_trailing_stop = cond2_toggle and plot_ts and strategy.position_size > 0 and stop_long4_level > strategy.position_avg_price * (1 + tp_all) ? stop_long4_level : naplot(plot_stop_level, title = 'Stop Level', style=plot.style_linebr, color = color.new(#41e3ff, 0), linewidth = 1)plot(plot_trailing_stop, title = 'Trailing Stop', style=plot.style_linebr, color = color.new(#4cfca4, 0), linewidth = 1)plot_tp_level = cond2_toggle and strategy.position_size > 0 ? strategy.position_avg_price * (1 + tp_all) : naplot(plot_tp_level, title = 'Take Profit Level', style=plot.style_linebr, color = color.new(#ff41df, 0), linewidth = 1)// Calculate Stop Loss based on equity and average price //loss_equity = ((strategy.position_size * strategy.position_avg_price) - (strategy.equity * stopLoss)) / strategy.position_sizeloss_avg_price = strategy.position_avg_price * (1 - stopLoss)stop_loss = sl_mode == 'avg_price' ? loss_avg_price : loss_equityplot(strategy.position_size > 0 and sl_on ? stop_loss : na, title = 'Stop Loss', color=color.new(color.red,0),style=plot.style_linebr, linewidth = 1)// Counter to prevent opening too much orders in a row. //if strategy.position_size > 0 order_after := order_after + 1// Enter first position //if ta.crossunder(close,entry_price) and window() and strategy.position_size == 0 strategy.entry('L_1', strategy.long, qty = math.round(q,prec), comment = '+' + str.tostring(math.round(q,prec)))// Enter Next Pyramiding Orders //if buy_signal and window() and strategy.position_size > 0 and below_avg for i = 1 to strategy.opentrades entry_comment = '+' + str.tostring((quantity_mode(i,q_mode))) // Comment with variable // if strategy.opentrades == i and i < posCount and order_after > oa_lookback entry_price := close entry_id = 'L_' + str.tostring(i + 1) strategy.entry(id = entry_id, direction=strategy.long, limit=entry_price, qty= quantity_mode(i,q_mode), comment = entry_comment) order_after := 0 // Reset counter to prevent opening to much orders in a row.// Exit per Order //if strategy.opentrades > 0 and window() for i = 0 to strategy.opentrades exit_comment = '-' + str.tostring(strategy.opentrades.size(i)) exit_from = 'L_' + str.tostring(i + 1) exit_id = 'Exit_' + str.tostring(i + 1) strategy.exit(id= exit_id, from_entry= exit_from, profit = take_profit_price, comment = exit_comment) // Exit All //if exit_conditions or (tp_exit and tp_on and cond2_toggle) and window() strategy.close_all('Exit All') entry_price := 0if ta.crossunder(close,stop_loss) and sl_on and window() strategy.close_all('StopLoss') entry_price := 0