Pine Script v4 · Strategy
TEMA Cross +HTF Backtest
A Pine Script v4 strategy built on EMA (xPrice, 20). It has 4 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 500 in cash and pays no commission. 12 settings; defaults include What type of Orders Longs+Shorts, From Month 1 and From Day 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 TEMA Cross +HTF Backtest repaint?
- Yes
- At least one pattern in the source can repaint: higher-timeframe data.
- How did TEMA Cross +HTF Backtest do on BTCUSDT 1D with default settings?
- +82.59%
- From 26 Jan 2024 to 21 Oct 2026, over 27 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can TEMA Cross +HTF Backtest 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 TEMA Cross +HTF Backtest use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does TEMA Cross +HTF Backtest run in Zeal Terminal?
- Yes
- It compiles in 31 ms and runs 1,000 bars in 189 ms, in the browser.
BTCUSDT · 1D · default settings
- TEMA1
- TEMA2
- TEMA1
- TEMA2
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +82.59%
- $412.96
- Buy and hold
- +102.5%
- same period
- Closed trades
- 27
- 13 won · 14 lost
- Profitable
- 48.1%
- of closed trades
- Profit factor
- 1.90
- gross profit ÷ gross loss
- Max drawdown
- 42.98%
- −$214.88
One run with the author's default settings, $500 starting capital, commission as the script sets it. A short 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 68, 69, 71, 72. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 38
transp = on line 38, 48, 55, 57, 58, 59. v5 folds the transparency into the colour with color.new(c, transp).
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.
- 4 strategy calls use the when = argument (line 68, 69, 71, 72). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 27 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 26: ema → ta.ema
- Line 27: ema → ta.ema
- Line 28: ema → ta.ema
- Line 33: ema → ta.ema
- Line 34: ema → ta.ema
15 more edits
- Line 35: ema → ta.ema
- Line 38: transp = 65 → color.new(xnRes > ynRes ? color.green : color.red, 65)
- Line 40: input(type = input.resolution) → input.timeframe()
- Line 41: security → request.security
- Line 44: ema → ta.ema
- Line 45: ema → ta.ema
- Line 46: ema → ta.ema
- Line 48: transp = 30 → color.new(color.yellow, 30)
- Line 51: ema → ta.ema
- Line 52: ema → ta.ema
- Line 53: ema → ta.ema
- Line 55: transp = 30 → color.new(color.purple, 30)
- Line 57: transp = 90 → color.new(HTFxnRes > HTFynRes ? color.yellow : color.purple, 90)
- Line 58: transp = 90 → color.new(HTFxnRes > HTFynRes ? color.yellow : na, 90)
- Line 59: transp = 90 → color.new(HTFxnRes < HTFynRes ? color.purple : na, 90)
⋯// © Seltzer_-//@version=4+//@version=5strategy(title="TEMA Cross +HTF Backtest", shorttitle="TEMA_X_+HTF_BT", overlay=true, commission_type=strategy.commission.percent, commission_value=0, initial_capital = 500, default_qty_type=strategy.cash, default_qty_value=500)⋯xPrice = close-xEMA1 = ema(xPrice, xLength)+xEMA1 = ta.ema(xPrice, xLength)-xEMA2 = ema(xEMA1, xLength)+xEMA2 = ta.ema(xEMA1, xLength)-xEMA3 = ema(xEMA2, xLength)+xEMA3 = ta.ema(xEMA2, xLength)xnRes = (3 * xEMA1) - (3 * xEMA2) + xEMA3⋯yPrice = close-yEMA1 = ema(yPrice, yLength)+yEMA1 = ta.ema(yPrice, yLength)-yEMA2 = ema(yEMA1, yLength)+yEMA2 = ta.ema(yEMA1, yLength)-yEMA3 = ema(yEMA2, yLength)+yEMA3 = ta.ema(yEMA2, yLength)ynRes = (3 * yEMA1) - (3 * yEMA2) + yEMA3ynResP = plot(ynRes, color=color.red, linewidth=2, title="TEMA2")-fill(xnResP, ynResP, color=xnRes > ynRes ? color.green : color.red, transp=65, editable=true)+fill(xnResP, ynResP, color=color.new(xnRes > ynRes ? color.green : color.red, 65), editable=true)//HTF Section-HTFres = input(defval="D", type=input.resolution, title="HTF Resolution")+HTFres = input.timeframe(defval="D", title="HTF Resolution")-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)HTFxLength = input(5, minval=1, title="HTF Fast Length")HTFxPrice = close-HTFxEMA1 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFxPrice, HTFxLength))+HTFxEMA1 = f_secureSecurity(syminfo.tickerid, HTFres, ta.ema(HTFxPrice, HTFxLength))-HTFxEMA2 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFxEMA1, HTFxLength))+HTFxEMA2 = f_secureSecurity(syminfo.tickerid, HTFres, ta.ema(HTFxEMA1, HTFxLength))-HTFxEMA3 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFxEMA2, HTFxLength))+HTFxEMA3 = f_secureSecurity(syminfo.tickerid, HTFres, ta.ema(HTFxEMA2, HTFxLength))HTFxnRes = (3 * HTFxEMA1) - (3 * HTFxEMA2) + HTFxEMA3-HTFxnResP = plot(HTFxnRes, color=color.yellow, linewidth=1,transp=30, title="TEMA1")+HTFxnResP = plot(HTFxnRes, color=color.new(color.yellow, 30), linewidth=1, title="TEMA1")HTFyLength = input(15, minval=1, title="HTF Slow Length")HTFyPrice = close-HTFyEMA1 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFyPrice, HTFyLength))+HTFyEMA1 = f_secureSecurity(syminfo.tickerid, HTFres, ta.ema(HTFyPrice, HTFyLength))-HTFyEMA2 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFyEMA1, HTFyLength))+HTFyEMA2 = f_secureSecurity(syminfo.tickerid, HTFres, ta.ema(HTFyEMA1, HTFyLength))-HTFyEMA3 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFyEMA2, HTFyLength))+HTFyEMA3 = f_secureSecurity(syminfo.tickerid, HTFres, ta.ema(HTFyEMA2, HTFyLength))HTFynRes = (3 * HTFyEMA1) - (3 * HTFyEMA2) + HTFyEMA3-HTFynResP = plot(HTFynRes, color=color.purple, linewidth=1, transp=30, title="TEMA2")+HTFynResP = plot(HTFynRes, color=color.new(color.purple, 30), linewidth=1, title="TEMA2")// HTF Plot-fill(HTFxnResP, HTFynResP, color=HTFxnRes > HTFynRes ? color.yellow : color.purple, transp=90, editable=true)+fill(HTFxnResP, HTFynResP, color=color.new(HTFxnRes > HTFynRes ? color.yellow : color.purple, 90), editable=true)-bgcolor(HTFxnRes > HTFynRes ? color.yellow : na, transp=90, editable=true)+bgcolor(color.new(HTFxnRes > HTFynRes ? color.yellow : na, 90), editable=true)-bgcolor(HTFxnRes < HTFynRes ? color.purple : na, transp=90, editable=true)+bgcolor(color.new(HTFxnRes < HTFynRes ? color.purple : na, 90), editable=true)// }⋯Same 27 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.string()
- Line 10: input() → input.int()
- Line 11: input() → input.int()
- Line 12: input() → input.int()
- Line 13: input() → input.int()
- Line 14: input() → input.int()
9 more edits
- Line 15: input() → input.int()
- Line 24: input() → input.int()
- Line 31: input() → input.int()
- Line 42: input() → input.int()
- Line 49: input() → input.int()
- Line 68: when = LongEntryAlert → if block
- Line 69: when = LongCloseAlert → if block
- Line 71: when = ShortEntryAlert → if block
- Line 72: when = ShortCloseAlert → if block
⋯strategy(title="TEMA Cross +HTF Backtest", shorttitle="TEMA_X_+HTF_BT", overlay=true, commission_type=strategy.commission.percent, commission_value=0, initial_capital = 500, default_qty_type=strategy.cash, default_qty_value=500)-orderType = input("Longs+Shorts",title="What type of Orders", options=["Longs+Shorts","LongsOnly","ShortsOnly"])+orderType = input.string("Longs+Shorts",title="What type of Orders", options=["Longs+Shorts","LongsOnly","ShortsOnly"])isLong = (orderType != "ShortsOnly")⋯// Backtest inputs-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=2020, title="From Year", minval=2010)+FromYear = input.int(defval=2020, title="From Year", minval=2010)-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)// Define backtest timewindow⋯//LTF Section-xLength = input(20, minval=1, title="Fast Length")+xLength = input.int(20, minval=1, title="Fast Length")xPrice = close⋯xnResP = plot(xnRes, color=color.green, linewidth=2, title="TEMA1")-yLength = input(60, minval=1, title="Slow Length")+yLength = input.int(60, minval=1, title="Slow Length")yPrice = close⋯f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)-HTFxLength = input(5, minval=1, title="HTF Fast Length")+HTFxLength = input.int(5, minval=1, title="HTF Fast Length")HTFxPrice = close⋯HTFxnResP = plot(HTFxnRes, color=color.new(color.yellow, 30), linewidth=1, title="TEMA1")-HTFyLength = input(15, minval=1, title="HTF Slow Length")+HTFyLength = input.int(15, minval=1, title="HTF Slow Length")HTFyPrice = close⋯if isLong- strategy.entry("Long", strategy.long, when = LongEntryAlert)+ if LongEntryAlert+ strategy.entry("Long", strategy.long)- strategy.close("Long", when = LongCloseAlert)+ if LongCloseAlert+ strategy.close("Long")if isShort- strategy.entry("Short", strategy.short, when = ShortEntryAlert)+ if ShortEntryAlert+ strategy.entry("Short", strategy.short)- strategy.close("Short", when = ShortCloseAlert)+ if ShortCloseAlert+ strategy.close("Short")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: 27 before, 1 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.
⋯// © Seltzer_-//@version=5+//@version=6strategy(title="TEMA Cross +HTF Backtest", shorttitle="TEMA_X_+HTF_BT", overlay=true, commission_type=strategy.commission.percent, commission_value=0, initial_capital = 500, default_qty_type=strategy.cash, default_qty_value=500)⋯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 |
|---|---|---|---|
| What type of Orders | Longs+Shorts | string · options ["Longs+Shorts","LongsOnly","ShortsOnly"] | isLong isShort |
| From Month | 1 | int · min 1 · max 12 | start |
| From Day | 1 | int · min 1 · max 31 | start |
| From Year | 2020 | int · min 2010 | start |
| To Month | 1 | int · min 1 · max 12 | finish |
| To Day | 1 | int · min 1 · max 31 | finish |
| To Year | 9999 | int · min 2017 | finish |
| Fast Length | 20 | int · min 1 | xEMA1 xEMA2 xEMA3 |
| Slow Length | 60 | int · min 1 | yEMA1 yEMA2 yEMA3 |
| HTF Resolution | D | timeframe (written input.resolution) | HTFxEMA1 HTFxEMA2 HTFxEMA3 HTFyEMA1 HTFyEMA2 HTFyEMA3 |
| HTF Fast Length | 5 | int · min 1 | HTFxEMA1 HTFxEMA2 HTFxEMA3 |
| HTF Slow Length | 15 | int · min 1 | HTFyEMA1 HTFyEMA2 HTFyEMA3 |
What it calculates
- EMA (
ema,ta.emain v5)xPrice, Fast Length (20)xEMA1 · line 26 - EMA (
ema,ta.emain v5)xEMA1, Fast Length (20)xEMA2 · line 27 - EMA (
ema,ta.emain v5)xEMA2, Fast Length (20)xEMA3 · line 28 - EMA (
ema,ta.emain v5)yPrice, Slow Length (60)yEMA1 · line 33 - EMA (
ema,ta.emain v5)yEMA1, Slow Length (60)yEMA2 · line 34 - EMA (
ema,ta.emain v5)yEMA2, Slow Length (60)yEMA3 · line 35 - EMA (
ema,ta.emain v5)HTFxPrice, HTF Fast Length (5)HTFxEMA1 · line 44 - EMA (
ema,ta.emain v5)HTFxEMA1, HTF Fast Length (5)HTFxEMA2 · line 45 - EMA (
ema,ta.emain v5)HTFxEMA2, HTF Fast Length (5)HTFxEMA3 · line 46 - EMA (
ema,ta.emain v5)HTFyPrice, HTF Slow Length (15)HTFyEMA1 · line 51 - EMA (
ema,ta.emain v5)HTFyEMA1, HTF Slow Length (15)HTFyEMA2 · line 52 - EMA (
ema,ta.emain v5)HTFyEMA2, HTF Slow Length (15)HTFyEMA3 · line 53
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 | LongEntryAlertxnRes > ynRes and HTFxnRes > HTFynRes and window() | On | 68 |
| Close Long | LongCloseAlertxnRes < ynRes and window() | On | 69 |
| Open Short short | ShortEntryAlertxnRes < ynRes and HTFxnRes < HTFynRes and window() | On | 71 |
| Close Short | ShortCloseAlertxnRes > ynRes | On | 72 |
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 41). 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
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 | +82.6% | +102.5% | 27 | 48% | 1.90 | 43.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +10.9% | +5.5% | 27 | 37% | 1.32 | 11.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +119.8% | +9.8% | 24 | 50% | 1.81 | 35.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +13.1% | +8.4% | 25 | 28% | 1.27 | 20.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +39.6% | +16.7% | 34 | 38% | 1.21 | 45.9% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +11.7% | +21.5% | 28 | 29% | 1.22 | 15.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −36.6% | +148.3% | 33 | 39% | 0.77 | 85.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +1.0% | +17.1% | 32 | 34% | 1.03 | 15.4% | 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 | 11 | 55% | $143.78 |
| Sideways market | 12 | 42% | $133.22 |
| Falling market | 4 | 50% | $135.96 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
46.7%
The 95th percentile of the worst drawdown when the same 27 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 21.9% to 46.7%; the tick is what actually happened, 41.8%. Order luck alone moves it this much.
All 27 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 22 Jul 2024 68,165.40 | 5 Aug 2024 58,161.00 | 14 | −$73.38 |
| short | 5 Aug 2024 58,161.00 | 23 Aug 2024 60,375.80 | 18 | −$19.04 |
| long | 23 Aug 2024 60,375.80 | 4 Sep 2024 57,487.70 | 12 | −$23.92 |
| short | 4 Sep 2024 57,487.70 | 14 Sep 2024 60,498.00 | 10 | −$26.18 |
| long | 14 Sep 2024 60,498.00 | 10 Oct 2024 60,636.00 | 26 | $1.14 |
| short | 11 Oct 2024 60,326.40 | 15 Oct 2024 66,084.00 | 4 | −$47.72 |
| long | 15 Oct 2024 66,084.00 | 6 Nov 2024 69,372.00 | 22 | $24.88 |
| short | 6 Nov 2024 69,372.00 | 7 Nov 2024 75,572.00 | 1 | −$44.69 |
| long | 8 Nov 2024 75,857.90 | 3 Dec 2024 95,840.60 | 25 | $131.71 |
| short | 3 Dec 2024 95,840.60 | 21 Jan 2025 102,260.00 | 49 | −$33.49 |
| long | 21 Jan 2025 102,260.00 | 2 Feb 2025 100,636.00 | 12 | −$7.94 |
| short | 2 Feb 2025 100,636.00 | 20 Mar 2025 86,845.90 | 46 | $68.51 |
| long | 20 Mar 2025 86,845.90 | 30 May 2025 105,590.00 | 71 | $107.91 |
| short | 30 May 2025 105,590.00 | 11 Jul 2025 116,010.00 | 42 | −$49.34 |
| long | 11 Jul 2025 116,010.00 | 29 Jul 2025 118,062.00 | 18 | $8.85 |
| short | 29 Jul 2025 118,062.00 | 12 Sep 2025 115,483.00 | 45 | $10.92 |
| long | 12 Sep 2025 115,483.00 | 27 Sep 2025 109,643.00 | 15 | −$25.28 |
| short | 27 Sep 2025 109,643.00 | 1 Oct 2025 114,049.00 | 4 | −$20.09 |
| long | 1 Oct 2025 114,049.00 | 15 Oct 2025 113,028.00 | 14 | −$4.48 |
| short | 15 Oct 2025 113,028.00 | 3 Dec 2025 91,277.90 | 49 | $96.22 |
| long | 4 Dec 2025 93,429.90 | 26 Jan 2026 86,670.40 | 53 | −$36.17 |
| short | 26 Jan 2026 86,670.40 | 27 Feb 2026 67,485.20 | 32 | $110.68 |
| long | 27 Feb 2026 67,485.20 | 14 May 2026 79,313.60 | 76 | $87.64 |
| short | 14 May 2026 79,313.60 | 4 Jul 2026 62,583.30 | 51 | $105.47 |
| long | 4 Jul 2026 62,583.30 | 12 Sep 2026 77,225.70 | 70 | $116.98 |
| short | 12 Sep 2026 77,225.70 | 24 Sep 2026 84,397.60 | 12 | −$46.43 |
| long | 24 Sep 2026 84,397.60 | 27 Sep 2026 84,433.10 | 3 | $0.21 |
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 |
|---|---|---|---|---|---|
| TEMA Cross +HTF Backtest this page | — | v4 strategy | +82.6% | 27 | 43.0% |
| EMA Slope AyE by fajardoarelys | ema | v4 strategy | No trades on this market | ||
| ROoT by realisticDove62527 | ema | v5 strategy | −4.7% | 225 | 9.4% |
| sachin5986 by mohitesachin78 | ema | v4 strategy | +32.1% | 35 | 17.6% |
| sonu1997 by Sonu1997 | ema | v4 strategy | +960.7% | 2 | 1,103.8% |
| Moving Average Crossover by Muh4mm4d | ema | v4 strategy | +19,990.1% | 30 | 1,274.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 31 ms | Yes 1,000 bars in 189 ms | 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/// © Seltzer_//@version=4strategy(title="TEMA Cross +HTF Backtest", shorttitle="TEMA_X_+HTF_BT", overlay=true, commission_type=strategy.commission.percent, commission_value=0, initial_capital = 500, default_qty_type=strategy.cash, default_qty_value=500)orderType = input("Longs+Shorts",title="What type of Orders", options=["Longs+Shorts","LongsOnly","ShortsOnly"])isLong = (orderType != "ShortsOnly")isShort = (orderType != "LongsOnly")// Backtest Section {// Backtest inputsFromMonth = input(defval=1, title="From Month", minval=1, maxval=12)FromDay = input(defval=1, title="From Day", minval=1, maxval=31)FromYear = input(defval=2020, title="From Year", minval=2010)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)// Define backtest timewindowstart = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start windowfinish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish windowwindow() => // create function "within window of time" time >= start and time <= finish ? true : false// }//TEMA Section {//LTF SectionxLength = input(20, minval=1, title="Fast Length")xPrice = closexEMA1 = ema(xPrice, xLength)xEMA2 = ema(xEMA1, xLength)xEMA3 = ema(xEMA2, xLength)xnRes = (3 * xEMA1) - (3 * xEMA2) + xEMA3xnResP = plot(xnRes, color=color.green, linewidth=2, title="TEMA1")yLength = input(60, minval=1, title="Slow Length")yPrice = closeyEMA1 = ema(yPrice, yLength)yEMA2 = ema(yEMA1, yLength)yEMA3 = ema(yEMA2, yLength)ynRes = (3 * yEMA1) - (3 * yEMA2) + yEMA3ynResP = plot(ynRes, color=color.red, linewidth=2, title="TEMA2")fill(xnResP, ynResP, color=xnRes > ynRes ? color.green : color.red, transp=65, editable=true)//HTF SectionHTFres = input(defval="D", type=input.resolution, title="HTF Resolution")f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)HTFxLength = input(5, minval=1, title="HTF Fast Length")HTFxPrice = closeHTFxEMA1 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFxPrice, HTFxLength))HTFxEMA2 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFxEMA1, HTFxLength))HTFxEMA3 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFxEMA2, HTFxLength))HTFxnRes = (3 * HTFxEMA1) - (3 * HTFxEMA2) + HTFxEMA3HTFxnResP = plot(HTFxnRes, color=color.yellow, linewidth=1,transp=30, title="TEMA1")HTFyLength = input(15, minval=1, title="HTF Slow Length")HTFyPrice = closeHTFyEMA1 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFyPrice, HTFyLength))HTFyEMA2 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFyEMA1, HTFyLength))HTFyEMA3 = f_secureSecurity(syminfo.tickerid, HTFres, ema(HTFyEMA2, HTFyLength))HTFynRes = (3 * HTFyEMA1) - (3 * HTFyEMA2) + HTFyEMA3HTFynResP = plot(HTFynRes, color=color.purple, linewidth=1, transp=30, title="TEMA2")// HTF Plotfill(HTFxnResP, HTFynResP, color=HTFxnRes > HTFynRes ? color.yellow : color.purple, transp=90, editable=true)bgcolor(HTFxnRes > HTFynRes ? color.yellow : na, transp=90, editable=true)bgcolor(HTFxnRes < HTFynRes ? color.purple : na, transp=90, editable=true)// }// Buy and Sell TriggersLongEntryAlert = xnRes > ynRes and HTFxnRes > HTFynRes and window()LongCloseAlert = xnRes < ynRes and window()ShortEntryAlert = xnRes < ynRes and HTFxnRes < HTFynRes and window()ShortCloseAlert = xnRes > ynRes// Entry & Exit signalsif isLong strategy.entry("Long", strategy.long, when = LongEntryAlert) strategy.close("Long", when = LongCloseAlert)if isShort strategy.entry("Short", strategy.short, when = ShortEntryAlert) strategy.close("Short", when = ShortCloseAlert)