Pine Script v4 · Strategy
Higher TF - Repainting + Limiting backtest results
A Pine Script v4 strategy built on SMA or EMA or HMA or RMA or VWMA or WMA (source, length) and Supertrend (1, 4). It has 2 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 0.01% commission. 8 settings; defaults include HTFMultiplier 4, Moving Average Type rma and Supertrend Mult 1.
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 Higher TF - Repainting + Limiting backtest results repaint?
- Yes
- At least one pattern in the source can repaint: higher-timeframe data.
- How did Higher TF - Repainting + Limiting backtest results 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. On BTCUSDT 4h it did trade; that row of the markets table below has its figures. A backtest, not investment advice.
- Can Higher TF - Repainting + Limiting backtest results 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 Higher TF - Repainting + Limiting backtest results use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Higher TF - Repainting + Limiting backtest results run in Zeal Terminal?
- Yes
- It compiles in 22 ms and runs 1,000 bars in 189 ms, in the browser.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
Needs BINANCE:BTCUSDT 1S data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Values that depend on it are empty on this chart.
- 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, $100,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
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
4 settings have no label
In the settings dialog HTFMultiplier (line 5), SupertrendMult (line 7), SupertrendPd (line 8), usePreviousBar (line 20) 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 148, 149. 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 tostring → str.tostring, round → math.round, min → math.min, sma → ta.sma, ema → ta.ema, hma → ta.hma, rma → ta.rma, vwma → ta.vwma, wma → ta.wma, security → request.security, max → math.max, supertrend → ta.supertrend.
- 2 strategy calls use the when = argument (line 148, 149). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 39 trades and the same P&L on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 32: tostring → str.tostring
- Line 32: round → math.round
- Line 33: tostring → str.tostring
- Line 33: round → math.round
- Line 33: min → math.min
19 more edits
- Line 81: sma → ta.sma
- Line 83: ema → ta.ema
- Line 85: hma → ta.hma
- Line 87: rma → ta.rma
- Line 89: vwma → ta.vwma
- Line 91: wma → ta.wma
- Line 93: security → request.security
- Line 94: security → request.security
- Line 100: max → math.max
- Line 100: min → math.min
- Line 107: max → math.max
- Line 111: min → math.min
- Line 127: security → request.security
- Line 127: supertrend → ta.supertrend
- Line 128: security → request.security
- Line 128: supertrend → ta.supertrend
- Line 129: security → request.security
- Line 129: supertrend → ta.supertrend
- Line 132: supertrend → ta.supertrend
⋯// © HeWhoMustNotBeNamed-//@version=4+//@version=5strategy("Higher TF - Repainting", overlay=true, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01, calc_on_order_fills = true)⋯ target_Res_In_Min <= 0.751 ? "30S" :- target_Res_In_Min <= 1440 ? tostring(round(target_Res_In_Min)) :+ target_Res_In_Min <= 1440 ? str.tostring(math.round(target_Res_In_Min)) :- tostring(round(min(target_Res_In_Min / 1440, 365))) + "D"+ str.tostring(math.round(math.min(target_Res_In_Min / 1440, 365))) + "D"f_getBackTestTimeFrom(backtestFrom, backtestBars)=>⋯f_getMovingAverage(source, MAType, length)=>- ma = sma(source, length)+ ma = ta.sma(source, length) if(MAType == "ema")- ma := ema(source,length)+ ma := ta.ema(source,length) if(MAType == "hma")- ma := hma(source,length)+ ma := ta.hma(source,length) if(MAType == "rma")- ma := rma(source,length)+ ma := ta.rma(source,length) if(MAType == "vwma")- ma := vwma(source,length)+ ma := ta.vwma(source,length) if(MAType == "wma")- ma := wma(source,length)+ ma := ta.wma(source,length) ma-f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)+f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)-f_security(_symbol, _res, _src, _repaint) => security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]+f_security(_symbol, _res, _src, _repaint) => request.security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]f_getSupertrend(HTFMultiplier, AtrMAType, SupertrendMult, SupertrendPd, wicks, useSecureSecurity, repaint)=>⋯ hClose = useSecureSecurity? f_secureSecurity(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), close) : f_security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), close, repaint)- truerange = max(hHigh, hClose[1]) - min(hLow, hClose[1])+ truerange = math.max(hHigh, hClose[1]) - math.min(hLow, hClose[1]) ⋯ longStopPrev = nz(longStop[1], longStop)- longStop := nz(dir[1], dir) == -1 ? max(longStop, longStopPrev) : longStop+ longStop := nz(dir[1], dir) == -1 ? math.max(longStop, longStopPrev) : longStop ⋯ shortStopPrev = nz(shortStop[1], shortStop)- shortStop := nz(dir[1], dir) == 1 ? min(shortStop, shortStopPrev) : shortStop+ shortStop := nz(dir[1], dir) == 1 ? math.min(shortStop, shortStopPrev) : shortStop ⋯useCustomSecureSecurity = repaintOption == "Minimal - use custom f_secureSecurity"-[SupertrendRepaint, DirRepaint] = security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), supertrend(SupertrendMult, SupertrendPd), lookahead = true, gaps=true)+[SupertrendRepaint, DirRepaint] = request.security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), ta.supertrend(SupertrendMult, SupertrendPd), lookahead = true, gaps=true)-[SupertrendLookahead, DirLookahead] = security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), supertrend(SupertrendMult, SupertrendPd), lookahead = true, gaps=false)+[SupertrendLookahead, DirLookahead] = request.security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), ta.supertrend(SupertrendMult, SupertrendPd), lookahead = true, gaps=false)-[SupertrendNoLookahead, DirNoLookahead] = security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), supertrend(SupertrendMult, SupertrendPd), lookahead = false, gaps=false)+[SupertrendNoLookahead, DirNoLookahead] = request.security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), ta.supertrend(SupertrendMult, SupertrendPd), lookahead = false, gaps=false)[SupertrendSecure1, DirSecure1] = f_getSupertrend(HTFMultiplier, AtrMAType, SupertrendMult, SupertrendPd, false, false, false)[SupertrendSecure2, DirSecure2] = f_getSupertrend(HTFMultiplier, AtrMAType, SupertrendMult, SupertrendPd, false, true, false)-[SupertrendRegular, DirRegular] = supertrend(SupertrendMult, SupertrendPd)+[SupertrendRegular, DirRegular] = ta.supertrend(SupertrendMult, SupertrendPd)[date, mth, yr] = f_getBackTestTimeFrom(backtestFrom, backtestBars)⋯Same 39 trades and the same P&L on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.int()
- Line 6: input() → input.string()
- Line 7: input() → input.int()
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.string()
4 more edits
- Line 11: input() → input.string()
- Line 20: input() → input.bool()
- Line 148: when = longCondition and inDateRange → if block
- Line 149: when = shortCondition and inDateRange → if block
⋯strategy("Higher TF - Repainting", overlay=true, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01, calc_on_order_fills = true)-HTFMultiplier = input(4, minval=1, step=1)+HTFMultiplier = input.int(4, minval=1, step=1)-AtrMAType = input(title="Moving Average Type", defval="rma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])+AtrMAType = input.string(title="Moving Average Type", defval="rma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])-SupertrendMult = input(1)+SupertrendMult = input.int(1)-SupertrendPd = input(4, minval=4, step=4)+SupertrendPd = input.int(4, minval=4, step=4)-backtestBars = input(title="Backtest from ", defval=10, minval=1, maxval=30)+backtestBars = input.int(title="Backtest from ", defval=10, minval=1, maxval=30)-backtestFrom = input(title="Timeframe", defval="years", options=["days", "months", "years"])+backtestFrom = input.string(title="Timeframe", defval="years", options=["days", "months", "years"])-repaintOption = input(title="Repaint", defval="Yes - lookahead : true and merge : true", +repaintOption = input.string(title="Repaint", defval="Yes - lookahead : true and merge : true", options=[⋯ )-usePreviousBar = input(false)+usePreviousBar = input.bool(false)f_multiple_resolution(HTFMultiplier) => ⋯ DirRegular == 1-strategy.entry("Buy", strategy.long, when=longCondition and inDateRange)+if longCondition and inDateRange+ strategy.entry("Buy", strategy.long)-strategy.entry("Sell", strategy.short, when=shortCondition and inDateRange)+if shortCondition and inDateRange+ strategy.entry("Sell", strategy.short)Does it run in Pine Script v6?
Yes
Same 39 trades and the same P&L on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © HeWhoMustNotBeNamed-//@version=5+//@version=6strategy("Higher TF - Repainting", overlay=true, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01, calc_on_order_fills = true)⋯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 |
|---|---|---|---|
| HTFMultiplier (no label in the script) | 4 | int · min 1 · step 1 | target_Res_In_Min hHigh hLow hOpen hClose SupertrendRepaint DirRepaint SupertrendLookahead DirLookahead SupertrendNoLookahead DirNoLookahead SupertrendSecure1 DirSecure1 SupertrendSecure2 DirSecure2 |
| Moving Average Type | rma | string · options ["ema", "sma", "hma", "rma", "vwma", "wma"] | averagetruerange SupertrendSecure1 DirSecure1 SupertrendSecure2 DirSecure2 |
| Supertrend Mult (no label in the script) | 1 | int | atr SupertrendRepaint DirRepaint SupertrendLookahead DirLookahead SupertrendNoLookahead DirNoLookahead SupertrendSecure1 DirSecure1 SupertrendSecure2 DirSecure2 SupertrendRegular DirRegular |
| Supertrend Pd (no label in the script) | 4 | int · min 4 · step 4 | averagetruerange SupertrendRepaint DirRepaint SupertrendLookahead DirLookahead SupertrendNoLookahead DirNoLookahead SupertrendSecure1 DirSecure1 SupertrendSecure2 DirSecure2 SupertrendRegular DirRegular |
| Backtest from | 10 | int · min 1 · max 30 | date mth yr |
| Timeframe | years | string · options ["days", "months", "years"] | byDate byMonth byYear date mth yr |
| Repaint | Yes - lookahead : true and merge : true | string · options [ "Yes - lookahead : true and merge : true", "Yes - lookahead : true and merge : false", "Yes - lookahead/merge false", "Minimal - use custom f_security", "Minimal - use custom f_secureSecurity", "No - do not use security"] | repaint useLookAheadButNotMerge useSecurityLookahead useCustomSecurity useCustomSecureSecurity |
| Use Previous Bar (no label in the script) | false | bool | offset |
What it calculates
- SMA / EMA / HMA / RMA / VWMA / WMA (
sma,ta.smain v5)source, lengthma · line 81 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (1), Supertrend Pd (4)SupertrendRepaint · line 127 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (1), Supertrend Pd (4)DirRepaint · line 127 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (1), Supertrend Pd (4)SupertrendLookahead · line 128 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (1), Supertrend Pd (4)DirLookahead · line 128 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (1), Supertrend Pd (4)SupertrendNoLookahead · line 129 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (1), Supertrend Pd (4)DirNoLookahead · line 129 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (1), Supertrend Pd (4)SupertrendRegular · line 132 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (1), Supertrend Pd (4)DirRegular · line 132
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 Buy long | longCondition and inDateRange | On | 148 |
| Open Sell short | shortCondition and inDateRange | On | 149 |
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.
- CheckHigher-timeframe data1 security call(s) use lookahead_on (line 93). Without a [1] offset this reads future bars in history.
- 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
Not run on other markets: the script requests BINANCE:BTCUSDT 1S, and that data is not available here (a lower timeframe than the chart; the engine does not evaluate intrabar data). Its results would rest on empty values.
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 |
|---|---|---|---|---|---|
| Higher TF - Repainting + Limiting backtest results this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| Smoothed Heikin Ashi Trend on Chart - TraderHalai BACKTEST by TraderHalai | sma ema hma vwma rma | v5 strategy | +1.3% | 113 | 5.6% |
| High-Low Channel Multi averages Crypto Swing strategy by exlux99 | sma ema wma rma vwma | v4 strategy | No trades on this market | ||
| Ultra Moving Average Rating Trend Strategy by exlux99 | ema wma sma rma vwma hma | v5 strategy | +2.8% | 5 | 2.4% |
| VCP pivot buy by perrycc007 | sma ema rma vwma wma | v4 strategy | does not run in Zeal Terminal | ||
| Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] by myncrypto | ema sma supertrend rma | v5 strategy | −115.1% | 6 | 123.3% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 22 ms | With a workaround 1,000 bars in 189 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/// © HeWhoMustNotBeNamed//@version=4strategy("Higher TF - Repainting", overlay=true, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01, calc_on_order_fills = true)HTFMultiplier = input(4, minval=1, step=1)AtrMAType = input(title="Moving Average Type", defval="rma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])SupertrendMult = input(1)SupertrendPd = input(4, minval=4, step=4)backtestBars = input(title="Backtest from ", defval=10, minval=1, maxval=30)backtestFrom = input(title="Timeframe", defval="years", options=["days", "months", "years"])repaintOption = input(title="Repaint", defval="Yes - lookahead : true and merge : true", options=[ "Yes - lookahead : true and merge : true", "Yes - lookahead : true and merge : false", "Yes - lookahead/merge false", "Minimal - use custom f_security", "Minimal - use custom f_secureSecurity", "No - do not use security"] )usePreviousBar = input(false)f_multiple_resolution(HTFMultiplier) => target_Res_In_Min = timeframe.multiplier * HTFMultiplier * ( timeframe.isseconds ? 1. / 60. : timeframe.isminutes ? 1. : timeframe.isdaily ? 1440. : timeframe.isweekly ? 7. * 24. * 60. : timeframe.ismonthly ? 30.417 * 24. * 60. : na) target_Res_In_Min <= 0.0417 ? "1S" : target_Res_In_Min <= 0.167 ? "5S" : target_Res_In_Min <= 0.376 ? "15S" : target_Res_In_Min <= 0.751 ? "30S" : target_Res_In_Min <= 1440 ? tostring(round(target_Res_In_Min)) : tostring(round(min(target_Res_In_Min / 1440, 365))) + "D"f_getBackTestTimeFrom(backtestFrom, backtestBars)=> byDate = backtestFrom == "days" byMonth = backtestFrom == "months" byYear = backtestFrom == "years" date = dayofmonth(timenow) mth = month(timenow) yr = year(timenow) leapYearDaysInMonth = array.new_int(12,0) array.set(leapYearDaysInMonth,0,31) array.set(leapYearDaysInMonth,1,29) nonleapYearDaysInMonth = array.new_int(12,0) array.set(leapYearDaysInMonth,0,31) array.set(leapYearDaysInMonth,1,28) restMonths = array.new_int(10,0) array.set(leapYearDaysInMonth,0,31) array.set(leapYearDaysInMonth,1,30) array.set(leapYearDaysInMonth,2,31) array.set(leapYearDaysInMonth,3,30) array.set(leapYearDaysInMonth,4,31) array.set(leapYearDaysInMonth,5,31) array.set(leapYearDaysInMonth,6,30) array.set(leapYearDaysInMonth,7,31) array.set(leapYearDaysInMonth,8,30) array.set(leapYearDaysInMonth,9,31) array.concat(leapYearDaysInMonth,restMonths) array.concat(nonleapYearDaysInMonth,restMonths) isLeapYear = yr % 4 == 0 and (year%100 != 0 or year%400 == 0) numberOfDaysInCurrentMonth = isLeapYear ? array.get(leapYearDaysInMonth, mth-2) : array.get(nonleapYearDaysInMonth, mth-2) if(byDate) mth := (date - backtestBars) < 0 ? mth - 1 : mth yr := mth < 1 ? yr - 1 : yr mth := mth < 1 ? 1 : mth date := (date - backtestBars) < 0 ? numberOfDaysInCurrentMonth - backtestBars + date + 1 : date - backtestBars + 1 if(byMonth) date := 1 yr := (mth - (backtestBars%12)) < 0 ? yr - int(backtestBars/12) - 1 : yr - int(backtestBars/12) mth := mth - (backtestBars%12) + 1 if(byYear) date := 1 mth := 1 yr := yr - backtestBars [date, mth, yr]f_getMovingAverage(source, MAType, length)=> ma = sma(source, length) if(MAType == "ema") ma := ema(source,length) if(MAType == "hma") ma := hma(source,length) if(MAType == "rma") ma := rma(source,length) if(MAType == "vwma") ma := vwma(source,length) if(MAType == "wma") ma := wma(source,length) maf_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)f_security(_symbol, _res, _src, _repaint) => security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]f_getSupertrend(HTFMultiplier, AtrMAType, SupertrendMult, SupertrendPd, wicks, useSecureSecurity, repaint)=> hHigh = useSecureSecurity? f_secureSecurity(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), high) : f_security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), high, repaint) hLow = useSecureSecurity? f_secureSecurity(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), low) : f_security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), low, repaint) hOpen = useSecureSecurity? f_secureSecurity(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), open) : f_security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), open, repaint) hClose = useSecureSecurity? f_secureSecurity(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), close) : f_security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), close, repaint) truerange = max(hHigh, hClose[1]) - min(hLow, hClose[1]) averagetruerange = f_getMovingAverage(truerange, AtrMAType, SupertrendPd) atr = averagetruerange * SupertrendMult dir = -1 longStop = hClose - atr longStopPrev = nz(longStop[1], longStop) longStop := nz(dir[1], dir) == -1 ? max(longStop, longStopPrev) : longStop shortStop = hClose + atr shortStopPrev = nz(shortStop[1], shortStop) shortStop := nz(dir[1], dir) == 1 ? min(shortStop, shortStopPrev) : shortStop lowToHigh = (wicks ? hHigh : hClose) > shortStopPrev highToLow = (wicks ? hLow : hClose) < longStopPrev dir := nz(dir[1], dir) dir := dir == 1 and lowToHigh ? -1 : dir == -1 and highToLow ? 1 : dir trailingStop = dir == -1? longStop : shortStop [trailingStop, dir] repaint = repaintOption == "Yes - lookahead : true and merge : true"useLookAheadButNotMerge = repaintOption == "Yes - lookahead : true and merge : false"useSecurityLookahead = repaintOption == "Yes - lookahead/merge false"useCustomSecurity = repaintOption == "Minimal - use custom f_security"useCustomSecureSecurity = repaintOption == "Minimal - use custom f_secureSecurity"[SupertrendRepaint, DirRepaint] = security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), supertrend(SupertrendMult, SupertrendPd), lookahead = true, gaps=true)[SupertrendLookahead, DirLookahead] = security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), supertrend(SupertrendMult, SupertrendPd), lookahead = true, gaps=false)[SupertrendNoLookahead, DirNoLookahead] = security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), supertrend(SupertrendMult, SupertrendPd), lookahead = false, gaps=false)[SupertrendSecure1, DirSecure1] = f_getSupertrend(HTFMultiplier, AtrMAType, SupertrendMult, SupertrendPd, false, false, false)[SupertrendSecure2, DirSecure2] = f_getSupertrend(HTFMultiplier, AtrMAType, SupertrendMult, SupertrendPd, false, true, false)[SupertrendRegular, DirRegular] = supertrend(SupertrendMult, SupertrendPd)[date, mth, yr] = f_getBackTestTimeFrom(backtestFrom, backtestBars)inDateRange = time >= timestamp(syminfo.timezone, yr, mth, date, 0, 0)offset = usePreviousBar? 1 : 0longCondition = repaint ? DirRepaint[offset] == -1 : useSecurityLookahead? DirNoLookahead[offset] == -1 : useLookAheadButNotMerge? DirLookahead[offset] == -1 : useCustomSecurity? DirSecure1 == -1 : useCustomSecureSecurity? DirSecure2 == -1 : DirRegular == -1shortCondition = repaint ? DirRepaint[offset] == 1 : useSecurityLookahead? DirNoLookahead[offset] == 1 : useLookAheadButNotMerge? DirLookahead[offset] == 1 : useCustomSecurity? DirSecure1 == 1 : useCustomSecureSecurity? DirSecure2 == 1 : DirRegular == 1strategy.entry("Buy", strategy.long, when=longCondition and inDateRange)strategy.entry("Sell", strategy.short, when=shortCondition and inDateRange)