Pine Script v5 · Strategy
grid strategy long
A Pine Script v5 strategy built on EMA (close, 169). It has 7 order rules, 3 entries among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 1 setting; defaults include 网格大小是多少倍ATR? 1.
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 grid strategy long repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did grid strategy long 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. On SOLUSDT 1D it did trade; that row of the markets table below has its figures. A backtest, not investment advice.
- Does grid strategy long run in Pine Script v6?
- Yes
- Same 50 trades and the same P&L on SOLUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does grid strategy long use a stop loss?
- Yes
- strategy.exit sets a stop on line 85, 86, 87.
- Does grid strategy long run in Zeal Terminal?
- Yes
- It compiles in 36 ms and runs 1,000 bars in 46 ms, in the browser.
BTCUSDT · 1D · default settings
- 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, $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 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 |
|---|---|---|---|
| 网格大小是多少倍ATR? | 1 | float | grid_size |
What it calculates
- EMAclose, 169
ema169 · line 9 - EMAclose, 144
ema144 · line 10 - EMAclose, 12
ema12 · line 11 - EMAclose, 576
ema576 · line 12 - EMAclose, 676
ema676 · line 13 - EMAmtr, 30
atr · 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 open1 long | is_entry0 and not is_entry | On | 81 |
| Open open2 long | is_entry0 and not is_entry | On | 82 |
| Open open3 long | is_entry0 and not is_entry | On | 83 |
| Exit close1 stop and limit | is_entry0 and not is_entry | On | 85 |
| Exit close2 stop and limit | is_entry0 and not is_entry | On | 86 |
| Exit close3 stop and limit | is_entry0 and not is_entry | On | 87 |
| Close | is_entry and close <= price_stop_loss | On | 91 |
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.1% | +16.7% | 50 | 66% | 1.84 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 49 | 63% | 2.25 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 52 | 63% | 1.84 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 45 | 56% | 1.07 | 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 |
|---|---|---|---|---|---|
| grid strategy long this page | — | v5 strategy | 0.0% | 0 | 0.0% |
| EMA Slope AyE by fajardoarelys | ema | v4 strategy | No trades on this market | ||
| ROoT by realisticDove62527 | ema | v5 strategy | −4.7% | 225 | 9.4% |
| sachin5986 by mohitesachin78 | ema | v4 strategy | +32.1% | 35 | 17.6% |
| sonu1997 by Sonu1997 | ema | v4 strategy | +960.7% | 2 | 1,103.8% |
| Moving Average Crossover by Muh4mm4d | ema | v4 strategy | +19,990.1% | 30 | 1,274.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?
Yes
Same 50 trades and the same P&L on SOLUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © zxcvbnm3260-//@version=5+//@version=6strategy("grid strategy long", overlay=true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 36 ms | With a workaround 1,000 bars in 46 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/// © zxcvbnm3260//@version=5strategy("grid strategy long", overlay=true)// 版本更新记录:// v1.0 2021/11/09 只做多、不做空,选择大趋势向上的时间段。网格大小默认为1倍ATR,往下1、2、3个网格吃单,第5个网格止损。空仓时到达往上一个网格则网格整体抬升。(Only go long, not short, choose a time period when the general trend is up. The default grid size is 1x ATR, the next one, two, and three grids will take orders, and the fifth grid will stop loss. When the empty position reaches the upper grid, the grid as a whole rises.)X_ATR = input.float(title='网格大小是多少倍ATR?', defval = 1)// 1.基础变量ema169 = ta.ema(close, 169)ema144 = ta.ema(close, 144)ema12 = ta.ema(close, 12)ema576 = ta.ema(close, 576)ema676 = ta.ema(close, 676)plot(ema169, color=color.new(color.orange, 0), linewidth=2)// plot(ema144, color=color.orange)plot(ema12, color=color.blue)// plot(ema676, color=color.orange, linewidth=1)mtr = math.max(high - low, math.abs(close[1] - high), math.abs(close[1] - low))atr = ta.ema(mtr, 30)is_0930 = hour(time, 'GMT-4') == 9 and minute(time, 'GMT-4') == 30is_1500 = hour(time, 'GMT-4') == 15 and minute(time, 'GMT-4') == 00is_1530 = hour(time, 'GMT-4') == 15 and minute(time, 'GMT-4') == 30is_yangxian = close>openis_yinxian = close<open// 2.基本趋势标记big_trend = ema12 >= ema169 ? 1 : 0big_trend2 = ema12 <= ema169 ? 1 : 0// 背景的变色处理:bgcolor(big_trend == 1 ? color.new(color.green, 90) : color.new(color.red, 90) )// 3.网格点位初始化grid_size = atr * X_ATR // 网格大小 price_entry1 = open - grid_size*1price_entry2 = open - grid_size*2price_entry3 = open - grid_size*3price_stop_loss = open - grid_size*5price_exit1 = price_entry1 + grid_size*1price_exit2 = price_entry2 + grid_size*1price_exit3 = price_entry3 + grid_size*1qty1 = int(1000/price_entry1)qty2 = int(1000/price_entry2)qty3 = int(1000/price_entry3)// 标出各种点位slm_lines_time(time, price_entry1, price_entry2, price_entry3, price_stop_loss, price_exit1)=> time2 = time + 1000*3600*24*5 line.new(time, price_stop_loss, time2, price_stop_loss, color=color.red, xloc = xloc.bar_time, width=2) // 止损位 line.new(time, price_entry1, time2, price_entry1, color=color.green, xloc = xloc.bar_time) // line.new(time, price_entry2, time2, price_entry2, color=color.green, xloc = xloc.bar_time) // line.new(time, price_entry3, time2, price_entry3, color=color.green, xloc = xloc.bar_time) // line.new(time, price_exit1, time2, price_exit1, color=color.green, xloc = xloc.bar_time, width=2) // slm_lines(time, price_entry1, price_entry2, price_entry3, price_stop_loss, price_exit1)=> line.new(bar_index, price_stop_loss, bar_index[5], price_stop_loss, color=color.red, xloc = xloc.bar_index, width=2) // 止损位 line.new(bar_index, price_entry1, bar_index[5], price_entry1, color=color.green, xloc = xloc.bar_index) // line.new(bar_index, price_entry2, bar_index[5], price_entry2, color=color.green, xloc = xloc.bar_index) // line.new(bar_index, price_entry3, bar_index[5], price_entry3, color=color.green, xloc = xloc.bar_index) // line.new(bar_index, price_exit1, bar_index[5], price_exit1, color=color.green, xloc = xloc.bar_index, width=2) // // 4.网格点位更新和下单is_entry0 = big_trend==1 and year>=2020var is_entry = false// 未进场时:if is_entry0 and not is_entry is_entry := true grid_size := atr * X_ATR // 网格大小 price_entry1 := close - grid_size*1 price_entry2 := close - grid_size*2 price_entry3 := close - grid_size*3 price_stop_loss := close - grid_size*5 price_exit1 := price_entry1 + grid_size*1 price_exit2 := price_entry2 + grid_size*1 price_exit3 := price_entry3 + grid_size*1 qty1 := int(1000/price_entry1) qty2 := int(1000/price_entry2) qty3 := int(1000/price_entry3) // slm_lines(time, price_entry1, price_entry2, price_entry3, price_stop_loss, price_exit1) strategy.entry("open1", strategy.long, qty1, limit = price_entry1) strategy.entry("open2", strategy.long, qty2, limit = price_entry2) strategy.entry("open3", strategy.long, qty3, limit = price_entry3) strategy.exit("close1", qty = qty1, limit = price_exit1, stop = price_stop_loss) strategy.exit("close2", qty = qty2, limit = price_exit2, stop = price_stop_loss) strategy.exit("close3", qty = qty3, limit = price_exit3, stop = price_stop_loss)// 已进场的各类情况// 1.止损if is_entry and close <= price_stop_loss strategy.close_all() is_entry := false// 2.网格抬升if is_entry and close >= price_exit1 is_entry := false