Pine Script v4 · Strategy
Strategy Timeframe
A Pine Script v4 strategy built on Crossover or SMA (close, 14, close, 28) and Crossunder or SMA (close, 14, close, 28). 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 is one contract and pays no commission. 6 settings; defaults include From Month 1, From Day 1 and From Year 2020.
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 Strategy Timeframe repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 11 of 11 trades.
- How did Strategy Timeframe do on BTCUSDT 1D with default settings?
- −16.79%
- From 26 Jan 2024 to 21 Oct 2026, over 42 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Strategy Timeframe 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 Strategy Timeframe use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Strategy Timeframe run in Zeal Terminal?
- Yes
- It compiles in 6 ms and runs 1,000 bars in 22 ms, in the browser.
BTCUSDT · 1D · default settings
- Crossover
- Crossunder
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −16.79%
- −$16,790.50
- Buy and hold
- +102.5%
- same period
- Closed trades
- 42
- 13 won · 29 lost
- Profitable
- 31.0%
- of closed trades
- Profit factor
- 0.89
- gross profit ÷ gross loss
- Max drawdown
- 52.85%
- −$54,413.30
One run with the author's default settings, $100,000 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.
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 crossover → ta.crossover, sma → ta.sma, crossunder → ta.crossunder.
v4 → v5
VerifiedSame 42 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 16: crossover → ta.crossover
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 19: crossunder → ta.crossunder
- Line 19: sma → ta.sma
- Line 19: sma → ta.sma
⋯// © melihtuna-//@version=4+//@version=5strategy("Strategy Timeframe", overlay=true)⋯window() => time >= start and time <= finish ? true : false -longCondition = crossover(sma(close, 14), sma(close, 28))+longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))if (window() and longCondition) strategy.entry("My Long Entry Id", strategy.long)-shortCondition = crossunder(sma(close, 14), sma(close, 28))+shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))if (window() and shortCondition)⋯Same 42 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.int()
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 11: input() → input.int()
⋯// === 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 = 2020, title = "From Year", minval = 2017)+FromYear = input.int(defval = 2020, title = "From Year", minval = 2017)-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)// === FUNCTION EXAMPLE ===⋯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: 42 before, 31 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.
⋯// © melihtuna-//@version=5+//@version=6strategy("Strategy Timeframe", overlay=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 |
|---|---|---|---|
| From Month | 1 | int · min 1 · max 12 | start |
| From Day | 1 | int · min 1 · max 31 | start |
| From Year | 2020 | int · min 2017 | 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 |
What it calculates
- Crossover / SMA (
crossover,ta.crossoverin v5)sma(close, 14), sma(close, 28)longCondition · line 16 - Crossunder / SMA (
crossunder,ta.crossunderin v5)sma(close, 14), sma(close, 28)shortCondition · line 19
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 My Long Entry Id long | (window() and longCondition) | On | 18 |
| Open My Short Entry Id short | (window() and shortCondition) | On | 21 |
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, and the live replay matched history.
- 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 | −16.8% | +102.5% | 42 | 31% | 0.89 | 52.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −12.1% | +5.5% | 45 | 27% | 0.73 | 19.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +1.8% | +9.8% | 38 | 42% | 1.33 | 2.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.1% | +8.4% | 47 | 30% | 0.95 | 0.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.3% | +16.7% | 32 | 59% | 3.11 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.1% | +21.5% | 41 | 41% | 2.04 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 36 | 25% | 1.02 | 1.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.2% | +17.1% | 43 | 30% | 0.55 | 0.3% | 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 | 14 | 43% | $11,975.10 |
| Sideways market | 18 | 28% | −$5,807.41 |
| Falling market | 10 | 20% | −$22,958.20 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
70.3%
The 95th percentile of the worst drawdown when the same 42 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 41.4% to 70.3%; the tick is what actually happened, 50.7%. Order luck alone moves it this much.
All 42 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 29 Mar 2024 70,780.60 | 6 Apr 2024 67,820.60 | 8 | $2,959.97 |
| long | 6 Apr 2024 67,820.60 | 16 Apr 2024 63,420.00 | 10 | −$4,400.64 |
| short | 16 Apr 2024 63,420.00 | 19 May 2024 66,915.20 | 33 | −$3,495.21 |
| long | 19 May 2024 66,915.20 | 17 Jun 2024 66,676.90 | 29 | −$238.34 |
| short | 17 Jun 2024 66,676.90 | 21 Jul 2024 67,140.00 | 34 | −$463.11 |
| long | 21 Jul 2024 67,140.00 | 8 Aug 2024 55,133.80 | 18 | −$12,006.20 |
| short | 8 Aug 2024 55,133.80 | 27 Aug 2024 62,834.00 | 19 | −$7,700.24 |
| long | 27 Aug 2024 62,834.00 | 7 Sep 2024 53,963.00 | 11 | −$8,871.03 |
| short | 7 Sep 2024 53,963.00 | 22 Sep 2024 63,349.00 | 15 | −$9,386.00 |
| long | 22 Sep 2024 63,349.00 | 12 Oct 2024 62,540.00 | 20 | −$808.98 |
| short | 12 Oct 2024 62,540.00 | 18 Oct 2024 67,421.80 | 6 | −$4,881.79 |
| long | 18 Oct 2024 67,421.80 | 30 Dec 2024 93,738.20 | 73 | $26,316.40 |
| short | 30 Dec 2024 93,738.20 | 15 Jan 2025 96,560.90 | 16 | −$2,822.66 |
| long | 15 Jan 2025 96,560.90 | 8 Feb 2025 96,506.80 | 24 | −$54.05 |
| short | 8 Feb 2025 96,506.80 | 31 Mar 2025 82,390.00 | 51 | $14,116.80 |
| long | 31 Mar 2025 82,390.00 | 8 Apr 2025 79,163.20 | 8 | −$3,226.76 |
| short | 8 Apr 2025 79,163.20 | 22 Apr 2025 87,516.20 | 14 | −$8,352.98 |
| long | 22 Apr 2025 87,516.20 | 9 Jun 2025 105,734.00 | 48 | $18,217.80 |
| short | 9 Jun 2025 105,734.00 | 20 Jun 2025 104,659.00 | 11 | $1,075.42 |
| long | 20 Jun 2025 104,659.00 | 24 Jun 2025 105,334.00 | 4 | $675.35 |
| short | 24 Jun 2025 105,334.00 | 5 Jul 2025 107,984.00 | 11 | −$2,650.31 |
| long | 5 Jul 2025 107,984.00 | 6 Aug 2025 114,130.00 | 32 | $6,145.50 |
| short | 6 Aug 2025 114,130.00 | 17 Aug 2025 117,381.00 | 11 | −$3,250.91 |
| long | 17 Aug 2025 117,381.00 | 27 Aug 2025 111,763.00 | 10 | −$5,617.44 |
| short | 27 Aug 2025 111,763.00 | 15 Sep 2025 115,268.00 | 19 | −$3,504.79 |
| long | 15 Sep 2025 115,268.00 | 1 Oct 2025 114,049.00 | 16 | −$1,219.07 |
| short | 1 Oct 2025 114,049.00 | 6 Oct 2025 123,482.00 | 5 | −$9,433.38 |
| long | 6 Oct 2025 123,482.00 | 20 Oct 2025 108,643.00 | 14 | −$14,839.60 |
| short | 20 Oct 2025 108,643.00 | 11 Dec 2025 92,015.40 | 52 | $16,627.40 |
| long | 11 Dec 2025 92,015.40 | 20 Dec 2025 88,136.90 | 9 | −$3,878.43 |
| short | 20 Dec 2025 88,136.90 | 7 Jan 2026 93,748.00 | 18 | −$5,611.02 |
| long | 7 Jan 2026 93,748.00 | 29 Jan 2026 89,300.00 | 22 | −$4,447.97 |
| short | 29 Jan 2026 89,300.00 | 10 Mar 2026 68,432.20 | 40 | $20,867.80 |
| long | 10 Mar 2026 68,432.20 | 31 Mar 2026 66,797.40 | 21 | −$1,634.78 |
| short | 31 Mar 2026 66,797.40 | 14 Apr 2026 74,418.00 | 14 | −$7,620.62 |
| long | 14 Apr 2026 74,418.00 | 24 May 2026 76,752.00 | 40 | $2,334.00 |
| short | 24 May 2026 76,752.00 | 14 Jul 2026 62,334.50 | 51 | $14,417.50 |
| long | 14 Jul 2026 62,334.50 | 5 Aug 2026 64,106.60 | 22 | $1,772.03 |
| short | 5 Aug 2026 64,106.60 | 20 Aug 2026 69,334.80 | 15 | −$5,228.23 |
| long | 20 Aug 2026 69,334.80 | 18 Sep 2026 76,417.00 | 29 | $7,082.23 |
| short | 18 Sep 2026 76,417.00 | 23 Sep 2026 86,208.60 | 5 | −$9,791.56 |
| long | 23 Sep 2026 86,208.60 | 16 Oct 2026 82,245.90 | 23 | −$3,962.64 |
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 |
|---|---|---|---|---|---|
| Strategy Timeframe this page | — | v4 strategy | −16.8% | 42 | 52.8% |
| Follow the Crypto Shorts by xtradernet | sma | v4 strategy | No trades on this market | ||
| buy down and sell high trend 200 sma and ma cross by willi130503 | sma | v4 strategy | No trades on this market | ||
| Mayer Multiple Strategy by xtradernet | sma | v4 strategy | No trades on this market | ||
| Moving Average Cross Strategy early Closing by Geduldtrader | sma | v4 strategy | +73.8% | 20 | 151.5% |
| MFI Simple Strategy by OztheWoz | sma | v5 strategy | −0.2% | 28 | 3.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 6 ms | Yes 1,000 bars in 22 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/// © melihtuna//@version=4strategy("Strategy Timeframe", overlay=true)// === 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 = 2020, title = "From Year", minval = 2017)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)// === FUNCTION EXAMPLE ===start = timestamp(FromYear, FromMonth, FromDay, 00, 00) finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) window() => time >= start and time <= finish ? true : false longCondition = crossover(sma(close, 14), sma(close, 28))if (window() and longCondition) strategy.entry("My Long Entry Id", strategy.long)shortCondition = crossunder(sma(close, 14), sma(close, 28))if (window() and shortCondition) strategy.entry("My Short Entry Id", strategy.short)