Pine Script v5 · Strategy
Impulse Strategy Signals V2
A Pine Script v5 strategy built on SMA (src, len), RSI (close, 14) and ATR (14). It has 10 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays no commission. 33 settings; defaults include SMMA Length 1 21, SMMA Length 2 50 and SMMA Length 3 200.
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 Impulse Strategy Signals V2 repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Impulse Strategy Signals V2 do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. On BTCUSDT 4h it did trade; that row of the markets table below has its figures. A backtest, not investment advice.
- What alerts does Impulse Strategy Signals V2 have?
- 4
- longCond_txt; shortCond_txt; long_sltp_txt; short_sltp_txt.
- Does Impulse Strategy Signals 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: 12 before, 8 after on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Impulse Strategy Signals V2 use a stop loss?
- Yes
- strategy.exit sets a stop on line 144, 145, 146, 147, 148, 149, 150, 151.
BTCUSDT · 1D · default settings
- MA1
- MA2
- MA3
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
One run with the author's default settings, $100,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 commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
6 settings have no label
In the settings dialog i_sl_sw (line 48), i_tsl_sw (line 49), i_tp1_sw (line 52), i_tp2_sw (line 53), i_tp3_sw (line 54), i_tp4_sw (line 55) 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 an argument v6 removed
when = on line 144, 145, 146, 147, 148, 149, 150, 151. 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 |
|---|---|---|---|
| SMMA | |||
| SMMA Length 1 | 21 | int | ma1 |
| SMMA Length 2 | 50 | int | ma2 |
| SMMA Length 3 | 200 | int | ma3 |
| RSI | |||
| RSI Source | close | source | rsi |
| RSI Length | 14 | int | rsi |
| LONG: RSI Entry Threshold | 50.0 | float | long0 |
| SHORT: RSI Entry Threshold | 50.0 | float | short0 |
| Trading Session 1 | 0700-1600 | session | t1 |
| Trading Session 2 | 1200-2100 | session | t2 |
| Backtesting Start Time | timestamp("01 Sep 2002 13:30 +0000") | time | timeCond |
| Backtesting End Time | timestamp("30 Sep 2099 19:30 +0000") | time | timeCond |
| Show SL/TP Lines on Chart | true | bool | TP Long 1 TP Long 2 TP Long 3 TP Long 4 TP Short 1 TP Short 2 TP Short 3 TP Short 4 SL Long SL Short |
| strategy settings | |||
| ATR Length | 14 | int | atr |
| I sl sw (no label in the script) | ATR | string · options ["%", "ATR"] | sl sl_long0 sl_short0 |
| I tsl sw (no label in the script) | ATR | string · options ["ATR", "%"] | tsl trail_long_final trail_short_final |
| ● Stop Loss | 2.0 | float · min 0 · step 0.1 | sl |
| ● Trailing SL | 0.0 | float · min 0 · step 0.1 | tsl |
| I tp1 sw (no label in the script) | % | string · options ["%", "ATR", "R:R"] | tp1 tp_long1 tp_short1 |
| I tp2 sw (no label in the script) | % | string · options ["%", "ATR", "R:R"] | tp2 tp_long2 tp_short2 |
| I tp3 sw (no label in the script) | % | string · options ["%", "ATR", "R:R"] | tp3 tp_long3 tp_short3 |
| I tp4 sw (no label in the script) | % | string · options ["%", "ATR", "R:R"] | tp4 tp_long4 tp_short4 |
| TP 1 | 1.0 | float · min 0 · step 0.1 | tp1 last_tp_long last_tp_short |
| TP 2 | 2.0 | float · min 0 · step 0.1 | tp2 last_tp_long last_tp_short |
| TP 3 | 3.0 | float · min 0 · step 0.1 | tp3 last_tp_long last_tp_short |
| TP 4 | 4.0 | float · min 0 · step 0.1 | tp4 last_tp_long last_tp_short |
| QTY% | 10.0 | float | perc_tp1 perc_tp2 perc_tp3 perc_tp4 |
| QTY% | 10.0 | float | perc_tp2 perc_tp3 perc_tp4 |
| QTY% | 30.0 | float | perc_tp3 perc_tp4 |
| QTY% | 100.0 | float | perc_tp4 |
| Alert Message | |||
| Alert Msg: LONG Entry | | string | alert |
| Alert Msg: SHORT Entry | | string | alert |
| Alert Msg: LONG SL/TP | | string | alert |
| Alert Msg: SHORT SL/TP | | string | alert |
What it calculates
- SMAsrc, len
smma · line 10 - RSIRSI Source (close), RSI Length (14)
rsi · line 21 - ATRATR Length (14)
atr · line 47 - ValuewhenlongCond, close, 0
long_entry · line 74 - ValuewhenshortCond, close, 0
short_entry · line 75 - ValuewhenlongCond, close -atr * sl, 0
sl_long_atr · line 89 - ValuewhenshortCond, close +atr * sl, 0
sl_short_atr · line 90 - ValuewhenlongCond, close + atr * tp1, 0
atr_tp_long1 · line 95 - ValuewhenlongCond, close+math.abs(close-sl_long )* tp1, 0
rr_tp_long1 · line 95 - ValuewhenlongCond, close + atr * tp2, 0
atr_tp_long2 · line 96 - ValuewhenlongCond, close+math.abs(close-sl_long )* tp2, 0
rr_tp_long2 · line 96 - ValuewhenlongCond, close + atr * tp3, 0
atr_tp_long3 · line 97 - ValuewhenlongCond, close+math.abs(close-sl_long )* tp3, 0
rr_tp_long3 · line 97 - ValuewhenlongCond, close + atr * tp4, 0
atr_tp_long4 · line 98 - ValuewhenlongCond, close+math.abs(close-sl_long )* tp4, 0
rr_tp_long4 · line 98 - ValuewhenshortCond, close - atr * tp1, 0
atr_tp_short1 · line 99 - ValuewhenshortCond, close-math.abs(close-sl_short)*tp1, 0
rr_tp_short1 · line 99 - ValuewhenshortCond, close - atr * tp2, 0
atr_tp_short2 · line 100 - ValuewhenshortCond, close-math.abs(close-sl_short)*tp2, 0
rr_tp_short2 · line 100 - ValuewhenshortCond, close - atr * tp3, 0
atr_tp_short3 · line 101 - ValuewhenshortCond, close-math.abs(close-sl_short)*tp3, 0
rr_tp_short3 · line 101 - ValuewhenshortCond, close - atr * tp4, 0
atr_tp_short4 · line 102 - ValuewhenshortCond, close-math.abs(close-sl_short)*tp4, 0
rr_tp_short4 · line 102
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 153 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 long long | longCond | On | 140 |
| Open short short | shortCond | On | 142 |
| Exit SL/TP1 stop and limit | perc_tp1 >0 | On | 144 |
| Exit SL/TP2 stop and limit | perc_tp2 >0 | On | 145 |
| Exit SL/TP3 stop and limit | perc_tp3 >0 | On | 146 |
| Exit SL/TP4 stop and limit | perc_tp4 >0 | On | 147 |
| Exit SL/TP1 stop and limit | perc_tp1 >0 | On | 148 |
| Exit SL/TP2 stop and limit | perc_tp2 >0 | On | 149 |
| Exit SL/TP3 stop and limit | perc_tp3 >0 | On | 150 |
| Exit SL/TP4 stop and limit | perc_tp4 >0 | On | 151 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| (no title) | alert | longCond_txt | 170 |
| (no title) | alert | shortCond_txt | 172 |
| (no title) | alert | long_sltp_txt | 174 |
| (no title) | alert | short_sltp_txt | 176 |
Does it repaint?
No
No repainting patterns in the source.
- 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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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 | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.1% | +5.5% | 12 | 42% | 0.98 | 4.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.5% | +8.4% | 8 | 50% | 1.21 | 3.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −9.6% | +21.5% | 20 | 25% | 0.13 | 10.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −5.9% | +17.1% | 12 | 0% | 0.00 | 5.9% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
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 |
|---|---|---|---|---|---|
| Impulse Strategy Signals V2 this page | — | v5 strategy | 0.0% | 0 | 0.0% |
| Bollinger band & Volume based strategy V2 by Ayden_C | sma atr rsi valuewhen | v4 strategy | −0.1% | 5 | 0.2% |
| MA cross strategy by Glenn234 | sma rsi atr | v5 strategy | −606.2% | 35 | 656.0% |
| Estrategia Larry Connors [JoseMetal] by JoseMetal | sma rsi atr | v5 strategy | +2.7% | 24 | 2.4% |
| [fpemehd] Strategy Template by DuDu95 | atr sma valuewhen | v5 strategy | +10.5% | 41 | 14.8% |
| 3 x EMA + Stochastic RSI + ATR by tomimarson | rsi sma atr | v4 strategy | −42.3% | 18 | 44.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.
- 8 strategy calls use the when = argument (line 144, 145, 146, 147, 148, 149, 150, 151). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 15 settings use the untyped input() (line 5, 6, 7, 19, 20, 46, 60, 61, 62, 63, 152, 165, 166, 167, 168). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 12 trades and the same P&L on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.int()
- Line 6: input() → input.int()
- Line 7: input() → input.int()
- Line 19: input() → input.source()
- Line 20: input() → input.int()
- Line 46: input() → input.int()
17 more edits
- Line 60: input() → input.float()
- Line 61: input() → input.float()
- Line 62: input() → input.float()
- Line 63: input() → input.float()
- Line 144: when = perc_tp1 >0 → if block
- Line 145: when = perc_tp2 >0 → if block
- Line 146: when = perc_tp3 >0 → if block
- Line 147: when = perc_tp4 >0 → if block
- Line 148: when = perc_tp1 >0 → if block
- Line 149: when = perc_tp2 >0 → if block
- Line 150: when = perc_tp3 >0 → if block
- Line 151: when = perc_tp4 >0 → if block
- Line 152: input() → input.bool()
- Line 165: input() → input.string()
- Line 166: input() → input.string()
- Line 167: input() → input.string()
- Line 168: input() → input.string()
⋯strategy(title = "Impulse Strategy Signals (SMMA Cross)", overlay = true, default_qty_value = 100, initial_capital=100000,default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true) -ma_len1 = input(21, title="SMMA Length 1 ", group='SMMA')+ma_len1 = input.int(21, title="SMMA Length 1 ", group='SMMA')-ma_len2 = input(50, title="SMMA Length 2 ", group='SMMA')+ma_len2 = input.int(50, title="SMMA Length 2 ", group='SMMA')-ma_len3 = input(200, title="SMMA Length 3 ", group='SMMA')+ma_len3 = input.int(200, title="SMMA Length 3 ", group='SMMA')smma(src, len) =>⋯plot(ma3, title='MA3', color=color.white)-rsi_src = input(close, title="RSI Source", group='RSI')+rsi_src = input.source(close, title="RSI Source", group='RSI')-rsi_len = input(14, title='RSI Length', group='RSI')+rsi_len = input.int(14, title='RSI Length', group='RSI')rsi = ta.rsi(rsi_src, rsi_len)⋯strat_grp = "strategy settings"-atrlen = input(14, title="ATR Length", group=strat_grp)+atrlen = input.int(14, title="ATR Length", group=strat_grp)atr = ta.atr(atrlen)⋯i_tp4 = input.float(4.0, title="TP 4", minval=0, step=0.1, inline="i_tp4 ", group=strat_grp)-perc_tp1_0 = input(10.0 , title="QTY%", inline="i_tp1 ", group=strat_grp)+perc_tp1_0 = input.float(10.0 , title="QTY%", inline="i_tp1 ", group=strat_grp)-perc_tp2_0 = input(10.0 , title="QTY%", inline="i_tp2 ", group=strat_grp)+perc_tp2_0 = input.float(10.0 , title="QTY%", inline="i_tp2 ", group=strat_grp)-perc_tp3_0 = input(30.0 , title="QTY%", inline="i_tp3 ", group=strat_grp)+perc_tp3_0 = input.float(30.0 , title="QTY%", inline="i_tp3 ", group=strat_grp)-perc_tp4_0 = input(100.0, title="QTY%", inline="i_tp4 ", group=strat_grp)+perc_tp4_0 = input.float(100.0, title="QTY%", inline="i_tp4 ", group=strat_grp)perc_tp1 = perc_tp1_0⋯ -strategy.exit("SL/TP1 ", from_entry = "long" , stop=sl_long , limit=tp_long1 , qty_percent=perc_tp1 , when=perc_tp1 >0)+if perc_tp1 >0+ strategy.exit("SL/TP1 ", from_entry = "long" , stop=sl_long , limit=tp_long1 , qty_percent=perc_tp1 )-strategy.exit("SL/TP2 ", from_entry = "long" , stop=sl_long , limit=tp_long2 , qty_percent=perc_tp2 , when=perc_tp2 >0)+if perc_tp2 >0+ strategy.exit("SL/TP2 ", from_entry = "long" , stop=sl_long , limit=tp_long2 , qty_percent=perc_tp2 )-strategy.exit("SL/TP3 ", from_entry = "long" , stop=sl_long , limit=tp_long3 , qty_percent=perc_tp3 , when=perc_tp3 >0)+if perc_tp3 >0+ strategy.exit("SL/TP3 ", from_entry = "long" , stop=sl_long , limit=tp_long3 , qty_percent=perc_tp3 )-strategy.exit("SL/TP4 ", from_entry = "long" , stop=sl_long , limit=tp_long4 , qty_percent=perc_tp4 , when=perc_tp4 >0)+if perc_tp4 >0+ strategy.exit("SL/TP4 ", from_entry = "long" , stop=sl_long , limit=tp_long4 , qty_percent=perc_tp4 )-strategy.exit("SL/TP1 ", from_entry = "short" , stop=sl_short, limit=tp_short1 , qty_percent=perc_tp1 , when=perc_tp1 >0)+if perc_tp1 >0+ strategy.exit("SL/TP1 ", from_entry = "short" , stop=sl_short, limit=tp_short1 , qty_percent=perc_tp1 )-strategy.exit("SL/TP2 ", from_entry = "short" , stop=sl_short, limit=tp_short2 , qty_percent=perc_tp2 , when=perc_tp2 >0)+if perc_tp2 >0+ strategy.exit("SL/TP2 ", from_entry = "short" , stop=sl_short, limit=tp_short2 , qty_percent=perc_tp2 )-strategy.exit("SL/TP3 ", from_entry = "short" , stop=sl_short, limit=tp_short3 , qty_percent=perc_tp3 , when=perc_tp3 >0)+if perc_tp3 >0+ strategy.exit("SL/TP3 ", from_entry = "short" , stop=sl_short, limit=tp_short3 , qty_percent=perc_tp3 )-strategy.exit("SL/TP4 ", from_entry = "short" , stop=sl_short, limit=tp_short4 , qty_percent=perc_tp4 , when=perc_tp4 >0)+if perc_tp4 >0+ strategy.exit("SL/TP4 ", from_entry = "short" , stop=sl_short, limit=tp_short4 , qty_percent=perc_tp4 )-show_sltp = input(true, title="Show SL/TP Lines on Chart")+show_sltp = input.bool(true, title="Show SL/TP Lines on Chart")plot(show_sltp and pos== 1? tp_long1 : na, color=color.green, style=plot.style_linebr, title="TP Long 1")⋯plotshape(shortCond, textcolor=#ffffff, color=color.red , style=shape.labeldown, title="Sell", text="Sell", location=location.abovebar, offset=0, size=size.small)-longCond_txt = input("", title='Alert Msg: LONG Entry', group='Alert Message', inline='longCond_txt ')+longCond_txt = input.string("", title='Alert Msg: LONG Entry', group='Alert Message', inline='longCond_txt ')-shortCond_txt = input("", title='Alert Msg: SHORT Entry', group='Alert Message', inline='shortCond_txt ')+shortCond_txt = input.string("", title='Alert Msg: SHORT Entry', group='Alert Message', inline='shortCond_txt ')-long_sltp_txt = input("", title='Alert Msg: LONG SL/TP', group='Alert Message', inline='long_sltp_txt ')+long_sltp_txt = input.string("", title='Alert Msg: LONG SL/TP', group='Alert Message', inline='long_sltp_txt ')-short_sltp_txt = input("", title='Alert Msg: SHORT SL/TP', group='Alert Message', inline='short_sltp_txt ')+short_sltp_txt = input.string("", title='Alert Msg: SHORT SL/TP', group='Alert Message', inline='short_sltp_txt ')if longCond⋯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: 12 before, 8 after on BTCUSDT 4h, 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.
⋯// © Hiubris_Indicators-//@version=5+//@version=6strategy(title = "Impulse Strategy Signals (SMMA Cross)", overlay = true, default_qty_value = 100, initial_capital=100000,default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true) ⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 73 ms | With a workaround 1,000 bars in 176 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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/// © Hiubris_Indicators//@version=5strategy(title = "Impulse Strategy Signals (SMMA Cross)", overlay = true, default_qty_value = 100, initial_capital=100000,default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true) ma_len1 = input(21, title="SMMA Length 1 ", group='SMMA')ma_len2 = input(50, title="SMMA Length 2 ", group='SMMA')ma_len3 = input(200, title="SMMA Length 3 ", group='SMMA')smma(src, len) => smma = 0.0 smma := na(smma[1]) ? ta.sma(src, len) : (smma[1] * (len - 1) + src) / len smma ma1 = smma(close, ma_len1)ma2 = smma(close, ma_len2)ma3 = smma(close, ma_len3)plot(ma1, title='MA1', color=color.green)plot(ma2, title='MA2', color=color.orange)plot(ma3, title='MA3', color=color.white)rsi_src = input(close, title="RSI Source", group='RSI')rsi_len = input(14, title='RSI Length', group='RSI')rsi = ta.rsi(rsi_src, rsi_len)rsi_long = input.float(50.0, title="LONG: RSI Entry Threshold", group='RSI')rsi_short= input.float(50.0, title="SHORT: RSI Entry Threshold", group='RSI')// Trading Session 1session1 = input.session("0700-1600", title="Trading Session 1")+":1234567"t1 = time(timeframe.period, session1)trading_session_filter1 = na(t1) ? 0 : 1// Trading Session 2session2 = input.session("1200-2100", title="Trading Session 2")+":1234567"t2 = time(timeframe.period, session2)trading_session_filter2 = na(t2) ? 0 : 1bgcolor(trading_session_filter1? color.new(color.blue, 90):na, title="Trading Session 1")bgcolor(trading_session_filter2? color.new(color.blue, 90):na, title="Trading Session 2")trading_session_cond = (trading_session_filter1 or trading_session_filter2) and not (trading_session_filter1 and trading_session_filter2)long0 = rsi>rsi_long and close>ma1 and close>ma2 and close>ma3 and ((ta.crossover (ma1, ma2) or ta.crossover (close, ma3)))short0= rsi<rsi_short and close<ma1 and close<ma2 and close<ma3 and ((ta.crossunder(ma1, ma2) or ta.crossunder(close, ma3)))long = long0 and not long0 [1] and trading_session_condshort= short0 and not short0[1] and trading_session_cond// Position Management Toolspos = 0.0pos:= long? 1 : short? -1 : pos[1]longCond = long and (pos[1]!= 1 or na(pos[1]))shortCond = short and (pos[1]!=-1 or na(pos[1]))// EXIT FUNCTIONS //strat_grp = "strategy settings"atrlen = input(14, title="ATR Length", group=strat_grp)atr = ta.atr(atrlen)i_sl_sw = input.string("ATR", title="", inline="i_sl ", options=["%", "ATR"], group=strat_grp)i_tsl_sw = input.string("ATR", title="", inline="i_tsl", options=["ATR", "%"], group=strat_grp)i_sl = input.float(2.0, title="● Stop Loss ", minval=0, inline="i_sl ", group=strat_grp,step=0.1)i_tsl = input.float(0.0, title="● Trailing SL", minval=0, inline="i_tsl", group=strat_grp,step=0.1)i_tp1_sw = input.string("%", title="", inline="i_tp1 ", options=["%", "ATR", "R:R"], group=strat_grp)i_tp2_sw = input.string("%", title="", inline="i_tp2 ", options=["%", "ATR", "R:R"], group=strat_grp)i_tp3_sw = input.string("%", title="", inline="i_tp3 ", options=["%", "ATR", "R:R"], group=strat_grp)i_tp4_sw = input.string("%", title="", inline="i_tp4 ", options=["%", "ATR", "R:R"], group=strat_grp)i_tp1 = input.float(1.0, title="TP 1", minval=0, step=0.1, inline="i_tp1 ", group=strat_grp)i_tp2 = input.float(2.0, title="TP 2", minval=0, step=0.1, inline="i_tp2 ", group=strat_grp)i_tp3 = input.float(3.0, title="TP 3", minval=0, step=0.1, inline="i_tp3 ", group=strat_grp)i_tp4 = input.float(4.0, title="TP 4", minval=0, step=0.1, inline="i_tp4 ", group=strat_grp)perc_tp1_0 = input(10.0 , title="QTY%", inline="i_tp1 ", group=strat_grp)perc_tp2_0 = input(10.0 , title="QTY%", inline="i_tp2 ", group=strat_grp)perc_tp3_0 = input(30.0 , title="QTY%", inline="i_tp3 ", group=strat_grp)perc_tp4_0 = input(100.0, title="QTY%", inline="i_tp4 ", group=strat_grp)perc_tp1 = perc_tp1_0perc_tp2 = perc_tp1_0+perc_tp2_0==100? 100 : perc_tp2_0perc_tp3 = perc_tp1_0+perc_tp2_0+perc_tp3_0==100? 100 : perc_tp3_0perc_tp4 = perc_tp1_0+perc_tp2_0+perc_tp3_0+perc_tp4_0==100? 100 : perc_tp4_0sl = i_sl >0? (i_sl_sw =="%"? i_sl /100 : i_sl ) : 99999tsl = i_tsl>0? (i_tsl_sw=="%"? i_tsl/100 : i_tsl) : 99999tp1 = i_tp1 >0? (i_tp1_sw =="%"? i_tp1 /100 : i_tp1 ) : 99999tp2 = i_tp2 >0? (i_tp2_sw =="%"? i_tp2 /100 : i_tp2 ) : 99999tp3 = i_tp3 >0? (i_tp3_sw =="%"? i_tp3 /100 : i_tp3 ) : 99999tp4 = i_tp4 >0? (i_tp4_sw =="%"? i_tp4 /100 : i_tp4 ) : 99999long_entry = ta.valuewhen(longCond , close, 0)short_entry = ta.valuewhen(shortCond, close, 0)// Trailing Stop Losstrail_long = 0.0, trail_short = 0.0trail_long := longCond? high : high>trail_long[1]? high : pos<1 ? 0 : trail_long[1]trail_short := shortCond? low : low<trail_short[1]? low : pos>-1 ? 99999 : trail_short[1]trail_long_final_atr = trail_long - atr*tsltrail_short_final_atr = trail_short + atr*tsltrail_long_final_perc = trail_long * (1-tsl)trail_short_final_perc = trail_short * (1+tsl)trail_long_final = i_tsl_sw=="ATR"? trail_long_final_atr : trail_long_final_perc trail_short_final = i_tsl_sw=="ATR"? trail_short_final_atr : trail_short_final_perc// Stop Loss Functionssl_long_perc = long_entry * (1 - sl)sl_short_perc = short_entry * (1 + sl)sl_long_atr = ta.valuewhen(longCond , close -atr * sl, 0)sl_short_atr = ta.valuewhen(shortCond, close +atr * sl, 0)sl_long0 = i_sl_sw =="%"? sl_long_perc : i_sl_sw =="ATR"? sl_long_atr : 0 sl_short0 = i_sl_sw =="%"? sl_short_perc : i_sl_sw =="ATR"? sl_short_atr : 99999sl_long = math.max(sl_long0 , trail_long_final )sl_short = math.min(sl_short0, trail_short_final)perc_tp_long1 = long_entry * (1 + tp1 ), atr_tp_long1 = ta.valuewhen(longCond , close + atr * tp1 , 0), rr_tp_long1 = ta.valuewhen(longCond , close+math.abs(close-sl_long )* tp1 , 0)perc_tp_long2 = long_entry * (1 + tp2 ), atr_tp_long2 = ta.valuewhen(longCond , close + atr * tp2 , 0), rr_tp_long2 = ta.valuewhen(longCond , close+math.abs(close-sl_long )* tp2 , 0)perc_tp_long3 = long_entry * (1 + tp3 ), atr_tp_long3 = ta.valuewhen(longCond , close + atr * tp3 , 0), rr_tp_long3 = ta.valuewhen(longCond , close+math.abs(close-sl_long )* tp3 , 0)perc_tp_long4 = long_entry * (1 + tp4 ), atr_tp_long4 = ta.valuewhen(longCond , close + atr * tp4 , 0), rr_tp_long4 = ta.valuewhen(longCond , close+math.abs(close-sl_long )* tp4 , 0)perc_tp_short1 = short_entry * (1 - tp1 ), atr_tp_short1 = ta.valuewhen(shortCond , close - atr * tp1 , 0), rr_tp_short1 = ta.valuewhen(shortCond, close-math.abs(close-sl_short)*tp1 , 0)perc_tp_short2 = short_entry * (1 - tp2 ), atr_tp_short2 = ta.valuewhen(shortCond , close - atr * tp2 , 0), rr_tp_short2 = ta.valuewhen(shortCond, close-math.abs(close-sl_short)*tp2 , 0)perc_tp_short3 = short_entry * (1 - tp3 ), atr_tp_short3 = ta.valuewhen(shortCond , close - atr * tp3 , 0), rr_tp_short3 = ta.valuewhen(shortCond, close-math.abs(close-sl_short)*tp3 , 0)perc_tp_short4 = short_entry * (1 - tp4 ), atr_tp_short4 = ta.valuewhen(shortCond , close - atr * tp4 , 0), rr_tp_short4 = ta.valuewhen(shortCond, close-math.abs(close-sl_short)*tp4 , 0)tp_long1 = i_tp1_sw =="%"? perc_tp_long1 : i_tp1_sw =="ATR"? atr_tp_long1 : rr_tp_long1 tp_long2 = i_tp2_sw =="%"? perc_tp_long2 : i_tp2_sw =="ATR"? atr_tp_long2 : rr_tp_long2 tp_long3 = i_tp3_sw =="%"? perc_tp_long3 : i_tp3_sw =="ATR"? atr_tp_long3 : rr_tp_long3 tp_long4 = i_tp4_sw =="%"? perc_tp_long4 : i_tp4_sw =="ATR"? atr_tp_long4 : rr_tp_long4 tp_short1 = i_tp1_sw =="%"? perc_tp_short1 : i_tp1_sw =="ATR"? atr_tp_short1 : rr_tp_short1 tp_short2 = i_tp2_sw =="%"? perc_tp_short2 : i_tp2_sw =="ATR"? atr_tp_short2 : rr_tp_short2 tp_short3 = i_tp3_sw =="%"? perc_tp_short3 : i_tp3_sw =="ATR"? atr_tp_short3 : rr_tp_short3 tp_short4 = i_tp4_sw =="%"? perc_tp_short4 : i_tp4_sw =="ATR"? atr_tp_short4 : rr_tp_short4 // Take Profits %sum_tp4 = perc_tp1 + perc_tp2 + perc_tp3 + perc_tp4sum_tp3 = perc_tp1 + perc_tp2 + perc_tp3sum_tp2 = perc_tp1 + perc_tp2sum_tp1 = perc_tp1last_tp_long = i_tp4>0 and sum_tp4>=100? tp_long4 : i_tp3>0 and sum_tp3>=100? tp_long3 : i_tp2>0 and sum_tp2>=100? tp_long2 : i_tp1>0 and sum_tp1>=100? tp_long1 : na last_tp_short = i_tp4>0 and sum_tp4>=100? tp_short4 : i_tp3>0 and sum_tp3>=100? tp_short3 : i_tp2>0 and sum_tp2>=100? tp_short2 : i_tp1>0 and sum_tp1>=100? tp_short1 : na // Position Adjustmentlong_sl = low <sl_long[1] and pos[1]==1short_sl = high>sl_short[1] and pos[1]==-1final_long_tp = high>last_tp_long [1] and pos[1]==1final_short_tp = low <last_tp_short[1] and pos[1]==-1if ((long_sl or final_long_tp) and not shortCond) or ((short_sl or final_short_tp) and not longCond) pos:=0// Strategy Backtest Limiting Algorithmi_startTime = input.time(defval = timestamp("01 Sep 2002 13:30 +0000"), title = "Backtesting Start Time")i_endTime = input.time(defval = timestamp("30 Sep 2099 19:30 +0000"), title = "Backtesting End Time" )timeCond = (time > i_startTime) and (time < i_endTime)equity = strategy.initial_capital + strategy.netprofitif equity>0 and timeCond if longCond strategy.entry("long" , strategy.long ) if shortCond strategy.entry("short", strategy.short) strategy.exit("SL/TP1 ", from_entry = "long" , stop=sl_long , limit=tp_long1 , qty_percent=perc_tp1 , when=perc_tp1 >0)strategy.exit("SL/TP2 ", from_entry = "long" , stop=sl_long , limit=tp_long2 , qty_percent=perc_tp2 , when=perc_tp2 >0)strategy.exit("SL/TP3 ", from_entry = "long" , stop=sl_long , limit=tp_long3 , qty_percent=perc_tp3 , when=perc_tp3 >0)strategy.exit("SL/TP4 ", from_entry = "long" , stop=sl_long , limit=tp_long4 , qty_percent=perc_tp4 , when=perc_tp4 >0)strategy.exit("SL/TP1 ", from_entry = "short" , stop=sl_short, limit=tp_short1 , qty_percent=perc_tp1 , when=perc_tp1 >0)strategy.exit("SL/TP2 ", from_entry = "short" , stop=sl_short, limit=tp_short2 , qty_percent=perc_tp2 , when=perc_tp2 >0)strategy.exit("SL/TP3 ", from_entry = "short" , stop=sl_short, limit=tp_short3 , qty_percent=perc_tp3 , when=perc_tp3 >0)strategy.exit("SL/TP4 ", from_entry = "short" , stop=sl_short, limit=tp_short4 , qty_percent=perc_tp4 , when=perc_tp4 >0)show_sltp = input(true, title="Show SL/TP Lines on Chart")plot(show_sltp and pos== 1? tp_long1 : na, color=color.green, style=plot.style_linebr, title="TP Long 1")plot(show_sltp and pos== 1? tp_long2 : na, color=color.green, style=plot.style_linebr, title="TP Long 2")plot(show_sltp and pos== 1? tp_long3 : na, color=color.green, style=plot.style_linebr, title="TP Long 3")plot(show_sltp and pos== 1? tp_long4 : na, color=color.green, style=plot.style_linebr, title="TP Long 4")plot(show_sltp and pos==-1? tp_short1 : na, color=color.green, style=plot.style_linebr, title="TP Short 1")plot(show_sltp and pos==-1? tp_short2 : na, color=color.green, style=plot.style_linebr, title="TP Short 2")plot(show_sltp and pos==-1? tp_short3 : na, color=color.green, style=plot.style_linebr, title="TP Short 3")plot(show_sltp and pos==-1? tp_short4 : na, color=color.green, style=plot.style_linebr, title="TP Short 4")plot(show_sltp and pos== 1? sl_long : na, color=color.red , style=plot.style_linebr, title="SL Long ")plot(show_sltp and pos==-1? sl_short : na, color=color.red , style=plot.style_linebr, title="SL Short")plotshape(longCond, textcolor=#ffffff, color=color.lime, style=shape.labelup , title="Buy" , text="Buy" , location=location.belowbar, offset=0, size=size.small)plotshape(shortCond, textcolor=#ffffff, color=color.red , style=shape.labeldown, title="Sell", text="Sell", location=location.abovebar, offset=0, size=size.small)longCond_txt = input("", title='Alert Msg: LONG Entry', group='Alert Message', inline='longCond_txt ')shortCond_txt = input("", title='Alert Msg: SHORT Entry', group='Alert Message', inline='shortCond_txt ')long_sltp_txt = input("", title='Alert Msg: LONG SL/TP', group='Alert Message', inline='long_sltp_txt ')short_sltp_txt = input("", title='Alert Msg: SHORT SL/TP', group='Alert Message', inline='short_sltp_txt ')if longCond alert(longCond_txt, alert.freq_once_per_bar_close)if shortCond alert(shortCond_txt, alert.freq_once_per_bar_close)if long_sl or final_long_tp alert(long_sltp_txt, alert.freq_once_per_bar_close)if short_sl or final_short_tp alert(short_sltp_txt, alert.freq_once_per_bar_close)