Pine Script v4 · Strategy
SL1 Pips after TP2 (MA)
A Pine Script v4 strategy built on EMA (close, 15), WMA (close, 45) and Crossover (ema, wma). It has 6 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 is 10 contracts and pays no commission. 16 settings; defaults include Start year 2022, Start month 1 and Start day 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 SL1 Pips after TP2 (MA) 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 SL1 Pips after TP2 (MA) do on BTCUSDT 1D with default settings?
- +0.44%
- From 26 Jan 2024 to 21 Oct 2026, over 33 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can SL1 Pips after TP2 (MA) 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 SL1 Pips after TP2 (MA) use a stop loss?
- Yes
- strategy.exit sets a stop on line 78, 79, 82, 83.
- Does SL1 Pips after TP2 (MA) run in Zeal Terminal?
- Yes
- It compiles in 26 ms and runs 1,000 bars in 74 ms, in the browser.
BTCUSDT · 1D · default settings
- ema
- wma
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.44%
- $437.50
- Buy and hold
- +102.5%
- same period
- Closed trades
- 33
- 13 won · 20 lost
- Profitable
- 39.4%
- of closed trades
- Profit factor
- 2.09
- gross profit ÷ gross loss
- Max drawdown
- 0.10%
- −$100.10
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.
-
Note
3 settings have no label
In the settings dialog Buy (line 6), Sell (line 7), pip (line 38) 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 77, 81. The upgraded version below moves each call inside an if block.
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 ema → ta.ema, wma → ta.wma, crossover → ta.crossover, crossunder → ta.crossunder, max → math.max, min → math.min.
- 2 strategy calls use the when = argument (line 77, 81). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 33 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 23: ema → ta.ema
- Line 24: wma → ta.wma
- Line 27: crossover → ta.crossover
- Line 33: crossunder → ta.crossunder
- Line 62: max → math.max
5 more edits
- Line 63: min → math.min
- Line 64: max → math.max
- Line 65: min → math.min
- Line 68: min → math.min
- Line 72: max → math.max
⋯// © gpadihar-//@version=4+//@version=5strategy("SL1 Pips after TP2 (MA)", commission_type=strategy.commission.cash_per_order, overlay=true, default_qty_value=10, initial_capital=100000)⋯wma_period = input(title='WMA period',defval=45)-ema = ema(close,ema_period)+ema = ta.ema(close,ema_period)-wma = wma(close,wma_period)+wma = ta.wma(close,wma_period)// Entry Conditionbuy =- crossover(ema,wma) and+ ta.crossover(ema,wma) and nz(strategy.position_size) >= 0 and Buy and⋯sell =- crossunder(ema,wma) and+ ta.crossunder(ema,wma) and nz(strategy.position_size) >= 0 and Sell and⋯// adjust trade parameters and trailing stop calculations-TVL := max(TP1,open) - pip[1]+TVL := math.max(TP1,open) - pip[1]-TVS := min(TP1,open) + pip[1]+TVS := math.min(TP1,open) + pip[1]-TSL := open[1] > TSL[1] ? max(TVL,TSL[1]):TVL +TSL := open[1] > TSL[1] ? math.max(TVL,TSL[1]):TVL -TSS := open[1] < TSS[1] ? min(TVS,TSS[1]):TVS+TSS := open[1] < TSS[1] ? math.min(TVS,TSS[1]):TVSif LS and high > TP1 if open <= TP1- SL2:=min(EP,TSL)+ SL2:=math.min(EP,TSL) ⋯ if open >= TP1- SL2:=max(EP,TSS)+ SL2:=math.max(EP,TSS)// Closing conditions⋯Same 33 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.bool()
- Line 7: input() → input.bool()
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 11: input() → input.int()
- Line 12: input() → input.int()
12 more edits
- Line 13: input() → input.int()
- Line 14: input() → input.bool()
- Line 15: input() → input.int()
- Line 16: input() → input.int()
- Line 17: input() → input.int()
- Line 18: input() → input.int()
- Line 19: input() → input.int()
- Line 21: input() → input.int()
- Line 22: input() → input.int()
- Line 38: input() → input.int()
- Line 77: when = buy and not SS → if block
- Line 81: when = sell and not LS → if block
⋯// Strategy-Buy = input(true)+Buy = input.bool(true)-Sell = input(true)+Sell = input.bool(true)// Date Range-start_year = input(title='Start year' ,defval=2022)+start_year = input.int(title='Start year' ,defval=2022)-start_month = input(title='Start month' ,defval=1)+start_month = input.int(title='Start month' ,defval=1)-start_day = input(title='Start day' ,defval=1)+start_day = input.int(title='Start day' ,defval=1)-start_hour = input(title='Start hour' ,defval=0)+start_hour = input.int(title='Start hour' ,defval=0)-start_minute = input(title='Start minute' ,defval=0)+start_minute = input.int(title='Start minute' ,defval=0)-end_time = input(title='set end time?',defval=false)+end_time = input.bool(title='set end time?',defval=false)-end_year = input(title='end year' ,defval=2019)+end_year = input.int(title='end year' ,defval=2019)-end_month = input(title='end month' ,defval=12)+end_month = input.int(title='end month' ,defval=12)-end_day = input(title='end day' ,defval=31)+end_day = input.int(title='end day' ,defval=31)-end_hour = input(title='end hour' ,defval=23)+end_hour = input.int(title='end hour' ,defval=23)-end_minute = input(title='end minute' ,defval=59)+end_minute = input.int(title='end minute' ,defval=59)// MA-ema_period = input(title='EMA period',defval=15)+ema_period = input.int(title='EMA period',defval=15)-wma_period = input(title='WMA period',defval=45)+wma_period = input.int(title='WMA period',defval=45)ema = ta.ema(close,ema_period)⋯// Pips-pip = input(20)*10*syminfo.mintick+pip = input.int(20)*10*syminfo.mintick// Trading parameters //⋯// Buy-strategy.entry("buy" , strategy.long, when=buy and not SS)+if buy and not SS+ strategy.entry("buy" , strategy.long)strategy.exit ("exit1", from_entry="buy", stop=SL1, limit=TP1, qty_percent=100)⋯// Sell-strategy.entry("sell" , strategy.short, when=sell and not LS)+if sell and not LS+ strategy.entry("sell" , strategy.short)strategy.exit ("exit3", from_entry="sell", stop=SL1, limit=TP1, qty_percent=100)⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L40:C0: a 'bool' cannot be 'na' in Pine Script v6 — declare it as true or false. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © gpadihar-//@version=5+//@version=6strategy("SL1 Pips after TP2 (MA)", commission_type=strategy.commission.cash_per_order, overlay=true, default_qty_value=10, initial_capital=100000)⋯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 |
|---|---|---|---|
| Buy (no label in the script) | true | bool | buy |
| Sell (no label in the script) | true | bool | sell |
| Start year | 2022 | int | buy sell |
| Start month | 1 | int | buy sell |
| Start day | 1 | int | buy sell |
| Start hour | 0 | int | buy sell |
| Start minute | 0 | int | buy sell |
| set end time? | false | bool | buy sell |
| end year | 2019 | int | buy sell |
| end month | 12 | int | buy sell |
| end day | 31 | int | buy sell |
| end hour | 23 | int | buy sell |
| end minute | 59 | int | buy sell |
| EMA period | 15 | int | ema |
| WMA period | 45 | int | wma |
| Pip (no label in the script) | 20 | int | TVL TVS TP1 TP2 SL1 SL2 |
What it calculates
- EMA (
ema,ta.emain v5)close, EMA period (15)ema · line 23 - WMA (
wma,ta.wmain v5)close, WMA period (45)wma · line 24 - Crossover (
crossover,ta.crossoverin v5)ema, wmabuy · line 26 - Crossunder (
crossunder,ta.crossunderin v5)ema, wmasell · line 32
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 buy long | buy and not SS | On | 77 |
| Exit exit1 stop and limit | every bar | On | 78 |
| Exit exit2 stop and limit | every bar | On | 79 |
| Open sell short | sell and not LS | On | 81 |
| Exit exit3 stop and limit | every bar | On | 82 |
| Exit exit4 stop and limit | every bar | On | 83 |
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 | +0.4% | +102.5% | 33 | 39% | 2.09 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.1% | +5.5% | 39 | 31% | 1.19 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.4% | +9.8% | 29 | 41% | 2.12 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 35 | 31% | 1.08 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 29 | 31% | 1.10 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.1% | +21.5% | 21 | 29% | 1.28 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.1% | +148.3% | 31 | 32% | 1.33 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.0% | +17.1% | 28 | 25% | 0.95 | 0.2% | 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 | 9 | 67% | $300.30 |
| Sideways market | 17 | 35% | $177.40 |
| Falling market | 7 | 14% | −$40.20 |
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 33 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 33 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 14 Apr 2024 63,924.50 | 14 Apr 2024 63,916.50 | 0 | $80.10 |
| long | 18 May 2024 67,024.00 | 18 May 2024 67,028.00 | 0 | $40.00 |
| short | 17 Jun 2024 66,676.90 | 17 Jun 2024 66,678.90 | 0 | −$20.10 |
| long | 20 Jul 2024 66,660.00 | 20 Jul 2024 66,658.00 | 0 | −$20.10 |
| short | 6 Aug 2024 54,018.80 | 6 Aug 2024 54,010.80 | 0 | $80.10 |
| long | 27 Aug 2024 62,834.00 | 27 Aug 2024 62,838.00 | 0 | $40.00 |
| short | 28 Aug 2024 59,415.00 | 28 Aug 2024 59,417.00 | 0 | −$20.00 |
| long | 19 Sep 2024 61,760.00 | 19 Sep 2024 61,758.00 | 0 | −$19.90 |
| short | 28 Dec 2024 94,299.00 | 28 Dec 2024 94,291.00 | 0 | $80.00 |
| long | 18 Jan 2025 104,077.00 | 18 Jan 2025 104,081.00 | 0 | $40.10 |
| short | 7 Feb 2025 96,554.40 | 7 Feb 2025 96,546.40 | 0 | $80.00 |
| long | 19 Apr 2025 84,474.70 | 19 Apr 2025 84,472.70 | 0 | −$20.10 |
| short | 19 Jun 2025 104,887.00 | 19 Jun 2025 104,889.00 | 0 | −$19.90 |
| long | 30 Jun 2025 108,357.00 | 30 Jun 2025 108,361.00 | 0 | $40.00 |
| short | 7 Aug 2025 114,992.00 | 7 Aug 2025 114,984.00 | 0 | $80.00 |
| long | 8 Aug 2025 117,472.00 | 8 Aug 2025 117,476.00 | 0 | $39.90 |
| short | 10 Aug 2025 116,462.00 | 10 Aug 2025 116,464.00 | 0 | −$20.00 |
| long | 11 Aug 2025 119,294.00 | 11 Aug 2025 119,292.00 | 0 | −$22.60 |
| short | 20 Aug 2025 112,873.00 | 20 Aug 2025 112,865.00 | 0 | $80.10 |
| long | 15 Sep 2025 115,268.00 | 15 Sep 2025 115,266.00 | 0 | −$20.00 |
| short | 27 Sep 2025 109,643.00 | 27 Sep 2025 109,645.00 | 0 | −$20.00 |
| long | 2 Oct 2025 118,595.00 | 2 Oct 2025 118,593.00 | 0 | −$20.00 |
| short | 16 Oct 2025 110,763.00 | 16 Oct 2025 110,765.00 | 0 | −$20.00 |
| long | 5 Jan 2026 91,529.70 | 5 Jan 2026 91,527.70 | 0 | −$20.10 |
| short | 26 Jan 2026 86,670.40 | 26 Jan 2026 86,662.40 | 0 | $80.00 |
| long | 13 Mar 2026 70,541.30 | 13 Mar 2026 70,539.30 | 0 | −$20.00 |
| short | 29 Mar 2026 66,377.00 | 29 Mar 2026 66,379.00 | 0 | −$19.90 |
| long | 10 Apr 2026 71,788.00 | 10 Apr 2026 71,786.00 | 0 | −$20.10 |
| short | 23 May 2026 75,539.50 | 23 May 2026 75,531.50 | 0 | $80.00 |
| long | 12 Jul 2026 63,819.00 | 12 Jul 2026 63,817.00 | 0 | −$20.10 |
| short | 13 Aug 2026 63,480.00 | 13 Aug 2026 63,482.00 | 0 | −$20.00 |
| long | 20 Aug 2026 69,334.80 | 20 Aug 2026 69,332.80 | 0 | −$19.90 |
| short | 18 Oct 2026 81,491.00 | 18 Oct 2026 81,493.00 | 0 | −$20.00 |
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 |
|---|---|---|---|---|---|
| SL1 Pips after TP2 (MA) this page | — | v4 strategy | +0.4% | 33 | 0.1% |
| Escaping of Rate from Avarage By Mustafa OZVER Strategy by Mustafaozver | wma ema | v4 strategy | +122.0% | 96 | 79.1% |
| Forex SWING Trader by SoftKill21 | wma ema | v4 strategy | No trades on this market | ||
| No-lose trading targets (Based on EoRfA) By Mustafa ÖZVER by Mustafaozver | wma ema | v4 strategy | No trades on this market | ||
| EDMA Scalping Strategy (Exponentially Deviating Moving Average) by MightyZinger | wma ema | v5 strategy | does not run in Zeal Terminal | ||
| tvbot Trend Following with Mean Reversion algo by tvbot- | ema wma | v5 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 26 ms | With a workaround 1,000 bars in 74 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. 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/// © gpadihar//@version=4strategy("SL1 Pips after TP2 (MA)", commission_type=strategy.commission.cash_per_order, overlay=true, default_qty_value=10, initial_capital=100000)// StrategyBuy = input(true)Sell = input(true)// Date Rangestart_year = input(title='Start year' ,defval=2022)start_month = input(title='Start month' ,defval=1)start_day = input(title='Start day' ,defval=1)start_hour = input(title='Start hour' ,defval=0)start_minute = input(title='Start minute' ,defval=0)end_time = input(title='set end time?',defval=false)end_year = input(title='end year' ,defval=2019)end_month = input(title='end month' ,defval=12)end_day = input(title='end day' ,defval=31)end_hour = input(title='end hour' ,defval=23)end_minute = input(title='end minute' ,defval=59)// MAema_period = input(title='EMA period',defval=15)wma_period = input(title='WMA period',defval=45)ema = ema(close,ema_period)wma = wma(close,wma_period)// Entry Conditionbuy = crossover(ema,wma) and nz(strategy.position_size) >= 0 and Buy and time > timestamp(start_year, start_month, start_day, start_hour, start_minute) and (end_time?(time < timestamp(end_year, end_month, end_day, end_hour, end_minute)):true) sell = crossunder(ema,wma) and nz(strategy.position_size) >= 0 and Sell and time > timestamp(start_year, start_month, start_day, start_hour, start_minute) and (end_time?(time < timestamp(end_year, end_month, end_day, end_hour, end_minute)):true)// Pipspip = input(20)*10*syminfo.mintick// Trading parameters //var bool LS = navar bool SS = navar float EP = navar float TVL = navar float TVS = navar float TSL = navar float TSS = navar float TP1 = navar float TP2 = navar float SL1 = navar float SL2 = naif buy or sell and strategy.position_size == 0 EP := close SL1 := EP - pip * 1 * (sell?-1:1) SL2 := EP - pip * (sell?-1:1) TP1 := EP + pip * 2 * (sell?-2:1) TP2 := EP + pip * 3 * (sell?-2:1) // current trade direction LS := buy or strategy.position_size > 0SS := sell or strategy.position_size < 0// adjust trade parameters and trailing stop calculationsTVL := max(TP1,open) - pip[1]TVS := min(TP1,open) + pip[1]TSL := open[1] > TSL[1] ? max(TVL,TSL[1]):TVL TSS := open[1] < TSS[1] ? min(TVS,TSS[1]):TVSif LS and high > TP1 if open <= TP1 SL2:=min(EP,TSL) if SS and low < TP1 if open >= TP1 SL2:=max(EP,TSS)// Closing conditionsclose_long = LS and open < SL2close_short = SS and open > SL2// Buystrategy.entry("buy" , strategy.long, when=buy and not SS)strategy.exit ("exit1", from_entry="buy", stop=SL1, limit=TP1, qty_percent=100)strategy.exit ("exit2", from_entry="buy", stop=SL2, limit=TP2)// Sellstrategy.entry("sell" , strategy.short, when=sell and not LS)strategy.exit ("exit3", from_entry="sell", stop=SL1, limit=TP1, qty_percent=100)strategy.exit ("exit4", from_entry="sell", stop=SL2, limit=TP2)// Plotsa=plot(strategy.position_size > 0 ? SL1 : na, color=#dc143c, style=plot.style_linebr)b=plot(strategy.position_size < 0 ? SL1 : na, color=#dc143c, style=plot.style_linebr) c=plot(strategy.position_size > 0 ? TP1 : na, color=#00ced1, style=plot.style_linebr) d=plot(strategy.position_size < 0 ? TP1 : na, color=#00ced1, style=plot.style_linebr) e=plot(strategy.position_size > 0 ? TP2 : na, color=#00ced1, style=plot.style_linebr) f=plot(strategy.position_size < 0 ? TP2 : na, color=#00ced1, style=plot.style_linebr) g=plot(strategy.position_size >= 0 ? na : EP, color=#ffffff, style=plot.style_linebr) h=plot(strategy.position_size <= 0 ? na : EP, color=#ffffff, style=plot.style_linebr) plot(ema,title="ema",color=#fff176)plot(wma,title="wma",color=#00ced1)