Pine Script v4 · Strategy
DEMA-EMA & VOLATILITY (VAMS)
A Pine Script v4 strategy built on EMA (close, 10), ATR (14) and Crossunder (dema1, ema1). It has 3 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade is 10000 contracts and pays no commission. 18 settings; defaults include DEMA LENGTH 10, Source close and EMA Length 25.
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 DEMA-EMA & VOLATILITY (VAMS) repaint?
- No
- No repainting patterns were found in the source.
- Can DEMA-EMA & VOLATILITY (VAMS) 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 DEMA-EMA & VOLATILITY (VAMS) use a stop loss?
- Yes
- strategy.exit sets a stop on line 68.
- Does DEMA-EMA & VOLATILITY (VAMS) run in Zeal Terminal?
- Yes
- It compiles in 14 ms and runs 1,000 bars in 71 ms, in the browser.
BTCUSDT · 1D · default settings
- DEMA
- EMA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +2,833,450.00%
- $283,345,000.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 19
- 6 won · 13 lost
- Profitable
- 31.6%
- of closed trades
- Profit factor
- 1.85
- gross profit ÷ gross loss
- Max drawdown
- 15,721.00%
- −$223,600,000.00
One run with the author's default settings, $10,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
7 settings do nothing
“Show Moving Average?”, “From Month”, “From Day”, “From Year”, “To Month”, “To Day”, “To Year” are declared but never read.
-
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 61, 66. The upgraded version below moves each call inside an if block.
-
Note
Uses long =, which v5 renamed line 61
long = on line 61. 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, security → request.security, atr → ta.atr, sma → ta.sma, crossover → ta.crossover, crossunder → ta.crossunder, max → math.max.
- 2 strategy calls use the when = argument (line 61, 66). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 19 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 8: ema → ta.ema
- Line 9: ema → ta.ema
- Line 15: input(type = input.integer) → input.int()
- Line 16: ema → ta.ema
- Line 19: input(type = input.resolution) → input.timeframe()
13 more edits
- Line 20: input(type = input.integer) → input.int()
- Line 21: input(type = input.bool) → input.bool()
- Line 26: input(type = input.float) → input.float()
- Line 28: input(type = input.float) → input.float()
- Line 38: security → request.security
- Line 38: atr → ta.atr
- Line 42: ema → ta.ema
- Line 42: sma → ta.sma
- Line 43: security → request.security
- Line 45: crossover → ta.crossover
- Line 47: crossunder → ta.crossunder
- Line 55: max → math.max
- Line 61: long = strategy.long → direction = strategy.long
⋯// © Qorbanjf-//@version=4+//@version=5strategy("ORIGIN DEMA/EMA & VOL LONG ONLY", shorttitle="ORIGIN DEMA/EMA & VOL LONG", overlay=true, initial_capital=10000, default_qty_value=10000)⋯src = input(close, title="Source")-e1 = ema(src, length)+e1 = ta.ema(src, length)-e2 = ema(e1, length)+e2 = ta.ema(e1, length)dema1 = 2 * e1 - e2⋯srb = input(close, title="Source")-offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)+offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)-ema1 = ema(srb, len)+ema1 = ta.ema(srb, len)plot(ema1, title="EMA", color=color.blue, offset=offset)// Inputs-atrTimeFrame = input("D", title="ATR Timeframe", type=input.resolution)+atrTimeFrame = input.timeframe("D", title="ATR Timeframe")-atrLookback = input(defval=14,title="ATR Lookback Period",type=input.integer)+atrLookback = input.int(defval=14,title="ATR Lookback Period")-useMA = input(title = "Show Moving Average?", type = input.bool, defval = true)+useMA = input.bool(title = "Show Moving Average?", defval = true)maType = input(defval="EMA", options=["EMA", "SMA"], title = "Moving Average Type")⋯ // type=input.float, minval=0.0, step=0.1, defval=1) * 0.01-longTrailPerc = input(title="Trail stop loss (%)",- type=input.float, minval=0.0, step=0.1, defval=50) * 0.01+longTrailPerc = input.float(title="Trail stop loss (%)", minval=0.0, step=0.1, defval=50) * 0.01-longProfitPerc = input(title="Long Take Profit (%)",- type=input.float, minval=0.0, step=0.1, defval=3000) / 100+longProfitPerc = input.float(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=3000) / 100// === INPUT BACKTEST RANGE ===⋯// ATR Logic // atrValue = atr(atrLookback) // atrp = (atrValue/close)*100 // plot(atrp, color=color.white, linewidth=2, transp = 30)-atrValue = security(syminfo.tickerid, atrTimeFrame, atr(atrLookback))+atrValue = request.security(syminfo.tickerid, atrTimeFrame, ta.atr(atrLookback))atrp = (atrValue/close)*100⋯ma(maType, src, length) =>- maType == "EMA" ? ema(src, length) : sma(src, length) //Ternary Operator (if maType equals EMA, then do ema calc, else do sma calc)+ maType == "EMA" ? ta.ema(src, length) : ta.sma(src, length) //Ternary Operator (if maType equals EMA, then do ema calc, else do sma calc)-maFilter = security(syminfo.tickerid, atrTimeFrame, ma(maType, atrp, maLength))+maFilter = request.security(syminfo.tickerid, atrTimeFrame, ma(maType, atrp, maLength))// variables for enter position-enterLong = crossover(dema1, ema1) and atrp < maFilter+enterLong = ta.crossover(dema1, ema1) and atrp < maFilter// variables for exit position-sale = crossunder(dema1, ema1)+sale = ta.crossunder(dema1, ema1)// stop loss⋯ stopValue = close * (1 - longTrailPerc)- max(stopValue, longStopTrail[1])+ math.max(stopValue, longStopTrail[1])else⋯strategy.entry(id="long",- long=strategy.long,+ direction = strategy.long, when=enterLong,⋯Same 19 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 7: input() → input.source()
- Line 13: input() → input.int()
- Line 14: input() → input.source()
- Line 22: input() → input.string()
- Line 23: input() → input.int()
8 more edits
- Line 29: input() → input.int()
- Line 30: input() → input.int()
- Line 31: input() → input.int()
- Line 32: input() → input.int()
- Line 33: input() → input.int()
- Line 34: input() → input.int()
- Line 59: when = enterLong → if block
- Line 64: when = sale → if block
⋯// DEMA-length = input(10, minval=1, title="DEMA LENGTH")+length = input.int(10, minval=1, title="DEMA LENGTH")-src = input(close, title="Source")+src = input.source(close, title="Source")e1 = ta.ema(src, length)⋯//EMA-len = input(25, minval=1, title="EMA Length")+len = input.int(25, minval=1, title="EMA Length")-srb = input(close, title="Source")+srb = input.source(close, title="Source")offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)⋯useMA = input.bool(title = "Show Moving Average?", defval = true)-maType = input(defval="EMA", options=["EMA", "SMA"], title = "Moving Average Type")+maType = input.string(defval="EMA", options=["EMA", "SMA"], title = "Moving Average Type")-maLength = input(defval = 20, title = "Moving Average Period", minval = 1)+maLength = input.int(defval = 20, title = "Moving Average Period", minval = 1)//longLossPerc = input(title="Long Stop Loss (%)",⋯// === INPUT BACKTEST RANGE ===-FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)+FromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)-FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)+FromDay = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)-FromYear = input(defval = 2017, title = "From Year", minval = 2000)+FromYear = input.int(defval = 2017, title = "From Year", minval = 2000)-ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)+ToMonth = input.int(defval = 1, title = "To Month", minval = 1, maxval = 12)-ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)+ToDay = input.int(defval = 1, title = "To Day", minval = 1, maxval = 31)-ToYear = input(defval = 9999, title = "To Year", minval = 2017)+ToYear = input.int(defval = 9999, title = "To Year", minval = 2017)// ATR Logic // atrValue = atr(atrLookback) // atrp = (atrValue/close)*100 // plot(atrp, color=color.white, linewidth=2, transp = 30)⋯//Enter trades when conditions are met-strategy.entry(id="long",- direction = strategy.long,- when=enterLong,- comment="long")+if enterLong+ strategy.entry(id="long",+ direction = strategy.long,+ comment="long")//-strategy.close("long", when = sale, comment = "Sell")+if sale+ strategy.close("long", comment = "Sell")//place exit orders (only executed after trades are active)⋯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: 19 before, 0 after on BTCUSDT 1D, 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.
⋯// © Qorbanjf-//@version=5+//@version=6strategy("ORIGIN DEMA/EMA & VOL LONG ONLY", shorttitle="ORIGIN DEMA/EMA & VOL LONG", overlay=true, initial_capital=10000, default_qty_value=10000)⋯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 |
|---|---|---|---|
| DEMA LENGTH | 10 | int · min 1 | e1 e2 line 42 |
| Source | close | source | e1 line 42 |
| EMA Length | 25 | int · min 1 | ema1 |
| Source | close | source | ema1 |
| Offset | 0 | int (written input.integer) · min -500 · max 500 | EMA |
| ATR Timeframe | D | timeframe (written input.resolution) | atrValue maFilter |
| ATR Lookback Period | 14 | int (written input.integer) | atrValue |
| Show Moving Average? | true | bool | unused |
| Moving Average Type | EMA | string · options ["EMA", "SMA"] | maFilter line 42 |
| Moving Average Period | 20 | int · min 1 | maFilter |
| Trail stop loss (%) | 50 | float · min 0.0 · step 0.1 | longStopTrail stopValue |
| Long Take Profit (%) | 3000 | float · min 0.0 · step 0.1 | longExitPrice |
| From Month | 1 | int · min 1 · max 12 | unused |
| From Day | 1 | int · min 1 · max 31 | unused |
| From Year | 2017 | int · min 2000 | unused |
| To Month | 1 | int · min 1 · max 12 | unused |
| To Day | 1 | int · min 1 · max 31 | unused |
| To Year | 9999 | int · min 2017 | unused |
What it calculates
- EMA (
ema,ta.emain v5)Source (close), DEMA LENGTH (10)e1 · line 8 - EMA (
ema,ta.emain v5)e1, DEMA LENGTH (10)e2 · line 9 - EMA (
ema,ta.emain v5)Source (close), EMA Length (25)ema1 · line 16 - ATR (
atr,ta.atrin v5)ATR Lookback Period (14)atrValue · line 38 - Crossunder (
crossunder,ta.crossunderin v5)dema1, ema1sale · line 47
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 | enterLongcrossover(dema1, ema1) and atrp < maFilter | On | 61 |
| Close long | sale | On | 66 |
| Exit sell stop and limit | every bar | On | 68 |
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 script reads request.security() at the chart's own timeframe, which Zeal Terminal's engine answers one bar late in live mode but not in history, so this replay cannot tell repainting from that engine issue.
- ClearHigher-timeframe data2 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.
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 | +2,833,450.0% | +102.5% | 19 | 32% | 1.85 | 15,721.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +358,802.0% | +5.5% | 21 | 19% | 1.20 | 1,136,090.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −15,372.0% | +9.8% | 17 | 24% | 0.95 | 190,007.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +7,898.0% | +8.4% | 19 | 26% | 1.14 | 12,034.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −1,865.0% | +16.7% | 19 | 26% | 0.89 | 4,928.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +3,194.0% | +21.5% | 12 | 33% | 3.26 | 846.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +39,227.0% | +148.3% | 18 | 28% | 2.59 | 17,888.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +1,130.0% | +17.1% | 20 | 30% | 1.11 | 5,254.0% | 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 | 6 | 33% | −$65,737,800.00 |
| Sideways market | 7 | 29% | $190,012,000.00 |
| Falling market | 6 | 33% | $159,071,000.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
796,021.0%
The 95th percentile of the worst drawdown when the same 19 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 434.2% to 796,021.0%; the tick is what actually happened, 577.7%. Order luck alone moves it this much.
All 19 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 17 May 2024 65,235.20 | 15 Jun 2024 66,044.00 | 29 | $8,087,800.00 |
| long | 24 Aug 2024 64,037.20 | 30 Aug 2024 59,359.00 | 6 | −$46,782,400.00 |
| long | 15 Sep 2024 59,993.00 | 10 Oct 2024 60,636.00 | 25 | $6,429,900.00 |
| long | 13 Oct 2024 63,206.20 | 24 Dec 2024 94,881.50 | 72 | $316,752,000.00 |
| long | 6 Jan 2025 98,363.60 | 10 Jan 2025 92,552.50 | 4 | −$58,111,200.00 |
| long | 16 Jan 2025 100,497.00 | 5 Feb 2025 97,763.10 | 20 | −$27,342,100.00 |
| long | 27 Mar 2025 86,909.20 | 29 Mar 2025 84,424.40 | 2 | −$24,847,900.00 |
| long | 16 Apr 2025 83,644.00 | 6 Jun 2025 101,509.00 | 51 | $178,647,000.00 |
| long | 9 Jun 2025 105,734.00 | 19 Jun 2025 104,887.00 | 10 | −$8,472,200.00 |
| long | 27 Jun 2025 106,947.00 | 3 Aug 2025 112,546.00 | 37 | $55,992,900.00 |
| long | 10 Aug 2025 116,462.00 | 20 Aug 2025 112,873.00 | 10 | −$35,893,000.00 |
| long | 12 Sep 2025 115,483.00 | 25 Sep 2025 113,307.00 | 13 | −$21,756,800.00 |
| long | 2 Oct 2025 118,595.00 | 13 Oct 2025 114,959.00 | 11 | −$36,361,800.00 |
| long | 4 Jan 2026 90,628.00 | 22 Jan 2026 89,454.70 | 18 | −$11,732,800.00 |
| long | 13 Mar 2026 70,541.30 | 23 Mar 2026 67,859.00 | 10 | −$26,823,400.00 |
| long | 25 Mar 2026 70,556.70 | 27 Mar 2026 68,820.30 | 2 | −$17,364,300.00 |
| long | 8 Apr 2026 71,924.20 | 19 May 2026 77,001.90 | 41 | $50,776,600.00 |
| long | 10 Jul 2026 63,230.00 | 1 Aug 2026 62,887.90 | 22 | −$3,421,300.00 |
| long | 8 Aug 2026 64,923.20 | 13 Aug 2026 63,480.00 | 5 | −$14,432,100.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 |
|---|---|---|---|---|---|
| DEMA-EMA & VOLATILITY (VAMS) this page | — | v4 strategy | +2,833,450.0% | 19 | 15,721.0% |
| Trend Reversal - Potential pressure by BardiaB2 | atr ema | v4 strategy | +133,960.0% | 5 | 1,504.8% |
| Simple Ema_ATR Strategy HulkTrading by TheHulkTrading | ema atr | v4 strategy | +2.2% | 14 | 15.6% |
| Keltner Breakout Strategy by Zeal Terminal | ema atr | v5 strategy | +3.5% | 21 | 2.3% |
| [EKIN] ATR Exit Strategy by ekinbasarkomur | ema atr | v5 strategy | +781.7% | 30 | 2,960.8% |
| Super Z strategy - Thanks to Rafael Zioni by 03.freeman | ema atr | v4 strategy | −404,823.0% | 56 | 2,951,790.0% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 14 ms | With a workaround 1,000 bars in 71 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = strategy.long as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 61) 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/// © Qorbanjf//@version=4strategy("ORIGIN DEMA/EMA & VOL LONG ONLY", shorttitle="ORIGIN DEMA/EMA & VOL LONG", overlay=true, initial_capital=10000, default_qty_value=10000)// DEMAlength = input(10, minval=1, title="DEMA LENGTH")src = input(close, title="Source")e1 = ema(src, length)e2 = ema(e1, length)dema1 = 2 * e1 - e2plot(dema1, "DEMA", color=color.yellow)//EMAlen = input(25, minval=1, title="EMA Length")srb = input(close, title="Source")offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)ema1 = ema(srb, len)plot(ema1, title="EMA", color=color.blue, offset=offset)// InputsatrTimeFrame = input("D", title="ATR Timeframe", type=input.resolution)atrLookback = input(defval=14,title="ATR Lookback Period",type=input.integer)useMA = input(title = "Show Moving Average?", type = input.bool, defval = true)maType = input(defval="EMA", options=["EMA", "SMA"], title = "Moving Average Type")maLength = input(defval = 20, title = "Moving Average Period", minval = 1)//longLossPerc = input(title="Long Stop Loss (%)", // type=input.float, minval=0.0, step=0.1, defval=1) * 0.01longTrailPerc = input(title="Trail stop loss (%)", type=input.float, minval=0.0, step=0.1, defval=50) * 0.01longProfitPerc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=3000) / 100// === INPUT BACKTEST RANGE ===FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)FromYear = input(defval = 2017, title = "From Year", minval = 2000)ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)ToYear = input(defval = 9999, title = "To Year", minval = 2017)// ATR Logic // atrValue = atr(atrLookback) // atrp = (atrValue/close)*100 // plot(atrp, color=color.white, linewidth=2, transp = 30)atrValue = security(syminfo.tickerid, atrTimeFrame, atr(atrLookback))atrp = (atrValue/close)*100// Moving Average Logicma(maType, src, length) => maType == "EMA" ? ema(src, length) : sma(src, length) //Ternary Operator (if maType equals EMA, then do ema calc, else do sma calc)maFilter = security(syminfo.tickerid, atrTimeFrame, ma(maType, atrp, maLength))// variables for enter positionenterLong = crossover(dema1, ema1) and atrp < maFilter// variables for exit positionsale = crossunder(dema1, ema1)// stop loss//longStopPrice = strategy.position_avg_price * (1 - longLossPerc)// trail stop// Determine trail stop loss priceslongStopTrail = 0.0longStopTrail := if (strategy.position_size > 0) stopValue = close * (1 - longTrailPerc) max(stopValue, longStopTrail[1])else 0//Take profit PercentagelongExitPrice = strategy.position_avg_price * (1 + longProfitPerc)//Enter trades when conditions are metstrategy.entry(id="long", long=strategy.long, when=enterLong, comment="long")//strategy.close("long", when = sale, comment = "Sell")//place exit orders (only executed after trades are active)strategy.exit(id="sell", limit = longExitPrice, stop = longStopTrail, comment = "SL/TP")