Pine Script v5 · Strategy
The strategy example. Close position by timeout
A Pine Script v5 strategy built on Crossover or SMA (close, 14, close, 28) and Crossunder or SMA (close, 14, close, 28). It has 3 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. It has no settings.
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 The strategy example. Close position by timeout repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 12 of 12 trades.
- How did The strategy example. Close position by timeout do on BTCUSDT 1D with default settings?
- −0.33%
- From 26 Jan 2024 to 21 Oct 2026, over 43 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does The strategy example. Close position by timeout 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: 43 before, 53 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does The strategy example. Close position by timeout use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does The strategy example. Close position by timeout run in Zeal Terminal?
- Yes
- It compiles in 9 ms and runs 1,000 bars in 16 ms, in the browser.
BTCUSDT · 1D · default settings
- Crossover
- Crossunder
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −0.33%
- −$3,313.40
- Buy and hold
- +102.5%
- same period
- Closed trades
- 43
- 23 won · 20 lost
- Profitable
- 53.5%
- of closed trades
- Profit factor
- 0.91
- gross profit ÷ gross loss
- Max drawdown
- 1.46%
- −$14,737.60
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.
-
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.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
The script declares no settings.
What it calculates
- Crossover / SMAta.sma(close, 14), ta.sma(close, 28)
longCondition · line 5 - Crossunder / SMAta.sma(close, 14), ta.sma(close, 28)
shortCondition · line 8
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 | (longCondition) | On | 7 |
| Open My Short Entry Id short | (shortCondition) | On | 10 |
| Close entry | time - strategy.opentrades.entry_time(i) >= timeoutS*1000 | On | 16 |
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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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.3% | +102.5% | 43 | 53% | 0.91 | 1.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.4% | +5.5% | 46 | 37% | 0.61 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.1% | +9.8% | 39 | 49% | 1.45 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.0% | +8.4% | 48 | 54% | 0.89 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 33 | 55% | 1.03 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 42 | 48% | 0.85 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 37 | 41% | 0.71 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 44 | 52% | 1.33 | 0.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 | 15 | 47% | −$6,999.31 |
| Sideways market | 16 | 63% | −$9,317.56 |
| Falling market | 12 | 50% | $13,003.50 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
2.4%
The 95th percentile of the worst drawdown when the same 43 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.1% to 2.4%; the tick is what actually happened, 1.4%. Order luck alone moves it this much.
All 43 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 28 Mar 2024 70,780.60 | 29 Mar 2024 69,850.50 | 1 | $930.06 |
| long | 5 Apr 2024 67,820.60 | 6 Apr 2024 68,896.00 | 1 | $1,075.38 |
| short | 15 Apr 2024 63,420.00 | 16 Apr 2024 63,793.40 | 1 | −$373.40 |
| long | 18 May 2024 66,915.20 | 19 May 2024 66,274.00 | 1 | −$641.19 |
| short | 16 Jun 2024 66,676.90 | 17 Jun 2024 66,504.30 | 1 | $172.54 |
| long | 20 Jul 2024 67,140.00 | 21 Jul 2024 68,165.30 | 1 | $1,025.38 |
| short | 7 Aug 2024 55,134.20 | 8 Aug 2024 61,686.00 | 1 | −$6,551.83 |
| long | 26 Aug 2024 62,834.00 | 27 Aug 2024 59,415.00 | 1 | −$3,419.00 |
| short | 6 Sep 2024 53,963.00 | 7 Sep 2024 54,160.90 | 1 | −$197.89 |
| long | 21 Sep 2024 63,349.00 | 22 Sep 2024 63,578.80 | 1 | $229.80 |
| short | 11 Oct 2024 62,540.00 | 12 Oct 2024 63,206.20 | 1 | −$666.22 |
| long | 17 Oct 2024 67,421.80 | 18 Oct 2024 68,428.00 | 1 | $1,006.22 |
| short | 29 Dec 2024 93,738.20 | 30 Dec 2024 92,792.10 | 1 | $946.15 |
| long | 14 Jan 2025 96,560.90 | 15 Jan 2025 100,497.00 | 1 | $3,936.49 |
| short | 7 Feb 2025 96,506.80 | 8 Feb 2025 96,444.70 | 1 | $62.06 |
| long | 30 Mar 2025 82,390.00 | 31 Mar 2025 82,550.00 | 1 | $160.02 |
| short | 7 Apr 2025 79,163.20 | 8 Apr 2025 76,322.40 | 1 | $2,840.82 |
| long | 21 Apr 2025 87,516.20 | 22 Apr 2025 93,443.00 | 1 | $5,926.76 |
| short | 8 Jun 2025 105,734.00 | 9 Jun 2025 110,263.00 | 1 | −$4,529.02 |
| long | 19 Jun 2025 104,659.00 | 20 Jun 2025 103,298.00 | 1 | −$1,360.60 |
| short | 23 Jun 2025 105,334.00 | 24 Jun 2025 106,083.00 | 1 | −$749.07 |
| long | 4 Jul 2025 107,984.00 | 5 Jul 2025 108,198.00 | 1 | $213.88 |
| short | 5 Aug 2025 114,130.00 | 6 Aug 2025 114,992.00 | 1 | −$862.52 |
| long | 16 Aug 2025 117,381.00 | 17 Aug 2025 117,405.00 | 1 | $24.35 |
| short | 26 Aug 2025 111,763.00 | 27 Aug 2025 111,262.00 | 1 | $501.21 |
| long | 14 Sep 2025 115,268.00 | 15 Sep 2025 115,350.00 | 1 | $81.70 |
| short | 30 Sep 2025 114,049.00 | 1 Oct 2025 118,595.00 | 1 | −$4,546.06 |
| long | 5 Oct 2025 123,482.00 | 6 Oct 2025 124,659.00 | 1 | $1,176.23 |
| short | 19 Oct 2025 108,643.00 | 20 Oct 2025 110,532.00 | 1 | −$1,889.31 |
| long | 10 Dec 2025 92,015.40 | 11 Dec 2025 92,513.40 | 1 | $498.01 |
| short | 19 Dec 2025 88,136.90 | 20 Dec 2025 88,360.90 | 1 | −$223.96 |
| long | 6 Jan 2026 93,748.00 | 7 Jan 2026 91,364.20 | 1 | −$2,383.81 |
| short | 28 Jan 2026 89,300.00 | 29 Jan 2026 84,650.20 | 1 | $4,649.83 |
| long | 9 Mar 2026 68,432.20 | 10 Mar 2026 69,948.60 | 1 | $1,516.47 |
| short | 30 Mar 2026 66,797.40 | 31 Mar 2026 68,284.50 | 1 | −$1,487.11 |
| long | 13 Apr 2026 74,418.00 | 14 Apr 2026 74,131.60 | 1 | −$286.44 |
| short | 23 May 2026 76,752.00 | 24 May 2026 77,065.00 | 1 | −$312.95 |
| long | 13 Jul 2026 62,334.50 | 14 Jul 2026 65,044.00 | 1 | $2,709.46 |
| short | 4 Aug 2026 64,106.60 | 5 Aug 2026 64,665.20 | 1 | −$558.67 |
| long | 19 Aug 2026 69,334.80 | 20 Aug 2026 73,025.10 | 1 | $3,690.36 |
| short | 17 Sep 2026 76,417.00 | 18 Sep 2026 80,883.90 | 1 | −$4,466.86 |
| long | 22 Sep 2026 86,208.60 | 23 Sep 2026 84,397.60 | 1 | −$1,810.96 |
| short | 15 Oct 2026 82,245.90 | 16 Oct 2026 81,615.60 | 1 | $630.29 |
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 |
|---|---|---|---|---|---|
| The strategy example. Close position by timeout this page | — | v5 strategy | −0.3% | 43 | 1.5% |
| 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% |
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 runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 43 before, 53 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.
⋯// © adolgov-//@version=5+//@version=6strategy("The strategy example. Close position by timeout", overlay=true, margin_long=100, margin_short=100, process_orders_on_close=true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | Yes 1,000 bars in 16 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/// © adolgov//@version=5strategy("The strategy example. Close position by timeout", overlay=true, margin_long=100, margin_short=100, process_orders_on_close=true)longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))if (longCondition) strategy.entry("My Long Entry Id", strategy.long)shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))if (shortCondition) strategy.entry("My Short Entry Id", strategy.short)closePositionAfter(timeoutS)=> if strategy.opentrades > 0 for i = 0 to strategy.opentrades-1 if time - strategy.opentrades.entry_time(i) >= timeoutS*1000 entry = strategy.opentrades.entry_id(i) strategy.close(entry, comment = str.format("Close \"{0}\" by timeout {1}s", entry, timeoutS)) closePositionAfter(120) // close position after 120 sec