Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (0)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$996,688$2,025,273Equity from $1,000,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference.
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.

closecloseprice datatimetimeprice datalongConditionCrossover / SMA→ longConditionshortConditionCrossunder / SMA→ shortConditionentryentryvalueentry:My Long Entry IdOpen My Long Entry Idstrategy.entryentry:My Short Entry IdOpen My Short Entry Idstrategy.entryclose:entryClose entrystrategy.close

Order rules

OrderFires whenBy defaultLine
Open My Long Entry Id long(longCondition)On7
Open My Short Entry Id short(shortCondition)On10
Close entrytime - strategy.opentrades.entry_time(i) >= timeoutS*1000On16

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.

12/12trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D−0.3%+102.5%4353%0.911.5%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.4%+5.5%4637%0.610.7%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+0.1%+9.8%3949%1.450.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.0%+8.4%4854%0.890.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.0%+16.7%3355%1.030.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−0.0%+21.5%4248%0.850.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.0%+148.3%3741%0.710.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%4452%1.330.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 barsTradesWonP&L
Rising market1547%−$6,999.31
Sideways market1663%−$9,317.56
Falling market1250%$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.

0%3%
All 43 trades
SideEntryExitBarsP&L
short28 Mar 2024 70,780.6029 Mar 2024 69,850.501$930.06
long5 Apr 2024 67,820.606 Apr 2024 68,896.001$1,075.38
short15 Apr 2024 63,420.0016 Apr 2024 63,793.401−$373.40
long18 May 2024 66,915.2019 May 2024 66,274.001−$641.19
short16 Jun 2024 66,676.9017 Jun 2024 66,504.301$172.54
long20 Jul 2024 67,140.0021 Jul 2024 68,165.301$1,025.38
short7 Aug 2024 55,134.208 Aug 2024 61,686.001−$6,551.83
long26 Aug 2024 62,834.0027 Aug 2024 59,415.001−$3,419.00
short6 Sep 2024 53,963.007 Sep 2024 54,160.901−$197.89
long21 Sep 2024 63,349.0022 Sep 2024 63,578.801$229.80
short11 Oct 2024 62,540.0012 Oct 2024 63,206.201−$666.22
long17 Oct 2024 67,421.8018 Oct 2024 68,428.001$1,006.22
short29 Dec 2024 93,738.2030 Dec 2024 92,792.101$946.15
long14 Jan 2025 96,560.9015 Jan 2025 100,497.001$3,936.49
short7 Feb 2025 96,506.808 Feb 2025 96,444.701$62.06
long30 Mar 2025 82,390.0031 Mar 2025 82,550.001$160.02
short7 Apr 2025 79,163.208 Apr 2025 76,322.401$2,840.82
long21 Apr 2025 87,516.2022 Apr 2025 93,443.001$5,926.76
short8 Jun 2025 105,734.009 Jun 2025 110,263.001−$4,529.02
long19 Jun 2025 104,659.0020 Jun 2025 103,298.001−$1,360.60
short23 Jun 2025 105,334.0024 Jun 2025 106,083.001−$749.07
long4 Jul 2025 107,984.005 Jul 2025 108,198.001$213.88
short5 Aug 2025 114,130.006 Aug 2025 114,992.001−$862.52
long16 Aug 2025 117,381.0017 Aug 2025 117,405.001$24.35
short26 Aug 2025 111,763.0027 Aug 2025 111,262.001$501.21
long14 Sep 2025 115,268.0015 Sep 2025 115,350.001$81.70
short30 Sep 2025 114,049.001 Oct 2025 118,595.001−$4,546.06
long5 Oct 2025 123,482.006 Oct 2025 124,659.001$1,176.23
short19 Oct 2025 108,643.0020 Oct 2025 110,532.001−$1,889.31
long10 Dec 2025 92,015.4011 Dec 2025 92,513.401$498.01
short19 Dec 2025 88,136.9020 Dec 2025 88,360.901−$223.96
long6 Jan 2026 93,748.007 Jan 2026 91,364.201−$2,383.81
short28 Jan 2026 89,300.0029 Jan 2026 84,650.201$4,649.83
long9 Mar 2026 68,432.2010 Mar 2026 69,948.601$1,516.47
short30 Mar 2026 66,797.4031 Mar 2026 68,284.501−$1,487.11
long13 Apr 2026 74,418.0014 Apr 2026 74,131.601−$286.44
short23 May 2026 76,752.0024 May 2026 77,065.001−$312.95
long13 Jul 2026 62,334.5014 Jul 2026 65,044.001$2,709.46
short4 Aug 2026 64,106.605 Aug 2026 64,665.201−$558.67
long19 Aug 2026 69,334.8020 Aug 2026 73,025.101$3,690.36
short17 Sep 2026 76,417.0018 Sep 2026 80,883.901−$4,466.86
long22 Sep 2026 86,208.6023 Sep 2026 84,397.601−$1,810.96
short15 Oct 2026 82,245.9016 Oct 2026 81,615.601$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.

ScriptSharesTypeNetTradesMax drawdown
The strategy example. Close position by timeout this page—v5 strategy−0.3%431.5%
Follow the Crypto Shorts by xtradernetsmav4 strategyNo trades on this market
buy down and sell high trend 200 sma and ma cross by willi130503smav4 strategyNo trades on this market
Mayer Multiple Strategy by xtradernetsmav4 strategyNo trades on this market
Moving Average Cross Strategy early Closing by Geduldtradersmav4 strategy+73.8%20151.5%
MFI Simple Strategy by OztheWozsmav5 strategy−0.2%283.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

Verified

Nothing 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 9 msYes 1,000 bars in 16 msNot measured
PineTSNot testedNot testedNot 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