Pine Script v4 · Strategy
4-Hour Stochastic EMA Trend
A Pine Script v4 strategy built on EMA (close, period1), SMA or Stoch (smoothK) and SMA (k, periodD). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. It has no settings.
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 4-Hour Stochastic EMA Trend repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did 4-Hour Stochastic EMA Trend 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; it opened a long position that was still open on the last bar, so there is no result to report. On BTCUSDT 4h it did trade; that row of the markets table below has its figures. A backtest, not investment advice.
- Can 4-Hour Stochastic EMA Trend 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 4-Hour Stochastic EMA Trend use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does 4-Hour Stochastic EMA Trend run in Zeal Terminal?
- Yes
- It compiles in 9 ms and runs 1,000 bars in 46 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- EMA period 5
- EMA period 15
- EMA period 50
- d
- k
- 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
- 60,794.80%
- −$60,794,800.00
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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
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.
-
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 19, 20. 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, stoch → ta.stoch, crossover → ta.crossover, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 19, 20). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 1 trades and the same P&L on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 9: ema → ta.ema
- Line 10: ema → ta.ema
- Line 11: ema → ta.ema
- Line 15: sma → ta.sma
- Line 15: stoch → ta.stoch
7 more edits
- Line 16: sma → ta.sma
- Line 17: crossover → ta.crossover
- Line 17: crossover → ta.crossover
- Line 17: crossover → ta.crossover
- Line 18: crossunder → ta.crossunder
- Line 18: crossunder → ta.crossunder
- Line 18: crossunder → ta.crossunder
⋯// © slymnturkoglu-//@version=4+//@version=5strategy("Stochastic")⋯period3 = 50-ma1 = ema(close, period1)+ma1 = ta.ema(close, period1)-ma2 = ema(close, period2)+ma2 = ta.ema(close, period2)-ma3 = ema(close, period3)+ma3 = ta.ema(close, period3)periodK=13⋯smoothK=5-k = sma(stoch(close, high, low, periodK), smoothK)+k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)-d = sma(k, periodD)+d = ta.sma(k, periodD)-buyCondition = crossover(k, d) and crossover(ma1, ma3) and crossover(ma2, ma3)+buyCondition = ta.crossover(k, d) and ta.crossover(ma1, ma3) and ta.crossover(ma2, ma3)-sellCondition = crossunder(k, d) and crossunder(ma1, ma3) and crossunder(ma2, ma3)+sellCondition = ta.crossunder(k, d) and ta.crossunder(ma1, ma3) and ta.crossunder(ma2, ma3)strategy.entry("long", strategy.long, 1000, alert_message="LongAlert", when=buyCondition)⋯Same 1 trades and the same P&L on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine.
- Line 19: when = buyCondition → if block
- Line 20: when = sellCondition → if block
⋯sellCondition = ta.crossunder(k, d) and ta.crossunder(ma1, ma3) and ta.crossunder(ma2, ma3)-strategy.entry("long", strategy.long, 1000, alert_message="LongAlert", when=buyCondition)+if buyCondition+ strategy.entry("long", strategy.long, 1000, alert_message="LongAlert")-strategy.close("long", alert_message="CloseAlert", when=sellCondition)+if sellCondition+ strategy.close("long", alert_message="CloseAlert") ⋯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: 1 before, 0 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.
⋯// © slymnturkoglu-//@version=5+//@version=6strategy("Stochastic")⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
The script declares no settings.
What it calculates
- EMA (
ema,ta.emain v5)close, period1ma1 · line 9 - EMA (
ema,ta.emain v5)close, period2ma2 · line 10 - EMA (
ema,ta.emain v5)close, period3ma3 · line 11 - SMA / Stoch (
sma,ta.smain v5)stoch(close, high, low, periodK), smoothKk · line 15 - SMA (
sma,ta.smain v5)k, periodDd · line 16
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 long long | buyConditioncrossover(k, d) and crossover(ma1, ma3) and crossover(ma2, ma3) | On | 19 |
| Close long | sellConditioncrossunder(k, d) and crossunder(ma1, ma3) and crossunder(ma2, ma3) | On | 20 |
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% | — | 60,794.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −988.1% | +5.5% | 1 | 0% | 0.00 | 2,206.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 1,510.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 198.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 105.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% | — | 91.9% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 2.7% | 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 |
|---|---|---|---|---|---|
| 4-Hour Stochastic EMA Trend this page | — | v4 strategy | 0.0% | 0 | 60,794.8% |
| Stochastic Moving Average by LucasVivien | stoch sma ema | v5 strategy | +2.8% | 7 | 4.4% |
| OTT-Stoch-TP-SL by BigCoinHunter | ema sma stoch | v5 strategy | No trades on this market | ||
| Webhook Starter Kit [HullBuster] by tradingtudi | ema stoch sma | v4 strategy | does not run in Zeal Terminal | ||
| 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% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | With a workaround 1,000 bars in 46 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/// © slymnturkoglu//@version=4strategy("Stochastic")//study(title="Stochastic", shorttitle="Stoch", format=format.price, precision=2, resolution="")period1 = 5period2 = 15period3 = 50ma1 = ema(close, period1)ma2 = ema(close, period2)ma3 = ema(close, period3)periodK=13periodD=15smoothK=5k = sma(stoch(close, high, low, periodK), smoothK)d = sma(k, periodD)buyCondition = crossover(k, d) and crossover(ma1, ma3) and crossover(ma2, ma3)sellCondition = crossunder(k, d) and crossunder(ma1, ma3) and crossunder(ma2, ma3)strategy.entry("long", strategy.long, 1000, alert_message="LongAlert", when=buyCondition)strategy.close("long", alert_message="CloseAlert", when=sellCondition) //study("Stochastic EMA Trend", overlay=false)plot(close)plot(ma1, color=color.blue, linewidth=3, title="EMA period 5")plot(ma2, color=color.green,linewidth=3, title="EMA period 15")plot(ma3, color=color.yellow,linewidth=3, title="EMA period 50")plot(d, color=color.red,linewidth=3, title="d")plot(k, color=color.blue,linewidth=3, title="k")