Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (1)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,000,000$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. Lines are what the script plots. 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.
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.

SettingDefaultType and rangeFeeds
网格大小是多少倍ATR?1floatgrid_size

What it calculates

  • EMAclose, 169ema169 · line 9
  • EMAclose, 144ema144 · line 10
  • EMAclose, 12ema12 · line 11
  • EMAclose, 576ema576 · line 12
  • EMAclose, 676ema676 · line 13
  • EMAmtr, 30atr · 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.

closecloseprice datalowlowprice datahighhighprice dataX_ATR网格大小是多少倍ATR?setting · 1openopenprice datatimetimeprice dataema169EMA→ ema169ema144EMA→ ema144ema12EMA→ ema12ema576EMA→ ema576ema676EMA→ ema676atrEMA→ atrmtrmtrvaluebig_trend, big_trend2big_trend · big_trend22 valuesgrid_sizegrid_sizevalueprice_entry1, price_entry2, price_entry3, price_stop_lossprice_entry1 · price_en…4 valuesprice_exit1price_exit1valueprice_exit2price_exit2valueprice_exit3price_exit3valueqty1qty1valueqty2qty2valueqty3qty3valuetime2time2valueis_entry0is_entry0conditionis_0930, is_1500, is_1530is_0930 · is_1500 +13 conditionsis_yangxian, is_yinxianis_yangxian · is_yinxian2 conditionsentry:open1Open open1strategy.entryentry:open2Open open2strategy.entryentry:open3Open open3strategy.entryexit:close1exit close1strategy.exitexit:close2exit close2strategy.exitexit:close3exit close3strategy.exitclose_all:close_all strategy.close_allplot:14plotplotplot:16plotplotbgcolor:29bgcolorbgcolor

Order rules

OrderFires whenBy defaultLine
Open open1 longis_entry0 and not is_entryOn81
Open open2 longis_entry0 and not is_entryOn82
Open open3 longis_entry0 and not is_entryOn83
Exit close1 stop and limitis_entry0 and not is_entryOn85
Exit close2 stop and limitis_entry0 and not is_entryOn86
Exit close3 stop and limitis_entry0 and not is_entryOn87
Close is_entry and close <= price_stop_lossOn91

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.

Not measuredNo trades to compareThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, but neither the live nor the historical run produced any trades in them, so the replay says nothing either way.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D0.0%+102.5%00%—0.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h0.0%+5.5%00%—0.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D0.0%+9.8%00%—0.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h0.0%+8.4%00%—0.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.1%+16.7%5066%1.840.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%4963%2.250.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+0.0%+148.3%5263%1.840.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%4556%1.070.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.

ScriptSharesTypeNetTradesMax drawdown
grid strategy long this page—v5 strategy0.0%00.0%
EMA Slope AyE by fajardoarelysemav4 strategyNo trades on this market
ROoT by realisticDove62527emav5 strategy−4.7%2259.4%
sachin5986 by mohitesachin78emav4 strategy+32.1%3517.6%
sonu1997 by Sonu1997emav4 strategy+960.7%21,103.8%
Moving Average Crossover by Muh4mm4demav4 strategy+19,990.1%301,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

Verified

Nothing 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.

Download the v6 script

⋯// © zxcvbnm3260-//@version=5+//@version=6strategy("grid strategy long", overlay=true)⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 36 msWith a workaround 1,000 bars in 46 msNot measured
PineTSNot testedNot testedNot 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