Pine Script v4 · Strategy
First Script, buy-sell on EMA
A Pine Script v4 strategy built on EMA (close, 12) and SMA (macd, 9). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays no commission. 5 settings; defaults include Fast Length 12, Slow Length 26 and Signal Length 9.
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 First Script, buy-sell on EMA repaint?
- No
- No repainting patterns were found in the source.
- Can First Script, buy-sell on EMA be converted to Pine Script v5?
- Not tested
- The steps are shown below, but the original does not run in Zeal Terminal's engine, so they could not be checked.
- Does First Script, buy-sell on EMA use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does First Script, buy-sell on EMA run in Zeal Terminal?
- No
- Not yet: Pine Script v4 Error at @L82:C21: Undeclared identifier 'updatetime'.
Review
What a careful reader of this script would point out, found by checking the source.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
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
3 settings have no label
In the settings dialog fastLength (line 5), slowLength (line 5), signalLength (line 6) 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 117, 120. 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, sma → ta.sma, abs → math.abs, tickerid → ticker.new, security → request.security.
- 2 strategy calls use the when = argument (line 117, 120). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
Not verifiedThe v4 script does not run in Zeal Terminal's engine (Pine Script v4 Error at @L82:C21: Undeclared identifier 'updatetime'.), so this step could not be checked.
- Line 3: //@version=4 → //@version=5
- Line 8: ema → ta.ema
- Line 9: ema → ta.ema
- Line 11: sma → ta.sma
- Line 15: ema → ta.ema
- Line 16: ema → ta.ema
15 more edits
- Line 18: sma → ta.sma
- Line 24: ema → ta.ema
- Line 25: ema → ta.ema
- Line 27: sma → ta.sma
- Line 34: abs → math.abs
- Line 35: ema → ta.ema
- Line 36: ema → ta.ema
- Line 38: sma → ta.sma
- Line 74: tickerid → ticker.new
- Line 77: security → request.security
- Line 78: security → request.security
- Line 79: security → request.security
- Line 80: security → request.security
- Line 82: security → request.security
- Line 100: ema → ta.ema
⋯// © dejoski-//@version=4+//@version=5strategy(title="My Strategy ", default_qty_type = strategy.percent_of_equity,overlay=true, default_qty_value=100)⋯// fastLength = 12, signalLength = 26-fastMA = ema(close, fastLength)+fastMA = ta.ema(close, fastLength)-slowMA = ema(close, slowLength)+slowMA = ta.ema(close, slowLength)macd = fastMA - slowMA-signal = sma(macd, signalLength)+signal = ta.sma(macd, signalLength)hist = macd - signal⋯// plot(rsi(open,7))-fastMA2 = ema(close[1], fastLength)+fastMA2 = ta.ema(close[1], fastLength)-slowMA2 = ema(close[1], slowLength)+slowMA2 = ta.ema(close[1], slowLength)macd2 = fastMA2 - slowMA2-signal2 = sma(macd2, signalLength)+signal2 = ta.sma(macd2, signalLength)hist2 = macd2 - signal2⋯// plot(histdif)-fastMA3 = ema(close[2], fastLength)+fastMA3 = ta.ema(close[2], fastLength)-slowMA3 = ema(close[2], slowLength)+slowMA3 = ta.ema(close[2], slowLength)macd3 = fastMA3 - slowMA3-signal3 = sma(macd3, signalLength)+signal3 = ta.sma(macd3, signalLength)hist3 = macd3 - signal3⋯hist_momentum = histdif+histdif2-momentum = (abs(hist_momentum)/(open/10))+momentum = (math.abs(hist_momentum)/(open/10))-fastMA7 = ema(close[10], fastLength)+fastMA7 = ta.ema(close[10], fastLength)-slowMA7 = ema(close[10], slowLength)+slowMA7 = ta.ema(close[10], slowLength)macd7 = fastMA7 - slowMA7-signal7 = sma(macd7, signalLength)+signal7 = ta.sma(macd7, signalLength)hist7 = macd7 - signal7⋯esd = "EARNINGS"-t = tickerid(prefix = "ESD", ticker=syminfo.prefix + ";" + syminfo.ticker + ";" + esd)+t = ticker.new(prefix = "ESD", ticker=syminfo.prefix + ";" + syminfo.ticker + ";" + esd)prefix = syminfo.prefix //input("NASDAQ", "Exchange Prefix")tick = syminfo.ticker // input("TSLA", "Ticker")-earning = security("ESD_FACTSET:" + prefix + ";" + tick + ";EARNINGS", "D", close, true)+earning = request.security("ESD_FACTSET:" + prefix + ";" + tick + ";EARNINGS", "D", close, true)-c = security(t, "D", close, true)+c = request.security(t, "D", close, true)-o = security(t, "D", open, true)+o = request.security(t, "D", open, true)-h = security(t, "D", high, true)+h = request.security(t, "D", high, true)// h2 = security(t, "D", high[1], true, lookahead = barmerge.lookahead_on)-u = security(t, "D", updatetime, true)+u = request.security(t, "D", updatetime, true)// c - earning⋯src = close-out = ema(src, len)+out = ta.ema(src, len)up = out > out[1]⋯Clean v5
Not verifiedThe v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L82:C29: Undeclared identifier 'updatetime'.), so this step could not be checked.
- Line 5: input() → input.int()
- Line 5: input() → input.int()
- Line 6: input() → input.int()
- Line 97: input() → input.bool()
- Line 98: input() → input.int()
- Line 117: when = strategy.position_size == 0 → if block
- Line 120: when = strategy.position_size == 0 → if block
⋯strategy(title="My Strategy ", default_qty_type = strategy.percent_of_equity,overlay=true, default_qty_value=100)-fastLength = input(12, minval=1), slowLength=input(26,minval=1)+fastLength = input.int(12, minval=1), slowLength=input.int(26,minval=1)-signalLength=input(9,minval=1)+signalLength=input.int(9,minval=1)// fastLength = 12, signalLength = 26⋯// plotchar(t_minus>=max-172800?true:na, location=location.absolute, color=color.red, size=size.tiny)-emaplot = input (true, title="Show EMA on chart")+emaplot = input.bool (true, title="Show EMA on chart")-len = input(8, minval=1, title="ema Length")+len = input.int(8, minval=1, title="ema Length")src = close⋯ strategy.close("short")- strategy.entry("long", strategy.long, when = strategy.position_size == 0 )+ if strategy.position_size == 0+ strategy.entry("long", strategy.long )if (not buy and not earnings_soon) strategy.close("long")- strategy.entry("short", strategy.short, when = strategy.position_size == 0 )+ if strategy.position_size == 0+ strategy.entry("short", strategy.short )if (earnings_soon)⋯Does it run in Pine Script v6?
Not tested
The v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L82:C29: Undeclared identifier 'updatetime'.), so this step could not be checked. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © dejoski-//@version=5+//@version=6strategy(title="My Strategy ", default_qty_type = strategy.percent_of_equity,overlay=true, default_qty_value=100)⋯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 |
|---|---|---|---|
| Fast Length (no label in the script) | 12 | int · min 1 | fastMA fastMA2 fastMA3 fastMA7 |
| Slow Length (no label in the script) | 26 | int · min 1 | slowMA slowMA2 slowMA3 slowMA7 |
| Signal Length (no label in the script) | 9 | int · min 1 | signal signal2 signal3 signal7 |
| Show EMA on chart | true | bool | EMA |
| ema Length | 8 | int · min 1 | out |
What it calculates
- EMA (
ema,ta.emain v5)close, Fast Length (12)fastMA · line 8 - EMA (
ema,ta.emain v5)close, Slow Length (26)slowMA · line 9 - SMA (
sma,ta.smain v5)macd, Signal Length (9)signal · line 11 - EMA (
ema,ta.emain v5)close[1], Fast Length (12)fastMA2 · line 15 - EMA (
ema,ta.emain v5)close[1], Slow Length (26)slowMA2 · line 16 - SMA (
sma,ta.smain v5)macd2, Signal Length (9)signal2 · line 18 - EMA (
ema,ta.emain v5)close[2], Fast Length (12)fastMA3 · line 24 - EMA (
ema,ta.emain v5)close[2], Slow Length (26)slowMA3 · line 25 - SMA (
sma,ta.smain v5)macd3, Signal Length (9)signal3 · line 27 - EMA (
ema,ta.emain v5)close[10], Fast Length (12)fastMA7 · line 35 - EMA (
ema,ta.emain v5)close[10], Slow Length (26)slowMA7 · line 36 - SMA (
sma,ta.smain v5)macd7, Signal Length (9)signal7 · line 38 - EMA (
ema,ta.emain v5)src, ema Length (8)out · line 100
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 |
|---|---|---|---|
| Close short | (buy and not earnings_soon) | On | 116 |
| Open long long | strategy.position_size == 0 | On | 117 |
| Close long | (not buy and not earnings_soon) | On | 119 |
| Open short short | strategy.position_size == 0 | On | 120 |
| Close short | (earnings_soon) | On | 122 |
| Close long | (earnings_soon) | On | 123 |
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.
The live replay needs a run, and this script does not run in Zeal Terminal yet; the answer comes from the checks on the source alone.
- ClearHigher-timeframe data5 security call(s), none with lookahead on.
- 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.
Similar scripts
Indicators and strategies built from the same calculations.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | No Pine Script v4 Error at @L82:C21: Undeclared identifier 'updatetime'. | Not measured | |
| PineTS | No v5 and v6 only | — | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © dejoski//@version=4strategy(title="My Strategy ", default_qty_type = strategy.percent_of_equity,overlay=true, default_qty_value=100)fastLength = input(12, minval=1), slowLength=input(26,minval=1)signalLength=input(9,minval=1)// fastLength = 12, signalLength = 26fastMA = ema(close, fastLength)slowMA = ema(close, slowLength)macd = fastMA - slowMAsignal = sma(macd, signalLength)hist = macd - signalpricedif7 = close-close[15]// plot(rsi(open,7))fastMA2 = ema(close[1], fastLength)slowMA2 = ema(close[1], slowLength)macd2 = fastMA2 - slowMA2signal2 = sma(macd2, signalLength)hist2 = macd2 - signal2// outSignal = security(tickerid, res, signal)sigdif = signal-signal2histdif = hist - hist2// plot(histdif)fastMA3 = ema(close[2], fastLength)slowMA3 = ema(close[2], slowLength)macd3 = fastMA3 - slowMA3signal3 = sma(macd3, signalLength)hist3 = macd3 - signal3// outSignal = security(tickerid, res, signal)sigdif2 = signal-signal3histdif2 = hist2 - hist3// plot(histdif2)hist_momentum = histdif+histdif2momentum = (abs(hist_momentum)/(open/10))fastMA7 = ema(close[10], fastLength)slowMA7 = ema(close[10], slowLength)macd7 = fastMA7 - slowMA7signal7 = sma(macd7, signalLength)hist7 = macd7 - signal7// outSignal = security(tickerid, res, signal)sigdif7 = signal-signal7histdif7 = hist - hist7// plot(histdif7)// plot(outSignal[1])// percent_red = (5+hist)/(5+(signal))// macd = fastMA - slowMA// signal = sma(macd, 9)//these were on// plot(pricedif7)// plot(histdif, color=color.green)// plot(histdif2, color=color.green)// plot(hist_momentum, color=color.orange)// plot((momentum), color=color.orange)// plot(sigdif7)// plot(macd, color=color.blue)// plot(signal, color=color.orange)// plot(hist, color=color.red)// plot(percent_red, color=color.green)GoodMacd = falsemadc_threshold = 0.5sell_threshold = -0.5if (signal>=madc_threshold) GoodMacd := trueif (signal<madc_threshold) GoodMacd := false // t = "ESD:" + ticker + "_EARNINGS"// esti = security(t, "D", open, true)// earn = security(t, "D", close, true)// plot(esti, style=circles, linewidth=4)// plot(earn, style=cross, linewidth=4)// longCondition = crossover(sma(close, 14), sma(close, 28))esd = "EARNINGS"t = tickerid(prefix = "ESD", ticker=syminfo.prefix + ";" + syminfo.ticker + ";" + esd)prefix = syminfo.prefix //input("NASDAQ", "Exchange Prefix")tick = syminfo.ticker // input("TSLA", "Ticker")earning = security("ESD_FACTSET:" + prefix + ";" + tick + ";EARNINGS", "D", close, true)c = security(t, "D", close, true)o = security(t, "D", open, true)h = security(t, "D", high, true)// h2 = security(t, "D", high[1], true, lookahead = barmerge.lookahead_on)u = security(t, "D", updatetime, true)// c - earning// o - estimate// h - date (in unix time)// u - period ending (in unix time)// last_c = c// last_c := c?c:nz(last_c[1])last_earnings_time = hlast_earnings_time := h?h:last_earnings_time[1]t_minus = time/1000-last_earnings_timemax = 0.0max := t_minus>nz(max[1]) or max[1]>22820200.00?t_minus:nz(max[1])// plotchar(max, "check", "", location = location.top)// plotchar(c, location=location.absolute, color=color.green, size=size.tiny)// plotchar(t_minus>=max-172800?true:na, location=location.absolute, color=color.red, size=size.tiny)emaplot = input (true, title="Show EMA on chart")len = input(8, minval=1, title="ema Length")src = closeout = ema(src, len)up = out > out[1]down = out < out[1]buy = up ? true : down ? false : falsemycolor = up ? color.green : down ? color.red : color.blueplot(out and emaplot ? out :na, title="EMA", color=mycolor, linewidth=3)// if (GoodMacd and (macd >= signal))// if (GoodMacd and hist> 0)sigdif_thresh = 0.0days = 7days_in_sec = days*86400time_before_earnings = days_in_secearnings_soon = t_minus>=(max-time_before_earnings)?true:na// plotshape(earnings_soon)plotchar(earnings_soon,"Exit all, earnings soon!", location=location.bottom, color=color.orange, size=size.tiny)if (buy and not earnings_soon) strategy.close("short") strategy.entry("long", strategy.long, when = strategy.position_size == 0 )if (not buy and not earnings_soon) strategy.close("long") strategy.entry("short", strategy.short, when = strategy.position_size == 0 )if (earnings_soon) strategy.close("short") strategy.close("long")