Pine Script v5 · Strategy
Villa Dinamic Pivot Supertrend Strategy
A Pine Script v5 strategy built on Pivothigh (2, 2), Pivotlow (2, 2) and ATR (9). 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 one contract and pays no commission. 37 settings; defaults include Stop Loss ATR Multiplier 2.5, Take Profit ATR Multiplier 4 and 2 Steps TP qty% 50.
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 Villa Dinamic Pivot Supertrend Strategy repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did Villa Dinamic Pivot Supertrend Strategy 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.
- Does Villa Dinamic Pivot Supertrend Strategy run in Pine Script v6?
- No
- The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L60:C15: cannot use 'float' as a condition — Pine Script v6 does not cast 'int' or 'float' to 'bool' implicitly. Wrap it in bool(). The v6 version is shown for reference and is not offered as a download.
- Does Villa Dinamic Pivot Supertrend Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 188, 246.
- Does Villa Dinamic Pivot Supertrend Strategy run in Zeal Terminal?
- Yes
- It compiles in 51 ms and runs 1,000 bars in 119 ms, in the browser.
BTCUSDT · 1D · default settings
- Pivothigh
- Pivotlow
- ATR
- ATR
- EMA
- EMA
- EMA
- EMA
- EMA
- SMA
- ATR
- ATR
- 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,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.
-
Likely bug
ta.atr(14) ignores the settings line 117
Line 117 hard-codes 14 where line 68 uses “PVT ST ATR Period”. Changing those settings moves one band but not this one.
ta.atr(14)ta.atr(Pd) -
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.
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 |
|---|---|---|---|
| Strategy Inputs | |||
| Use Simple EMA Filter | false | bool | ema_cond_b ema_cond_s |
| Use DEMA Angle Filter | true | bool | dema_ang_cond_b dema_ang_cond_s |
| Use DEMA Filter | true | bool | dema_cond_b dema_cond_s |
| Take Every Supertrend Signal | false | bool | st_cond_b st_cond_s |
| Stop Loss at Supertrend | true | bool | sl_b tp_b sl_s tp_s |
| 2 Steps Take Profit | false | bool | close Long close Short line 184 line 242 |
| Move SL | true | bool | line 186 line 244 |
| Stop Loss ATR Multiplier | 2.5 | float | sl_b tp_b sl_s tp_s |
| Take Profit ATR Multiplier | 4 | float | tp_b tp_s |
| 2 Steps TP qty% | 50 | int | close Long close Short |
| DEMA Angle Threshold (+ & -) | 0 | float | dema_ang_cond_b dema_ang_cond_s |
| DEMA Angle Lookback | 1 | int | dema_angle_rad |
| Testing | Backtest | string · options ["Backtest", "Forwardtest", "All"] | date_range_cond |
| Market | Forex | string · options ['Forex', 'Stocks'] | div_ang |
| BT Date Range | |||
| Backtesting start year | 2021 | int | bt_date_range |
| Backtesting start month | 1 | int | bt_date_range |
| Backtesting start day | 1 | int | bt_date_range |
| Backtesting end year | 2021 | int | bt_date_range |
| Backtesting end month | 12 | int | bt_date_range |
| Backtesting end day | 31 | int | bt_date_range |
| FT Date Range | |||
| Forwardtesting start year | 2022 | int | ft_date_range |
| Forwardtesting start month | 1 | int | ft_date_range |
| Forwardtesting start day | 1 | int | ft_date_range |
| Forwardtesting end year | 2022 | int | ft_date_range |
| Forwardtesting end month | 03 | int | ft_date_range |
| Forwardtesting end day | 26 | int | ft_date_range |
| Pivot Supertrend | |||
| PVT ST Pivot Point Period | 2 | int | ph pl |
| PVT ST ATR Factor | 3 | float | Up Dn |
| PVT ST ATR Period | 9 | int | Up Dn |
| D-EMAs | |||
| DEMA Len | 200 | int | ema_fd dema |
| D-EMAs Source | close | source | ema_fd ema1 ema2 ema3 |
| EMA 1 Len | 21 | int | ema1 |
| EMA 2 Len | 50 | int | ema2 |
| EMA 3 Len | 200 | int | ema3 |
| Normal Supertrend | |||
| ST ATR Period | 21 | int | atr2 atr3 |
| ST Supertrend Source | hl2 | source | up dn |
| ST ATR Multiplier | 2.0 | float | up dn |
What it calculates
- PivothighPVT ST Pivot Point Period (2), PVT ST Pivot Point Period (2)
ph · line 56 - PivotlowPVT ST Pivot Point Period (2), PVT ST Pivot Point Period (2)
pl · line 57 - ATRPVT ST ATR Period (9)
Up · line 68 - ATRPVT ST ATR Period (9)
Dn · line 69 - EMAD-EMAs Source (close), DEMA Len (200)
ema_fd · line 89 - EMAema_fd, DEMA Len (200)
dema · line 90 - EMAD-EMAs Source (close), EMA 1 Len (21)
ema1 · line 95 - EMAD-EMAs Source (close), EMA 2 Len (50)
ema2 · line 96 - EMAD-EMAs Source (close), EMA 3 Len (200)
ema3 · line 97 - SMAta.tr, ST ATR Period (21)
atr2 · line 103 - ATRST ATR Period (21)
atr3 · line 104 - ATR14
atr · line 117
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.
The script has 92 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | (longCondition) | On | 179 |
| Close Long | din_tp | Off “2 Steps Take Profit” | 185 |
| Exit Exit stop and limit | every bar | On | 188 |
| Open Short short | (shortCondition) | On | 237 |
| Close Short | din_tp | Off “2 Steps Take Profit” | 243 |
| Exit Exit stop and limit | every bar | On | 246 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
At least one pattern in the source can repaint; it is marked Check below.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- CheckIntrabar recalculationcalc_on_every_tick = true. Live values can differ from history.
- 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 |
|---|---|---|---|---|---|
| Villa Dinamic Pivot Supertrend Strategy this page | — | v5 strategy | 0.0% | 0 | 0.0% |
| Golden Triangle Strategy by mohanee | sma pivothigh pivotlow atr | v4 strategy | +156.1% | 10 | 101.2% |
| Ta Strategy by AHMEDABDELAZIZZIZO | pivothigh pivotlow ema sma | v5 strategy | +0.3% | 8 | 0.3% |
| ATR with EOM and VORTEX by SoftKill21 | atr ema sma | v4 strategy | +66.2% | 22 | 29.9% |
| cATRpillar Strategy by Obi-wanClintobe | atr sma ema | v4 strategy | +1.02827e+24% | 27 | 88,097.2% |
| Optimized Zhaocaijinbao strategy by fzj20020403 | sma ema atr | v5 strategy | +15.6% | 12 | 9.8% |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L60:C15: cannot use 'float' as a condition — Pine Script v6 does not cast 'int' or 'float' to 'bool' implicitly. Wrap it in bool(). The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © evillalobos1123-//@version=5+//@version=6strategy("Villa Dinamic Pivot Supertrend Strategy", overlay=true, calc_on_every_tick = true, default_qty_type = strategy.fixed)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 51 ms | Yes 1,000 bars in 119 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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/// © evillalobos1123//@version=5strategy("Villa Dinamic Pivot Supertrend Strategy", overlay=true, calc_on_every_tick = true, default_qty_type = strategy.fixed)//INPUTSema_b = input.bool(false, "Use Simple EMA Filter", group = "Strategy Inputs")ema_b_ang = input.bool(true, "Use DEMA Angle Filter", group = "Strategy Inputs")dema_b = input.bool(true, "Use DEMA Filter", group = "Strategy Inputs")st_sig = input.bool(false, "Take Every Supertrend Signal" , group = "Strategy Inputs")take_p = input.bool(true, "Stop Loss at Supertrend", group = "Strategy Inputs")din_tp = input.bool(false, "2 Steps Take Profit", group = "Strategy Inputs")move_sl = input.bool(true, "Move SL", group = "Strategy Inputs")sl_atr = input.float(2.5, "Stop Loss ATR Multiplier", group = "Strategy Inputs")tp_atr = input.float(4, "Take Profit ATR Multiplier", group = "Strategy Inputs")din_tp_qty = input.int(50, "2 Steps TP qty%", group = "Strategy Inputs")dema_a_filter = input.float(0, "DEMA Angle Threshold (+ & -)", group = "Strategy Inputs")dema_a_look = input.int(1, "DEMA Angle Lookback", group = "Strategy Inputs")dr_test = input.string("Backtest", "Testing", options = ["Backtest", "Forwardtest", "All"], group = "Strategy Inputs")test_act = input.string('Forex', 'Market', options = ['Forex', 'Stocks'], group = "Strategy Inputs")not_in_trade = strategy.position_size == 0//Backtesting date rangestart_year = input.int(2021, "Backtesting start year", group = "BT Date Range")start_month = input.int(1, "Backtesting start month", group = "BT Date Range")start_date = input.int(1, "Backtesting start day", group = "BT Date Range")end_year = input.int(2021, "Backtesting end year", group = "BT Date Range")end_month = input.int(12, "Backtesting end month", group = "BT Date Range")end_date = input.int(31, "Backtesting end day", group = "BT Date Range")bt_date_range = (time >= timestamp(syminfo.timezone, start_year, start_month, start_date, 0, 0)) and (time < timestamp(syminfo.timezone, end_year, end_month, end_date, 0, 0)) //Forward testing date rangestart_year_f = input.int(2022, "Forwardtesting start year", group = "FT Date Range")start_month_f = input.int(1, "Forwardtesting start month", group = "FT Date Range")start_date_f = input.int(1, "Forwardtesting start day", group = "FT Date Range")end_year_f = input.int(2022, "Forwardtesting end year", group = "FT Date Range")end_month_f = input.int(03, "Forwardtesting end month", group = "FT Date Range")end_date_f = input.int(26, "Forwardtesting end day", group = "FT Date Range")ft_date_range = (time >= timestamp(syminfo.timezone, start_year_f, start_month_f, start_date_f, 0, 0)) and (time < timestamp(syminfo.timezone, end_year_f, end_month_f, end_date_f, 0, 0))//date conditiondate_range_cond = if dr_test == "Backtest" bt_date_rangeelse if dr_test == "Forwardtest" ft_date_rangeelse true //INDICATORS//PIVOT SUPERTRENDprd = input.int(2, "PVT ST Pivot Point Period", group = "Pivot Supertrend")Factor=input.float(3, "PVT ST ATR Factor", group = "Pivot Supertrend")Pd=input.int(9 , "PVT ST ATR Period", group = "Pivot Supertrend")// get Pivot High/Lowfloat ph = ta.pivothigh(prd, prd)float pl = ta.pivotlow(prd, prd)// calculate the Center line using pivot pointsvar float center = nafloat lastpp = ph ? ph : pl ? pl : naif lastpp if na(center) center := lastpp else //weighted calculation center := (center * 2 + lastpp) / 3// upper/lower bands calculationUp = center - (Factor * ta.atr(Pd))Dn = center + (Factor * ta.atr(Pd))// get the trendfloat TUp = nafloat TDown = naTrend = 0TUp := close[1] > TUp[1] ? math.max(Up, TUp[1]) : UpTDown := close[1] < TDown[1] ? math.min(Dn, TDown[1]) : DnTrend := close > TDown[1] ? 1: close < TUp[1]? -1: nz(Trend[1], 1)Trailingsl = Trend == 1 ? TUp : TDown// check and plot the signalsbsignal = Trend == 1 and Trend[1] == -1ssignal = Trend == -1 and Trend[1] == 1//get S/R levels using Pivot Pointsfloat resistance = nafloat support = nasupport := pl ? pl : support[1]resistance := ph ? ph : resistance[1]//DEMAdema_ln = input.int(200, "DEMA Len", group = 'D-EMAs')dema_src = input.source(close, "D-EMAs Source", group = 'D-EMAs')ema_fd = ta.ema(dema_src, dema_ln)dema = (2*ema_fd)-(ta.ema(ema_fd,dema_ln))//EMAema1_l = input.int(21, "EMA 1 Len", group = 'D-EMAs')ema2_l = input.int(50, "EMA 2 Len", group = 'D-EMAs')ema3_l = input.int(200, "EMA 3 Len", group = 'D-EMAs')ema1 = ta.ema(dema_src, ema1_l)ema2 = ta.ema(dema_src, ema2_l)ema3 = ta.ema(dema_src, ema3_l)//SupertrendPeriods = input.int(21, "ST ATR Period", group = "Normal Supertrend")src_st = input.source(hl2, "ST Supertrend Source", group = "Normal Supertrend")Multiplier = input.float(2.0 , "ST ATR Multiplier", group = "Normal Supertrend")changeATR= trueatr2 = ta.sma(ta.tr, Periods)atr3= changeATR ? ta.atr(Periods) : atr2up=src_st-(Multiplier*atr3)up1 = nz(up[1],up)up := close[1] > up1 ? math.max(up,up1) : updn=src_st+(Multiplier*atr3)dn1 = nz(dn[1], dn)dn := close[1] < dn1 ? math.min(dn, dn1) : dntrend = 1trend := nz(trend[1], trend)trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trendbuySignal = trend == 1 and trend[1] == -1sellSignal = trend == -1 and trend[1] == 1//ATRatr = ta.atr(14)///CONDITIONS//BUY /// ema simpleema_cond_b = if ema_b ema1 > ema2 and ema2 > ema3else true///ema anglediv_ang = if test_act == 'Forex' 0.0001else 1dema_angle_rad = math.atan((dema - dema[dema_a_look])/div_ang)dema_angle = dema_angle_rad * (180/math.pi)dema_ang_cond_b = if ema_b_ang if dema_angle >= dema_a_filter true else falseelse true ///ema distancedema_cond_b = if dema_b close > demaelse true //supertrends///if pivot buy sig or (st buy sig and pivot. trend = 1)pvt_cond_b = bsignalst_cond_b = if st_sig buySignal and Trend == 1else falsest_entry_cond = pvt_cond_b or st_cond_b///stop loss tpsl_b = if take_p if trend == 1 up else close - (atr * sl_atr)else close - (atr * sl_atr)tp_b = if take_p if trend == 1 close + ((close - up) * (tp_atr / sl_atr)) else close + (atr * tp_atr)else close + (atr * tp_atr) //position size init_cap = strategy.equitypos_size_b = math.round((init_cap * .01) / (close - sl_b))ent_price = strategy.opentrades.entry_price(strategy.opentrades - 1)var sl_b_n = 0.0var tp_b_n = 0.0longCondition = (ema_cond_b and dema_cond_b and dema_ang_cond_b and st_entry_cond and date_range_cond and not_in_trade)if (longCondition) strategy.entry("Long", strategy.long, qty = pos_size_b) sl_b_n := sl_b tp_b_n := tp_b ent_price := strategy.opentrades.entry_price(strategy.opentrades - 1)if (up[1] < ent_price and up >= ent_price and trend[0] == 1) if din_tp strategy.close("Long", qty_percent = din_tp_qty) if move_sl sl_b_n := ent_pricestrategy.exit("Exit", "Long", stop =sl_b_n, limit = tp_b_n) //sell///ema simpleema_cond_s = if ema_b ema1 < ema2 and ema2 < ema3else true//ema distancedema_cond_s = if dema_b close < demaelse true//dema angledema_ang_cond_s = if ema_b_ang if dema_angle <= -(dema_a_filter) true else falseelse true//supertrends///if pivot buy sig or (st buy sig and pivot. trend = 1)pvt_cond_s = ssignalst_cond_s = if st_sig sellSignal and Trend == -1else falsest_entry_cond_s = pvt_cond_s or st_cond_s///stop loss tpsl_s = if take_p if trend == -1 dn else close + (atr * sl_atr)else close + (atr * sl_atr)tp_s = if take_p if trend == -1 close - ((dn - close) * (tp_atr / sl_atr)) else close - (atr * tp_atr)else close - (atr * tp_atr)shortCondition = (ema_cond_s and dema_cond_s and dema_ang_cond_s and date_range_cond and st_entry_cond_s and not_in_trade)pos_size_s = math.round((init_cap * .01) / (sl_s - close))var sl_s_n = 0.0var tp_s_n = 0.0if (shortCondition) strategy.entry("Short", strategy.short, qty = pos_size_s) sl_s_n := sl_s tp_s_n := tp_s if (dn[1] > ent_price and dn <= ent_price and trend[0] == -1) if din_tp strategy.close("Short", qty_percent = din_tp_qty) if move_sl sl_s_n := ent_pricestrategy.exit("Exit", "Short", stop = sl_s_n, limit = tp_s_n)