Pine Script v4 · Strategy
(IK) Grid Script
A Pine Script v4 strategy built on SMA (close, _bl). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 1 in cash and pays 0.1% commission. 7 settings; defaults include (Auto) Bound Source Hi & Low, (Auto) Bound Lookback 250 and (Auto) Bound Deviation 0.10.
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 (IK) Grid Script repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did (IK) Grid Script 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 entry conditions never all held on this data. It closed no trades on the other markets either. A backtest, not investment advice.
- Can (IK) Grid Script be converted to Pine Script v5?
- Not tested
- The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
- Does (IK) Grid Script use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does (IK) Grid Script run in Zeal Terminal?
- Yes
- It compiles in 102 ms and runs 1,000 bars in 158 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Plot
- Plot
- Plot
- Plot
- Plot
- Plot
- Plot
- 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, $100 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.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses long =, which v5 renamed line 52
long = on line 52. v5 writes it as direction = strategy.long or strategy.short.
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 highest → ta.highest, lowest → ta.lowest, sma → ta.sma, tostring → str.tostring.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.bool) → input.bool()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.float) → input.float()
- Line 10: input(type = input.float) → input.float()
- Line 11: input(type = input.float) → input.float()
9 more edits
- Line 12: input(type = input.integer) → input.int()
- Line 17: highest → ta.highest
- Line 17: lowest → ta.lowest
- Line 19: sma → ta.sma
- Line 52: tostring → str.tostring
- Line 52: long = true → direction = strategy.long
- Line 52: tostring → str.tostring
- Line 57: tostring → str.tostring
- Line 57: tostring → str.tostring
⋯// © tapRoot_coding - Ian Kuzmik-//@version=4+//@version=5strategy("(IK) Grid Script", overlay=true, pyramiding=14, close_entries_rule="ANY", default_qty_type=strategy.cash, initial_capital=100.0, currency="USD", commission_type=strategy.commission.percent, commission_value=0.1)// == INPUT == -i_autoBounds = input(group="Grid Bounds", title="Use Auto Bounds?", defval=false, type=input.bool) // calculate upper and lower bound of the grid automatically? This will theorhetically be less profitable, but will certainly require less attention+i_autoBounds = input.bool(group="Grid Bounds", title="Use Auto Bounds?", defval=false) // calculate upper and lower bound of the grid automatically? This will theorhetically be less profitable, but will certainly require less attentioni_boundSrc = input(group="Grid Bounds", title="(Auto) Bound Source", defval="Hi & Low", options=["Hi & Low", "Average"]) // should bounds of the auto grid be calculated from recent High & Low, or from a Simple Moving Average-i_boundLookback = input(group="Grid Bounds", title="(Auto) Bound Lookback", defval=250, type=input.integer, maxval=500, minval=0) // when calculating auto grid bounds, how far back should we look for a High & Low, or what should the length be of our sma +i_boundLookback = input.int(group="Grid Bounds", title="(Auto) Bound Lookback", defval=250, maxval=500, minval=0) // when calculating auto grid bounds, how far back should we look for a High & Low, or what should the length be of our sma -i_boundDev = input(group="Grid Bounds", title="(Auto) Bound Deviation", defval=0.10, type=input.float, maxval=1, minval=-1) // if sourcing auto bounds from High & Low, this percentage will (positive) widen or (negative) narrow the bound limits. If sourcing from Average, this is the deviation (up and down) from the sma, and CANNOT be negative. +i_boundDev = input.float(group="Grid Bounds", title="(Auto) Bound Deviation", defval=0.10, maxval=1, minval=-1) // if sourcing auto bounds from High & Low, this percentage will (positive) widen or (negative) narrow the bound limits. If sourcing from Average, this is the deviation (up and down) from the sma, and CANNOT be negative. -i_upperBound = input(group="Grid Bounds", title="(Manual) Upper Boundry", defval=0.285, type=input.float) // for manual grid bounds only. The upperbound price of your grid+i_upperBound = input.float(group="Grid Bounds", title="(Manual) Upper Boundry", defval=0.285) // for manual grid bounds only. The upperbound price of your grid-i_lowerBound = input(group="Grid Bounds", title="(Manual) Lower Boundry", defval=0.225, type=input.float) // for manual grid bounds only. The lowerbound price of your grid. +i_lowerBound = input.float(group="Grid Bounds", title="(Manual) Lower Boundry", defval=0.225) // for manual grid bounds only. The lowerbound price of your grid. -i_gridQty = input(group="Grid Lines", title="Grid Line Quantity", defval=8, maxval=15, minval=3, type=input.integer) // how many grid lines are in your grid+i_gridQty = input.int(group="Grid Lines", title="Grid Line Quantity", defval=8, maxval=15, minval=3) // how many grid lines are in your grid// == FUNCTIONS ==⋯ if _bs == "Hi & Low" - _up ? highest(close, _bl) * (1 + _bd) : lowest(close, _bl) * (1 - _bd)+ _up ? ta.highest(close, _bl) * (1 + _bd) : ta.lowest(close, _bl) * (1 - _bd) else- avg = sma(close, _bl)+ avg = ta.sma(close, _bl) _up ? avg * (1 + _bd) : avg * (1 - _bd)⋯ array.set(orderArr, buyId, true)- strategy.entry(id=tostring(buyId), long=true, qty=(strategy.initial_capital/(i_gridQty-1))/close, comment="#"+tostring(buyId))+ strategy.entry(id=str.tostring(buyId), direction = strategy.long, qty=(strategy.initial_capital/(i_gridQty-1))/close, comment="#"+str.tostring(buyId)) if close > array.get(gridLineArr, i) and i != 0 ⋯ array.set(orderArr, sellId, false)- strategy.close(id=tostring(sellId), comment="#"+tostring(sellId))+ strategy.close(id=str.tostring(sellId), comment="#"+str.tostring(sellId)) ⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 7: input() → input.string()
⋯i_autoBounds = input.bool(group="Grid Bounds", title="Use Auto Bounds?", defval=false) // calculate upper and lower bound of the grid automatically? This will theorhetically be less profitable, but will certainly require less attention-i_boundSrc = input(group="Grid Bounds", title="(Auto) Bound Source", defval="Hi & Low", options=["Hi & Low", "Average"]) // should bounds of the auto grid be calculated from recent High & Low, or from a Simple Moving Average+i_boundSrc = input.string(group="Grid Bounds", title="(Auto) Bound Source", defval="Hi & Low", options=["Hi & Low", "Average"]) // should bounds of the auto grid be calculated from recent High & Low, or from a Simple Moving Averagei_boundLookback = input.int(group="Grid Bounds", title="(Auto) Bound Lookback", defval=250, maxval=500, minval=0) // when calculating auto grid bounds, how far back should we look for a High & Low, or what should the length be of our sma ⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © tapRoot_coding - Ian Kuzmik-//@version=5+//@version=6strategy("(IK) Grid Script", overlay=true, pyramiding=14, close_entries_rule="ANY", default_qty_type=strategy.cash, initial_capital=100.0, currency="USD", commission_type=strategy.commission.percent, commission_value=0.1)⋯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 |
|---|---|---|---|
| Grid Bounds | |||
| Use Auto Bounds? | false | bool | upperBound lowerBound line 61 |
| (Auto) Bound Source | Hi & Low | string · options ["Hi & Low", "Average"] | upperBound lowerBound |
| (Auto) Bound Lookback | 250 | int (written input.integer) · min 0 · max 500 | upperBound lowerBound |
| (Auto) Bound Deviation | 0.10 | float · min -1 · max 1 | upperBound lowerBound |
| (Manual) Upper Boundry | 0.285 | float | upperBound |
| (Manual) Lower Boundry | 0.225 | float | lowerBound |
| Grid Lines | |||
| Grid Line Quantity | 8 | int (written input.integer) · min 3 · max 15 | gridWidth gridLineArr orderArr entry tostring(buyId) |
What it calculates
- SMA (
sma,ta.smain v5)close, _blavg · 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 tostring(buyId) long | close < array.get(gridLineArr, i) and not array.get(orderArr, i) and i < (array.size(gridLineArr) - 1) | On | 52 |
| Close tostring(sellId) | array.get(orderArr, i-1) | On | 57 |
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.
- 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 | 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
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 |
|---|---|---|---|---|---|
| (IK) Grid Script this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| 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 102 ms | With a workaround 1,000 bars in 158 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 52) so the engine trades the way the script says.
“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/// © tapRoot_coding - Ian Kuzmik//@version=4strategy("(IK) Grid Script", overlay=true, pyramiding=14, close_entries_rule="ANY", default_qty_type=strategy.cash, initial_capital=100.0, currency="USD", commission_type=strategy.commission.percent, commission_value=0.1)// == INPUT == i_autoBounds = input(group="Grid Bounds", title="Use Auto Bounds?", defval=false, type=input.bool) // calculate upper and lower bound of the grid automatically? This will theorhetically be less profitable, but will certainly require less attentioni_boundSrc = input(group="Grid Bounds", title="(Auto) Bound Source", defval="Hi & Low", options=["Hi & Low", "Average"]) // should bounds of the auto grid be calculated from recent High & Low, or from a Simple Moving Averagei_boundLookback = input(group="Grid Bounds", title="(Auto) Bound Lookback", defval=250, type=input.integer, maxval=500, minval=0) // when calculating auto grid bounds, how far back should we look for a High & Low, or what should the length be of our sma i_boundDev = input(group="Grid Bounds", title="(Auto) Bound Deviation", defval=0.10, type=input.float, maxval=1, minval=-1) // if sourcing auto bounds from High & Low, this percentage will (positive) widen or (negative) narrow the bound limits. If sourcing from Average, this is the deviation (up and down) from the sma, and CANNOT be negative. i_upperBound = input(group="Grid Bounds", title="(Manual) Upper Boundry", defval=0.285, type=input.float) // for manual grid bounds only. The upperbound price of your gridi_lowerBound = input(group="Grid Bounds", title="(Manual) Lower Boundry", defval=0.225, type=input.float) // for manual grid bounds only. The lowerbound price of your grid. i_gridQty = input(group="Grid Lines", title="Grid Line Quantity", defval=8, maxval=15, minval=3, type=input.integer) // how many grid lines are in your grid// == FUNCTIONS ==// for auto grid only. return the upperbound if _up == true, or the lowerbound if _up == falsef_getGridBounds(_bs, _bl, _bd, _up) => if _bs == "Hi & Low" _up ? highest(close, _bl) * (1 + _bd) : lowest(close, _bl) * (1 - _bd) else avg = sma(close, _bl) _up ? avg * (1 + _bd) : avg * (1 - _bd)// using the lowerbound, grid width, and grid line quantity, build an array of prices (grid lines) from the bottom upf_buildGrid(_lb, _gw, _gq) => gridArr = array.new_float(0) for i=0 to _gq-1 array.push(gridArr, _lb+(_gw*i)) gridArr // return an array that contains the closest grid lines above and below current price (not counting the line price is currently on). This is solely for plotting purposesf_getNearGridLines(_gridArr, _price) => arr = array.new_int(3) for i = 0 to array.size(_gridArr)-1 if array.get(_gridArr, i) > _price array.set(arr, 0, i == array.size(_gridArr)-1 ? i : i+1) array.set(arr, 1, i == 0 ? i : i-1) break arr // == VARIABLES ==var upperBound = i_autoBounds ? f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, true) : i_upperBound // upperbound of our gridvar lowerBound = i_autoBounds ? f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, false) : i_lowerBound // lowerbound of our gridvar gridWidth = (upperBound - lowerBound)/(i_gridQty-1) // space between lines in our gridvar gridLineArr = f_buildGrid(lowerBound, gridWidth, i_gridQty) // an array of prices that correspond to our grid linesvar orderArr = array.new_bool(i_gridQty, false) // a boolean array that indicates if there is an open order corresponding to each grid linevar closeLineArr = f_getNearGridLines(gridLineArr, close) // for plotting purposes - an array of 2 indices that correspond to grid lines near pricevar nearTopGridLine = array.get(closeLineArr, 0) // for plotting purposes - the index (in our grid line array) of the closest grid line above current price var nearBotGridLine = array.get(closeLineArr, 1) // for plotting purposes - the index (in our grid line array) of the closest grid line below current price // == LOGIC ==// ENTRY & EXITfor i = 0 to (array.size(gridLineArr) - 1) if close < array.get(gridLineArr, i) and not array.get(orderArr, i) and i < (array.size(gridLineArr) - 1) buyId = i array.set(orderArr, buyId, true) strategy.entry(id=tostring(buyId), long=true, qty=(strategy.initial_capital/(i_gridQty-1))/close, comment="#"+tostring(buyId)) if close > array.get(gridLineArr, i) and i != 0 if array.get(orderArr, i-1) sellId = i-1 array.set(orderArr, sellId, false) strategy.close(id=tostring(sellId), comment="#"+tostring(sellId)) // MAINTAIN GRID // update auto grid (if active)if i_autoBounds upperBound := f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, true) lowerBound := f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, false) gridWidth := (upperBound - lowerBound)/(i_gridQty-1) gridLineArr := f_buildGrid(lowerBound, gridWidth, i_gridQty)// update near grid lines (for plotting purposes)closeLineArr := f_getNearGridLines(gridLineArr, close)nearTopGridLine := array.get(closeLineArr, 0)nearBotGridLine := array.get(closeLineArr, 1)// plot grid linesplot(array.size(gridLineArr) > 0 ? array.get(gridLineArr,0) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 1 ? array.get(gridLineArr,1) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 2 ? array.get(gridLineArr,2) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 3 ? array.get(gridLineArr,3) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 4 ? array.get(gridLineArr,4) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 5 ? array.get(gridLineArr,5) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 6 ? array.get(gridLineArr,6) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 7 ? array.get(gridLineArr,7) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 8 ? array.get(gridLineArr,8) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 9 ? array.get(gridLineArr,9) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 10 ? array.get(gridLineArr,10) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 11 ? array.get(gridLineArr,11) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 12 ? array.get(gridLineArr,12) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 13 ? array.get(gridLineArr,13) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)plot(array.size(gridLineArr) > 14 ? array.get(gridLineArr,14) : na, color=bar_index % 2 == 1 ? color.rgb(255,255,0,50) : #00000000)// plot near grid linesplot(array.get(gridLineArr, nearTopGridLine), color=color.rgb(255,0,255,20), style=plot.style_stepline, linewidth=2)plot(array.get(gridLineArr, nearBotGridLine), color=color.rgb(0,0,255,20), style=plot.style_stepline, linewidth=2)