Pine Script v5 · Strategy
GAS Backtest
A Pine Script v5 strategy with no technical-analysis calculation. It has 7 order rules and no entry of its own. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 9 settings; defaults include Grid PCT 1, Max Order 2 and Grid Vol (optional) 0.
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 GAS Backtest repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did GAS Backtest 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. It closed no trades on the other markets either. A backtest, not investment advice.
- Does GAS Backtest run in Pine Script v6?
- No
- The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L35:C3: cannot compare to 'na' with '==' or '!=' (it does not behave as expected). Use the na(x) function instead. The v6 version is shown for reference and is not offered as a download.
- Does GAS Backtest use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does GAS Backtest run in Zeal Terminal?
- Yes
- It compiles in 85 ms and runs 1,000 bars in 28 ms, in the browser.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
- 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, $10,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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Start Date | timestamp("5 Mar 2022 00:00 +0800") | time | order str.format("Sell{0}",i) order str.format("Buy{0}",i) line 39 line 97 line 173 plot |
| Start Date | timestamp("5 Mar 2022 00:00 +0800") | time | cancel_all line 39 line 167 plot |
| Grid PCT | 1 | float · min 0.1 · max 100 · step 0.25 | pct vol |
| Max Order | 2 | int · min 1 | nt nb line 88 line 161 |
| Grid Reward Only | true | bool | order init line 139 line 155 |
| Grid Vol (optional) | 0 | float | vol line 149 line 151 |
| Ref. Grid (optional) | 0 | float · min 0 | n line1 line 99 |
| Apply User setting | false | bool | line 16 |
| Today Only | true | bool | line 13 line 173 |
What it calculates
No technical-analysis calculation: the script works on prices and settings directly.
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 |
|---|---|---|---|
| cancel_all | ( gridLine != gridLine[1] ) | On | 86 |
| order str.format("Sell{0}",i) short | ( gridLine != gridLine[1] ) | On | 91 |
| order str.format("Buy{0}",i) long | ( gridLine != gridLine[1] ) | On | 95 |
| order init long | initBuy == false | On | 156 |
| order str.format("Sell{0}",i) short | time == startDate | On | 163 |
| order str.format("Buy{0}",i) long | time == startDate | On | 166 |
| cancel_all | time == endDate | On | 168 |
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.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- CheckIntrabar recalculationreads realtime-only state. Live values can differ from history.
- 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 | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Similar scripts
No other indicator or strategy here shares enough of its calculations.
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 does not run in Zeal Terminal's engine: Pine Script v6 Error at @L35:C3: cannot compare to 'na' with '==' or '!=' (it does not behave as expected). Use the na(x) function instead. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=5+//@version=6strategy("GAS Backtest",overlay=true, initial_capital=10000, process_orders_on_close=false)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 85 ms | With a workaround 1,000 bars in 28 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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///@version=5strategy("GAS Backtest",overlay=true, initial_capital=10000, process_orders_on_close=false)startDate = input.time(title="Start Date", defval=timestamp("5 Mar 2022 00:00 +0800"))endDate = input.time(title="Start Date", defval=timestamp("5 Mar 2022 00:00 +0800"))gridPct = input.float(title="Grid PCT", defval=1, minval = 0.1, step = 0.25, maxval = 100)maxOrder = input.int(title="Max Order", defval=2, minval=1)initBuy = input.bool(title="Grid Reward Only",defval=true)userVol = input.float(title="Grid Vol (optional)",defval=0)refGrid = input.float(title="Ref. Grid (optional)",defval=0, minval=0)userSetting = input.bool(title="Apply User setting",defval=false)today = input.bool(title="Today Only",defval=true)if today startDate := timenow - ( timenow + 28800000 ) % 86400000 - 1800000 endDate := timenowif userSetting switch syminfo.ticker "SOLUSD" => gridPct := 1 maxOrder := 5 userVol := 1 refGrid := 45 => startDate := time + 120000 if timeframe.period != "1" startDate := time + 120000pct = 1 + gridPct / 100.0gridLine = openvol = 0.0ratio = 2sellCount = 0buyCount = 0gain = 0.0var label labelName = naif labelName[1] != na label.delete(labelName[1]) if time > startDate and time <= endDate vol := vol[1] gridLine := gridLine[1] pct := pct[1] if hour != 16 or minute != 0 buyCount := buyCount[1] sellCount := sellCount[1] gain := gain[1] nt = math.max( math.floor( math.log( high / gridLine ) / math.log( pct ) ), 0) nt := math.min( nt , maxOrder) //highLine = gridLine * math.pow(pct,nt) highLine = gridLine if nt != 0 prePrice = highLine for i = 1 to nt highLine := math.ceil( highLine * pct / syminfo.mintick ) * syminfo.mintick gain += ( highLine - prePrice ) * vol prePrice := highLine gain := math.round( gain * 100 ) * 0.01 sellCount += nt nb = math.min( math.ceil( math.log( low / gridLine ) / math.log( pct ) ), 0) nb := math.max( nb , -1 * maxOrder) //lowLine = gridLine * math.pow(pct,nb) lowLine = gridLine if nb != 0 for i = 1 to -1*nb lowLine := math.floor( lowLine / pct / syminfo.mintick ) * syminfo.mintick buyCount -= nb if ( highLine != gridLine ) and ( lowLine != gridLine ) gridLine := ( highLine - gridLine ) > ( gridLine - lowLine ) ? lowLine : highLine else if highLine != gridLine gridLine := highLine else if lowLine != gridLine gridLine := lowLine if ( gridLine != gridLine[1] ) buyPrice = gridLine sellPrice = gridLine strategy.cancel_all() for i = 1 to maxOrder //sellPrice := sellPrice * pct sellPrice := math.ceil( sellPrice * pct / syminfo.mintick ) * syminfo.mintick strategy.order( str.format("Sell{0}",i), strategy.short, vol, limit=sellPrice) //buyPrice := buyPrice / pct buyPrice := math.floor( buyPrice / pct / syminfo.mintick ) * syminfo.mintick strategy.order( str.format("Buy{0}",i), strategy.long, vol, limit=buyPrice) else if time == startDate //label.new(bar_index, high, syminfo.ticker ) if refGrid != 0 n = math.floor( math.log( gridLine / refGrid ) / math.log( pct ) ) line1 = refGrid line2 = line1 for i = 1 to ( n >= 0 ? n+1 : -1*n ) line2 := line1 line1 := if n >= 0 math.ceil( line1 * pct / syminfo.mintick ) * syminfo.mintick else math.floor( line1 / pct / syminfo.mintick ) * syminfo.mintick // line1 = refGrid * math.pow( pct, n )// line2 = line1 * pct h = high l = low i = 1 while true if ( line1 <= h ) and ( line1 >= l ) gridLine := line1 break if ( line2 <= h ) and ( line2 >= l ) gridLine := line2 break h := high[i] > h ? high[i] : h l := low[i] < l ? low[i] : l i += 1 botBound = gridLine / 1.1 topBound = gridLine * 1.1 nb = math.floor( math.log( gridLine / botBound ) / math.log( pct ) ) vol := gridLine / pct * ( 1 - math.pow( 1/pct, math.max(nb,0) ) ) / ( 1 - 1/pct) nt = math.floor( math.log( topBound / gridLine ) / math.log( pct ) ) if nt < 0 vol -= gridLine / pct * ( 1 - math.pow( 1/pct, -1 - nt) ) / ( 1 - 1/pct ) nt := 0 if initBuy == false if nb < 0 nt -= -1 - nb vol += nt * close else vol += gridLine * ( 1 - math.pow( pct, nt ) ) / ( 1 - pct) if nb < 0 vol -= gridLine * ( 1 - math.pow( pct, -1*nb ) ) / ( 1 - pct ) if userVol == 0 vol := strategy.equity / vol else if userVol > 0 vol := userVol else vol := -1 * userVol * gridPct if initBuy == false strategy.order("init", strategy.long, nt*vol) buyPrice = gridLine sellPrice = gridLine for i = 1 to maxOrder sellPrice := sellPrice * pct strategy.order( str.format("Sell{0}",i), strategy.short, vol, limit=sellPrice) buyPrice := buyPrice / pct strategy.order( str.format("Buy{0}",i), strategy.long, vol, limit=buyPrice)if time == endDate strategy.cancel_all()//plot(line1, color= color.red)//plot(line2)//plot(pct * 100 -10)//plot(syminfo.mintick)if today and ( time >= startDate ) and bar_index >= last_bar_index-1 labelName := label.new(x=bar_index+80, y=high*1.002, text = str.format("Buy : {0}\nSell : {1}\nGain : {2}",buyCount,sellCount,gain), style=label.style_label_center, textcolor = color.white)plot( time >= startDate and time <= endDate? gridLine : na , color = color.yellow, style = plot.style_stepline )//plot(close)