Pine Script v4 · Strategy
EMA crossover - BTC-USD 4hr
A Pine Script v4 strategy built on EMA (close, 11) and Crossover (ema_long_open_fast, ema_long_open_slow). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade commits 75% of equity and pays 0.075% commission. 18 settings; defaults include Start Date 1, Start Month 1 and Start Year 2020.
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 EMA crossover - BTC-USD 4hr repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did EMA crossover - BTC-USD 4hr 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. It closed no trades on the other markets either. A backtest, not investment advice.
- Can EMA crossover - BTC-USD 4hr be converted to Pine Script v5?
- Not tested
- The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
- Does EMA crossover - BTC-USD 4hr use a stop loss?
- Yes
- strategy.exit sets a stop on line 82, 89.
- Does EMA crossover - BTC-USD 4hr run in Zeal Terminal?
- Yes
- It compiles in 22 ms and runs 1,000 bars in 73 ms, in the browser.
BTCUSDT · 1D · default settings
- Long EMA open lower
- Long EMA close upper
- Long close lower
- Long close upper
- Short open fast
- Short open slow
- Short close fast
- Short close slow
- Long-term trend - Longs
- Long-term trend - Shorts
- 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, $1,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.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses long =, which v5 renamed line 78
long = on line 78, 85. v5 writes it as direction = strategy.long or strategy.short.
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, crossover → ta.crossover.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 10: input(type = input.integer) → input.int()
- Line 12: input(type = input.integer) → input.int()
- Line 14: input(type = input.integer) → input.int()
19 more edits
- Line 16: input(type = input.integer) → input.int()
- Line 28: ema → ta.ema
- Line 29: ema → ta.ema
- Line 30: ema → ta.ema
- Line 31: ema → ta.ema
- Line 38: ema → ta.ema
- Line 39: ema → ta.ema
- Line 40: ema → ta.ema
- Line 41: ema → ta.ema
- Line 44: crossover → ta.crossover
- Line 45: crossover → ta.crossover
- Line 47: input(type = input.float) → input.float()
- Line 50: crossover → ta.crossover
- Line 51: crossover → ta.crossover
- Line 53: input(type = input.float) → input.float()
- Line 69: ema → ta.ema
- Line 73: ema → ta.ema
- Line 78: long = true → direction = strategy.long
- Line 85: long = false → direction = strategy.short
⋯// © str1nger-//@version=4+//@version=5strategy(title="BTC - 4hr - Long/Short", shorttitle="BTC - 4hr - Long/Short", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=75,commission_type=strategy.commission.percent, commission_value=0.075)//////<---Uses a percentage of starting equity//DATE RANGE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-startDate = input(title="Start Date", type=input.integer,+startDate = input.int(title="Start Date", defval=1, minval=1, maxval=31)-startMonth = input(title="Start Month", type=input.integer,+startMonth = input.int(title="Start Month", defval=1, minval=1, maxval=12)-startYear = input(title="Start Year", type=input.integer,+startYear = input.int(title="Start Year", defval=2020, minval=2000, maxval=2100)-endDate = input(title="End Date", type=input.integer,+endDate = input.int(title="End Date", defval=1, minval=1, maxval=31)-endMonth = input(title="End Month", type=input.integer,+endMonth = input.int(title="End Month", defval=12, minval=1, maxval=12)-endYear = input(title="End Year", type=input.integer,+endYear = input.int(title="End Year", defval=2021, minval=2000, maxval=2100)⋯lcs= input(40, title="Long Close - Slow", step=1)-ema_long_open_fast = ema(close, lof)+ema_long_open_fast = ta.ema(close, lof)-ema_long_open_slow = ema(close, los)+ema_long_open_slow = ta.ema(close, los)-ema_long_close_fast= ema(close, lcf)+ema_long_close_fast= ta.ema(close, lcf)-ema_long_close_slow = ema(close, lcs)+ema_long_close_slow = ta.ema(close, lcs)//SHORT⋯scs= input(7, title="Short Close - Slow", step=1)-ema_short_open_fast = ema(close, sof)+ema_short_open_fast = ta.ema(close, sof)-ema_short_open_slow = ema(close, sos)+ema_short_open_slow = ta.ema(close, sos)-ema_short_close_fast = ema(close, scf)+ema_short_close_fast = ta.ema(close, scf)-ema_short_close_slow = ema(close, scs)+ema_short_close_slow = ta.ema(close, scs)//CONDITIONS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LONG-openlong = crossover(ema_long_open_fast, ema_long_open_slow)+openlong = ta.crossover(ema_long_open_fast, ema_long_open_slow)-closelong = crossover(ema_long_close_slow, ema_long_close_fast)+closelong = ta.crossover(ema_long_close_slow, ema_long_close_fast)//1.7%-long_loss_percent = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1.7) * 0.01+long_loss_percent = input.float(title="Long Stop Loss (%)", minval=0.0, step=0.1, defval=1.7) * 0.01long_stop_price = strategy.position_avg_price * (1 - long_loss_percent)//SHORT-openshort = crossover(ema_short_open_slow, ema_short_open_fast)+openshort = ta.crossover(ema_short_open_slow, ema_short_open_fast)-closeshort = crossover(ema_short_close_fast, ema_short_close_slow)+closeshort = ta.crossover(ema_short_close_fast, ema_short_close_slow)//0.4%-short_loss_percent = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=0.4) * 0.01+short_loss_percent = input.float(title="Short Stop Loss (%)", minval=0.0, step=0.1, defval=0.4) * 0.01short_stop_price = strategy.position_avg_price * (1 + short_loss_percent)⋯long_term_trend_longs= input(144, title="Long-term trend - Longs", step=1)-lttl= ema(close, long_term_trend_longs)+lttl= ta.ema(close, long_term_trend_longs)plot(lttl, "Long-term trend - Longs", linewidth=2, color=color.blue)⋯long_term_trend_shorts= input(89, title="Long-term trend - Shorts", step=1)-ltts = ema(close, long_term_trend_shorts)+ltts = ta.ema(close, long_term_trend_shorts)plot(ltts, "Long-term trend - Shorts", linewidth=2, color=color.blue)⋯if (inDateRange and openlong and (close > lttl))- strategy.entry("OL", long=true, comment="##insert open long comment here##")+ strategy.entry("OL", direction = strategy.long, comment="##insert open long comment here##")if (inDateRange and closelong)⋯if (inDateRange and openshort and (close < ltts))- strategy.entry("OS", long=false, comment="##insert open short comment here##")+ strategy.entry("OS", direction = strategy.short, comment="##insert open short comment here##")if (inDateRange and closeshort)⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 24: input() → input.int()
- Line 25: input() → input.int()
- Line 26: input() → input.int()
- Line 27: input() → input.int()
- Line 34: input() → input.int()
- Line 35: input() → input.int()
4 more edits
- Line 36: input() → input.int()
- Line 37: input() → input.int()
- Line 68: input() → input.int()
- Line 72: input() → input.int()
⋯//11,33,3,40-lof= input(11, title="Long Open - Fast", step=1)+lof= input.int(11, title="Long Open - Fast", step=1)-los= input(33, title="Long Open - Slow", step=1)+los= input.int(33, title="Long Open - Slow", step=1)-lcf= input(3, title="Long Close - Fast", step=1)+lcf= input.int(3, title="Long Close - Fast", step=1)-lcs= input(40, title="Long Close - Slow", step=1)+lcs= input.int(40, title="Long Close - Slow", step=1)ema_long_open_fast = ta.ema(close, lof)⋯//5,11,4,7-sof= input(5, title="Short Open - Fast", step=1)+sof= input.int(5, title="Short Open - Fast", step=1)-sos= input(11, title="Short Open - Slow", step=1)+sos= input.int(11, title="Short Open - Slow", step=1)-scf= input(4, title="Short Close - Fast", step=1)+scf= input.int(4, title="Short Close - Fast", step=1)-scs= input(7, title="Short Close - Slow", step=1)+scs= input.int(7, title="Short Close - Slow", step=1)ema_short_open_fast = ta.ema(close, sof)⋯//LONG 144-long_term_trend_longs= input(144, title="Long-term trend - Longs", step=1)+long_term_trend_longs= input.int(144, title="Long-term trend - Longs", step=1)lttl= ta.ema(close, long_term_trend_longs)⋯//SHORT 89-long_term_trend_shorts= input(89, title="Long-term trend - Shorts", step=1)+long_term_trend_shorts= input.int(89, title="Long-term trend - Shorts", step=1)ltts = ta.ema(close, long_term_trend_shorts)⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © str1nger-//@version=5+//@version=6strategy(title="BTC - 4hr - Long/Short", shorttitle="BTC - 4hr - Long/Short", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=75,commission_type=strategy.commission.percent, commission_value=0.075)//////<---Uses a percentage of starting equity⋯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 |
|---|---|---|---|
| Start Date | 1 | int (written input.integer) · min 1 · max 31 | inDateRange |
| Start Month | 1 | int (written input.integer) · min 1 · max 12 | inDateRange |
| Start Year | 2020 | int (written input.integer) · min 2000 · max 2100 | inDateRange |
| End Date | 1 | int (written input.integer) · min 1 · max 31 | inDateRange |
| End Month | 12 | int (written input.integer) · min 1 · max 12 | inDateRange |
| End Year | 2021 | int (written input.integer) · min 2000 · max 2100 | inDateRange |
| Long Open - Fast | 11 | int · step 1 | ema_long_open_fast |
| Long Open - Slow | 33 | int · step 1 | ema_long_open_slow |
| Long Close - Fast | 3 | int · step 1 | ema_long_close_fast |
| Long Close - Slow | 40 | int · step 1 | ema_long_close_slow |
| Short Open - Fast | 5 | int · step 1 | ema_short_open_fast |
| Short Open - Slow | 11 | int · step 1 | ema_short_open_slow |
| Short Close - Fast | 4 | int · step 1 | ema_short_close_fast |
| Short Close - Slow | 7 | int · step 1 | ema_short_close_slow |
| Long Stop Loss (%) | 1.7 | float · min 0.0 · step 0.1 | long_stop_price |
| Short Stop Loss (%) | 0.4 | float · min 0.0 · step 0.1 | short_stop_price |
| Long-term trend - Longs | 144 | int · step 1 | lttl |
| Long-term trend - Shorts | 89 | int · step 1 | ltts |
What it calculates
- EMA (
ema,ta.emain v5)close, Long Open - Fast (11)ema_long_open_fast · line 28 - EMA (
ema,ta.emain v5)close, Long Open - Slow (33)ema_long_open_slow · line 29 - EMA (
ema,ta.emain v5)close, Long Close - Fast (3)ema_long_close_fast · line 30 - EMA (
ema,ta.emain v5)close, Long Close - Slow (40)ema_long_close_slow · line 31 - EMA (
ema,ta.emain v5)close, Short Open - Fast (5)ema_short_open_fast · line 38 - EMA (
ema,ta.emain v5)close, Short Open - Slow (11)ema_short_open_slow · line 39 - EMA (
ema,ta.emain v5)close, Short Close - Fast (4)ema_short_close_fast · line 40 - EMA (
ema,ta.emain v5)close, Short Close - Slow (7)ema_short_close_slow · line 41 - Crossover (
crossover,ta.crossoverin v5)ema_long_open_fast, ema_long_open_slowopenlong · line 44 - Crossover (
crossover,ta.crossoverin v5)ema_long_close_slow, ema_long_close_fastcloselong · line 45 - Crossover (
crossover,ta.crossoverin v5)ema_short_open_slow, ema_short_open_fastopenshort · line 50 - Crossover (
crossover,ta.crossoverin v5)ema_short_close_fast, ema_short_close_slowcloseshort · line 51 - EMA (
ema,ta.emain v5)close, Long-term trend - Longs (144)lttl · line 69 - EMA (
ema,ta.emain v5)close, Long-term trend - Shorts (89)ltts · line 73
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 OL long | (inDateRange and openlong and (close > lttl)) | On | 78 |
| Close OL | (inDateRange and closelong) | On | 80 |
| Exit L-SL stop | strategy.position_size > 0 | On | 82 |
| Open OS short | (inDateRange and openshort and (close < ltts)) | On | 85 |
| Close OS | (inDateRange and closeshort) | On | 87 |
| Exit S-SL stop | strategy.position_size < 0 | On | 89 |
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.
- 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.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 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.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 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 |
|---|---|---|---|---|---|
| EMA crossover - BTC-USD 4hr this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| EMA Slope AyE by fajardoarelys | ema | v4 strategy | No trades on this market | ||
| ROoT by realisticDove62527 | ema | v5 strategy | −4.7% | 225 | 9.4% |
| sachin5986 by mohitesachin78 | ema | v4 strategy | +32.1% | 35 | 17.6% |
| sonu1997 by Sonu1997 | ema | v4 strategy | +960.7% | 2 | 1,103.8% |
| Moving Average Crossover by Muh4mm4d | ema | v4 strategy | +19,990.1% | 30 | 1,274.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 22 ms | With a workaround 1,000 bars in 73 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true, long = false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 78, 85) 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. 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/// © str1nger//@version=4strategy(title="BTC - 4hr - Long/Short", shorttitle="BTC - 4hr - Long/Short", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=75,commission_type=strategy.commission.percent, commission_value=0.075)//////<---Uses a percentage of starting equity//DATE RANGE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////startDate = input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31)startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12)startYear = input(title="Start Year", type=input.integer, defval=2020, minval=2000, maxval=2100)endDate = input(title="End Date", type=input.integer, defval=1, minval=1, maxval=31)endMonth = input(title="End Month", type=input.integer, defval=12, minval=1, maxval=12)endYear = input(title="End Year", type=input.integer, defval=2021, minval=2000, maxval=2100)inDateRange = (time >= timestamp("Europe/London", startYear, startMonth, startDate, 0, 0)) and (time < timestamp("Europe/London", endYear, endMonth, endDate, 0, 0))//EMAs////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LONG//11,33,3,40lof= input(11, title="Long Open - Fast", step=1)los= input(33, title="Long Open - Slow", step=1)lcf= input(3, title="Long Close - Fast", step=1)lcs= input(40, title="Long Close - Slow", step=1)ema_long_open_fast = ema(close, lof)ema_long_open_slow = ema(close, los)ema_long_close_fast= ema(close, lcf)ema_long_close_slow = ema(close, lcs)//SHORT//5,11,4,7sof= input(5, title="Short Open - Fast", step=1)sos= input(11, title="Short Open - Slow", step=1)scf= input(4, title="Short Close - Fast", step=1)scs= input(7, title="Short Close - Slow", step=1)ema_short_open_fast = ema(close, sof)ema_short_open_slow = ema(close, sos)ema_short_close_fast = ema(close, scf)ema_short_close_slow = ema(close, scs)//CONDITIONS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LONGopenlong = crossover(ema_long_open_fast, ema_long_open_slow)closelong = crossover(ema_long_close_slow, ema_long_close_fast)//1.7%long_loss_percent = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1.7) * 0.01long_stop_price = strategy.position_avg_price * (1 - long_loss_percent)//SHORTopenshort = crossover(ema_short_open_slow, ema_short_open_fast)closeshort = crossover(ema_short_close_fast, ema_short_close_slow)//0.4%short_loss_percent = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=0.4) * 0.01short_stop_price = strategy.position_avg_price * (1 + short_loss_percent)//PLOT EMAs//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LONGplot(ema_long_open_fast, "Long EMA open lower", linewidth=1, color=color.green)plot(ema_long_open_slow, "Long EMA close upper", linewidth=1, color=color.green)plot(ema_long_close_fast, "Long close lower", linewidth=1, color=color.red)plot(ema_long_close_slow, "Long close upper", linewidth=1, color=color.red)//SHORTplot(ema_short_open_fast, "Short open fast", linewidth=1, color=color.green)plot(ema_short_open_slow, "Short open slow", linewidth=1, color=color.green)plot(ema_short_close_fast, "Short close fast", linewidth=1, color=color.red)plot(ema_short_close_slow, "Short close slow", linewidth=1, color=color.red)//LONG-TERM TRENDS//LONG 144long_term_trend_longs= input(144, title="Long-term trend - Longs", step=1)lttl= ema(close, long_term_trend_longs)plot(lttl, "Long-term trend - Longs", linewidth=2, color=color.blue)//SHORT 89long_term_trend_shorts= input(89, title="Long-term trend - Shorts", step=1)ltts = ema(close, long_term_trend_shorts)plot(ltts, "Long-term trend - Shorts", linewidth=2, color=color.blue)//STRATEGY////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LONGif (inDateRange and openlong and (close > lttl)) strategy.entry("OL", long=true, comment="##insert open long comment here##")if (inDateRange and closelong) strategy.close("OL", comment="##insert close long comment here##")if strategy.position_size > 0 strategy.exit("L-SL", stop=long_stop_price, comment="##insert long stop-loss comment here##")//SHORT if (inDateRange and openshort and (close < ltts)) strategy.entry("OS", long=false, comment="##insert open short comment here##")if (inDateRange and closeshort) strategy.close("OS", comment="##insert close short comment here##")if strategy.position_size < 0 strategy.exit("S-SL", stop=short_stop_price, comment="##inster short stop-loss comment here##")