Pine Script v4 · Strategy
CoGrid Management
A Pine Script v4 strategy with no technical-analysis calculation. 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 is 0 contracts and pays no commission. 7 settings; defaults include 》 WIDTH TYPE PRICE, 》 WIDTH 500 and 》 PP PERIOD Month.
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 CoGrid Management repaint?
- Yes
- At least one pattern in the source can repaint: higher-timeframe data.
- How did CoGrid Management do on BTCUSDT 1D with default settings?
- +18.06%
- From 26 Jan 2024 to 21 Oct 2026, over 195 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can CoGrid Management be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does CoGrid Management use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does CoGrid Management run in Zeal Terminal?
- Yes
- It compiles in 80 ms and runs 1,000 bars in 8,624 ms, in the browser.
BTCUSDT · 1D · default settings
- PP
- Average Price
- Buy/Sell 1
- Buy/Sell 2
- Buy/Sell 3
- Buy/Sell 4
- Buy/Sell 5
- Buy/Sell 6
- Buy/Sell 7
- Buy/Sell 8
- Buy/Sell 9
- Buy/Sell 10
- Buy/Sell 11
- Buy/Sell 12
- Buy/Sell 13
- Buy/Sell 14
- Net profit
- +18.06%
- $18,060.20
- Buy and hold
- +102.5%
- same period
- Closed trades
- 195
- 116 won · 78 lost
- Profitable
- 59.5%
- of closed trades
- Profit factor
- 1.85
- gross profit ÷ gross loss
- Max drawdown
- 19.96%
- −$23,839.00
One run with the author's default settings, $100,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.
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 floor → math.floor, ceil → math.ceil, security → request.security, crossunder → ta.crossunder, crossover → ta.crossover, tostring → str.tostring.
v4 → v5
VerifiedSame 195 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 7: input(type = input.float) → input.float()
- Line 10: input(type = input.float) → input.float()
- Line 12: input(type = input.float) → input.float()
- Line 44: floor → math.floor
- Line 45: floor → math.floor
7 more edits
- Line 45: floor → math.floor
- Line 49: ceil → math.ceil
- Line 55: security → request.security
- Line 81: crossunder → ta.crossunder
- Line 94: crossover → ta.crossover
- Line 115: tostring → str.tostring
- Line 141: tostring → str.tostring
⋯// © XaviZ-//@version=4+//@version=5strategy(title = "CoGrid Management", shorttitle = "CoGrid💹", overlay = true, pyramiding = 1000, default_qty_value = 0)⋯WOption = input('PRICE', " 》 WIDTH TYPE", options = ['PRICE','% PP'])-Width = input(500, " 》 WIDTH", type = input.float, minval = 0)+Width = input.float(500, " 》 WIDTH", minval = 0)ppPeriod = input('Month', " 》 PP PERIOD", options = ['Day','Week','15D','Month'])BuyType = input("CASH", " 》 BUY TYPE", options = ["CONTRACTS","CASH","% EQUITY"])-BuyQ = input(10000, " 》 QUANTITY TO BUY", type = input.float, minval = 0)+BuyQ = input.float(10000, " 》 QUANTITY TO BUY", minval = 0)SellType = input('CONTRACTS', " 》 SELL TYPE", options = ["CONTRACTS","CASH","% EQUITY"])-SellQ = input(2, " 》 QUANTITY TO SELL", type = input.float, minval = 0)+SellQ = input.float(2, " 》 QUANTITY TO SELL", minval = 0)// ———————————————————— Vars⋯var float PPdownOrigin = na-PPdownOrigin := floor(PP / GridWidth) * GridWidth+PPdownOrigin := math.floor(PP / GridWidth) * GridWidth-PPdownOrigin := (fixnan(FinalSellPrice[1]) <= PP ? (floor(fixnan(FinalSellPrice[1]) / GridWidth) * GridWidth) - GridWidth : floor(PP / GridWidth) * GridWidth)+PPdownOrigin := (fixnan(FinalSellPrice[1]) <= PP ? (math.floor(fixnan(FinalSellPrice[1]) / GridWidth) * GridWidth) - GridWidth : math.floor(PP / GridWidth) * GridWidth)// ————— Origin from Rounded Position Price⋯PPupOrigin := nz(PPupOrigin[1])-PPupOrigin := (ceil(fixnan(AveragePrice[1]) / GridWidth) * GridWidth) + GridWidth+PPupOrigin := (math.ceil(fixnan(AveragePrice[1]) / GridWidth) * GridWidth) + GridWidth// ———————————————————— Pivot Points⋯// "Function to securely and simply call `security()` so that it never repaints and never looks ahead" (@PineCoders)-f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)+f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)phigh = f_secureSecurity(syminfo.tickerid, res, high)⋯ _ldown := fGrid(PPdownOrigin, -GridWidth, _i)- _pb := crossunder(low, _ldown) and high >= _ldown+ _pb := ta.crossunder(low, _ldown) and high >= _ldown _BuyCondition := na(FinalOpenPrice) ? _pb : _pb and _ldown < (FinalOpenPrice[1] - GridWidth / 4)⋯ _lup := fGrid(PPupOrigin, GridWidth, _i)- _ps := crossover(high, _lup) and low <= _lup+ _ps := ta.crossover(high, _lup) and low <= _lup _SellCondition := na(FinalOpenPrice) ? _ps : _ps and _lup > (FinalOpenPrice[1] + GridWidth / 4)⋯ y = FinalOpenPrice, - text = ' ' + tostring(nBuys), + text = ' ' + str.tostring(nBuys), textcolor = color.lime,⋯ y = FinalOpenPrice, - text = ' ' + tostring(nSells), + text = ' ' + str.tostring(nSells), textcolor = color.orange,⋯Same 195 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.string()
- Line 8: input() → input.string()
- Line 9: input() → input.string()
- Line 11: input() → input.string()
⋯// ———————————————————— Inputs-WOption = input('PRICE', " 》 WIDTH TYPE", options = ['PRICE','% PP'])+WOption = input.string('PRICE', " 》 WIDTH TYPE", options = ['PRICE','% PP'])Width = input.float(500, " 》 WIDTH", minval = 0)-ppPeriod = input('Month', " 》 PP PERIOD", options = ['Day','Week','15D','Month'])+ppPeriod = input.string('Month', " 》 PP PERIOD", options = ['Day','Week','15D','Month'])-BuyType = input("CASH", " 》 BUY TYPE", options = ["CONTRACTS","CASH","% EQUITY"])+BuyType = input.string("CASH", " 》 BUY TYPE", options = ["CONTRACTS","CASH","% EQUITY"])BuyQ = input.float(10000, " 》 QUANTITY TO BUY", minval = 0)-SellType = input('CONTRACTS', " 》 SELL TYPE", options = ["CONTRACTS","CASH","% EQUITY"])+SellType = input.string('CONTRACTS', " 》 SELL TYPE", options = ["CONTRACTS","CASH","% EQUITY"])SellQ = input.float(2, " 》 QUANTITY TO SELL", minval = 0)⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L75:C4: a 'bool' cannot be 'na' in Pine Script v6 — declare it as true or false. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © XaviZ-//@version=5+//@version=6strategy(title = "CoGrid Management", shorttitle = "CoGrid💹", overlay = true, pyramiding = 1000, default_qty_value = 0)⋯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 |
|---|---|---|---|
| 》 WIDTH TYPE | PRICE | string · options ['PRICE','% PP'] | GridWidth |
| 》 WIDTH | 500 | float · min 0 | GridWidth |
| 》 PP PERIOD | Month | string · options ['Day','Week','15D','Month'] | res |
| 》 BUY TYPE | CASH | string · options ["CONTRACTS","CASH","% EQUITY"] | BuyFactor |
| 》 QUANTITY TO BUY | 10000 | float · min 0 | BuyQuanTity |
| 》 SELL TYPE | CONTRACTS | string · options ["CONTRACTS","CASH","% EQUITY"] | SellFactor close BUY |
| 》 QUANTITY TO SELL | 2 | float · min 0 | SellQuanTity |
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.
The script has 76 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open BUY long | fBuyCondition(_i) | On | 123 |
| Close BUY | fSellCondition(_i) | On | 149 |
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.
- CheckHigher-timeframe data1 security call(s) use lookahead_on (line 55). Without a [1] offset this reads future bars in history.
- 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 | +18.1% | +102.5% | 195 | 59% | 1.85 | 20.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +3.8% | +5.5% | 33 | 94% | 3.78 | 2.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +14.0% | +9.8% | 12 | 92% | 144.63 | 38.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 2.4% | 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% | — | 2.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Higher-timeframe data for request.security(): BINANCE:BTCUSDT M (Binance BTCUSDT 1M), fetched from Binance's public market data for each market.
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 1 | 100% | $1,138.10 |
| Sideways market | 87 | 54% | $1,964.48 |
| Falling market | 107 | 64% | $14,957.60 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
4.9%
The 95th percentile of the worst drawdown when the same 195 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 2.0% to 4.9%; the tick is what actually happened, 7.4%. Order luck alone moves it this much.
The first 150 of 195 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 3 Feb 2024 43,200.00 | 6 Feb 2024 42,708.70 | 3 | −$113.72 |
| long | 5 Feb 2024 42,582.90 | 6 Feb 2024 42,708.70 | 1 | $29.55 |
| long | 7 Feb 2024 43,099.00 | 8 Feb 2024 44,349.60 | 1 | $290.18 |
| long | 6 Aug 2024 54,018.80 | 9 Sep 2024 54,869.90 | 34 | $157.56 |
| long | 6 Aug 2024 54,018.80 | 9 Sep 2024 54,869.90 | 34 | $157.56 |
| long | 12 Sep 2024 57,338.00 | 14 Sep 2024 60,498.00 | 2 | $551.12 |
| long | 12 Sep 2024 57,338.00 | 14 Sep 2024 60,498.00 | 2 | $551.12 |
| long | 17 Sep 2024 58,214.00 | 18 Sep 2024 60,314.00 | 1 | $360.74 |
| long | 3 Oct 2024 60,649.30 | 4 Oct 2024 60,752.70 | 1 | $17.06 |
| long | 4 Oct 2024 60,752.70 | 5 Oct 2024 62,086.00 | 1 | $219.46 |
| long | 10 Oct 2024 60,636.00 | 11 Oct 2024 60,326.40 | 1 | −$51.06 |
| long | 11 Oct 2024 60,326.40 | 12 Oct 2024 62,540.00 | 1 | $366.94 |
| long | 11 Oct 2024 60,326.40 | 12 Oct 2024 62,540.00 | 1 | $366.94 |
| long | 11 Oct 2024 60,326.40 | 12 Oct 2024 62,540.00 | 1 | $366.94 |
| long | 5 Nov 2024 67,850.00 | 7 Nov 2024 75,572.00 | 2 | $1,138.10 |
| long | 4 Feb 2026 75,770.20 | 6 Feb 2026 62,909.90 | 2 | −$1,697.28 |
| long | 4 Feb 2026 75,770.20 | 6 Feb 2026 62,909.90 | 2 | −$1,697.28 |
| long | 4 Feb 2026 75,770.20 | 6 Feb 2026 62,909.90 | 2 | −$1,697.28 |
| long | 5 Feb 2026 73,165.80 | 6 Feb 2026 62,909.90 | 1 | −$1,401.74 |
| long | 5 Feb 2026 73,165.80 | 6 Feb 2026 62,909.90 | 1 | −$1,401.74 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 6 Feb 2026 62,909.90 | 8 Feb 2026 69,289.40 | 2 | $1,014.07 |
| long | 9 Feb 2026 70,330.40 | 10 Feb 2026 70,138.00 | 1 | −$27.35 |
| long | 9 Feb 2026 70,330.40 | 10 Feb 2026 70,138.00 | 1 | −$27.35 |
| long | 9 Feb 2026 70,330.40 | 10 Feb 2026 70,138.00 | 1 | −$27.35 |
| long | 9 Feb 2026 70,330.40 | 10 Feb 2026 70,138.00 | 1 | −$27.35 |
| long | 9 Feb 2026 70,330.40 | 10 Feb 2026 70,138.00 | 1 | −$27.35 |
| long | 10 Feb 2026 70,138.00 | 11 Feb 2026 68,841.30 | 1 | −$184.88 |
| long | 11 Feb 2026 68,841.30 | 12 Feb 2026 67,082.50 | 1 | −$255.48 |
| long | 12 Feb 2026 67,082.50 | 14 Feb 2026 68,854.00 | 2 | $264.07 |
| long | 12 Feb 2026 67,082.50 | 14 Feb 2026 68,854.00 | 2 | $264.07 |
| long | 12 Feb 2026 67,082.50 | 14 Feb 2026 68,854.00 | 2 | $264.07 |
| long | 12 Feb 2026 67,082.50 | 14 Feb 2026 68,854.00 | 2 | $264.07 |
| long | 13 Feb 2026 66,272.20 | 14 Feb 2026 68,854.00 | 1 | $389.57 |
| long | 16 Feb 2026 68,832.60 | 17 Feb 2026 68,892.40 | 1 | $8.69 |
| long | 16 Feb 2026 68,832.60 | 17 Feb 2026 68,892.40 | 1 | $8.69 |
| long | 16 Feb 2026 68,832.60 | 17 Feb 2026 68,892.40 | 1 | $8.69 |
| long | 16 Feb 2026 68,832.60 | 17 Feb 2026 68,892.40 | 1 | $8.69 |
| long | 17 Feb 2026 68,892.40 | 18 Feb 2026 67,503.50 | 1 | −$201.61 |
| long | 17 Feb 2026 68,892.40 | 18 Feb 2026 67,503.50 | 1 | −$201.61 |
| long | 18 Feb 2026 67,503.50 | 21 Feb 2026 68,020.00 | 3 | $76.51 |
| long | 19 Feb 2026 66,461.00 | 21 Feb 2026 68,020.00 | 2 | $234.57 |
| long | 19 Feb 2026 66,461.00 | 21 Feb 2026 68,020.00 | 2 | $234.57 |
| long | 23 Feb 2026 67,643.40 | 26 Feb 2026 67,988.00 | 3 | $50.95 |
| long | 23 Feb 2026 67,643.40 | 26 Feb 2026 67,988.00 | 3 | $50.95 |
| long | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | $515.35 |
| long | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | $515.35 |
| long | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | $515.35 |
| long | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | $515.35 |
| long | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | $515.35 |
| long | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | $515.35 |
| long | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | $515.35 |
| long | 25 Feb 2026 64,058.20 | 26 Feb 2026 67,988.00 | 1 | $613.49 |
| long | 25 Feb 2026 64,058.20 | 26 Feb 2026 67,988.00 | 1 | $613.49 |
| long | 27 Feb 2026 67,485.20 | 28 Feb 2026 65,872.10 | 1 | −$239.03 |
| long | 27 Feb 2026 67,485.20 | 28 Feb 2026 65,872.10 | 1 | −$239.03 |
| long | 27 Feb 2026 67,485.20 | 28 Feb 2026 65,872.10 | 1 | −$239.03 |
| long | 27 Feb 2026 67,485.20 | 28 Feb 2026 65,872.10 | 1 | −$239.03 |
| long | 28 Feb 2026 65,872.10 | 1 Mar 2026 66,973.30 | 1 | $167.17 |
| long | 28 Feb 2026 65,872.10 | 1 Mar 2026 66,973.30 | 1 | $167.17 |
| long | 28 Feb 2026 65,872.10 | 1 Mar 2026 66,973.30 | 1 | $167.17 |
| long | 28 Feb 2026 65,872.10 | 1 Mar 2026 66,973.30 | 1 | $167.17 |
| long | 1 Mar 2026 66,973.30 | 2 Mar 2026 65,776.50 | 1 | −$178.69 |
| long | 1 Mar 2026 66,973.30 | 2 Mar 2026 65,776.50 | 1 | −$178.69 |
| long | 1 Mar 2026 66,973.30 | 2 Mar 2026 65,776.50 | 1 | −$178.69 |
| long | 3 Mar 2026 68,830.10 | 5 Mar 2026 72,666.80 | 2 | $557.42 |
| long | 3 Mar 2026 68,830.10 | 5 Mar 2026 72,666.80 | 2 | $557.42 |
| long | 3 Mar 2026 68,830.10 | 5 Mar 2026 72,666.80 | 2 | $557.42 |
| long | 3 Mar 2026 68,830.10 | 5 Mar 2026 72,666.80 | 2 | $557.42 |
| long | 3 Mar 2026 68,830.10 | 5 Mar 2026 72,666.80 | 2 | $557.42 |
| long | 4 Mar 2026 68,338.00 | 5 Mar 2026 72,666.80 | 1 | $633.43 |
| long | 4 Mar 2026 68,338.00 | 5 Mar 2026 72,666.80 | 1 | $633.43 |
| long | 4 Mar 2026 68,338.00 | 5 Mar 2026 72,666.80 | 1 | $633.43 |
| long | 4 Mar 2026 68,338.00 | 5 Mar 2026 72,666.80 | 1 | $633.43 |
| long | 4 Mar 2026 68,338.00 | 5 Mar 2026 72,666.80 | 1 | $633.43 |
| long | 7 Mar 2026 68,114.00 | 8 Mar 2026 67,262.90 | 1 | −$124.95 |
| long | 7 Mar 2026 68,114.00 | 8 Mar 2026 67,262.90 | 1 | −$124.95 |
| long | 8 Mar 2026 67,262.90 | 9 Mar 2026 65,971.20 | 1 | −$192.04 |
| long | 8 Mar 2026 67,262.90 | 9 Mar 2026 65,971.20 | 1 | −$192.04 |
| long | 9 Mar 2026 65,971.20 | 10 Mar 2026 68,432.20 | 1 | $373.04 |
| long | 9 Mar 2026 65,971.20 | 10 Mar 2026 68,432.20 | 1 | $373.04 |
| long | 11 Mar 2026 69,948.60 | 14 Mar 2026 70,930.00 | 3 | $140.30 |
| long | 23 Mar 2026 67,859.00 | 24 Mar 2026 70,906.40 | 1 | $449.09 |
| long | 23 Mar 2026 67,859.00 | 24 Mar 2026 70,906.40 | 1 | $449.09 |
| long | 23 Mar 2026 67,859.00 | 24 Mar 2026 70,906.40 | 1 | $449.09 |
| long | 27 Mar 2026 68,820.30 | 28 Mar 2026 66,407.30 | 1 | −$350.63 |
| long | 28 Mar 2026 66,407.30 | 31 Mar 2026 66,797.40 | 3 | $58.74 |
| long | 28 Mar 2026 66,407.30 | 31 Mar 2026 66,797.40 | 3 | $58.74 |
| long | 28 Mar 2026 66,407.30 | 31 Mar 2026 66,797.40 | 3 | $58.74 |
| long | 28 Mar 2026 66,407.30 | 31 Mar 2026 66,797.40 | 3 | $58.74 |
| long | 28 Mar 2026 66,407.30 | 31 Mar 2026 66,797.40 | 3 | $58.74 |
| long | 30 Mar 2026 66,010.90 | 31 Mar 2026 66,797.40 | 1 | $119.14 |
| long | 1 Apr 2026 68,284.50 | 2 Apr 2026 68,113.90 | 1 | −$24.98 |
| long | 1 Apr 2026 68,284.50 | 2 Apr 2026 68,113.90 | 1 | −$24.98 |
| long | 1 Apr 2026 68,284.50 | 2 Apr 2026 68,113.90 | 1 | −$24.98 |
| long | 1 Apr 2026 68,284.50 | 2 Apr 2026 68,113.90 | 1 | −$24.98 |
| long | 3 Apr 2026 66,902.00 | 4 Apr 2026 66,964.30 | 1 | $9.31 |
| long | 3 Apr 2026 66,902.00 | 4 Apr 2026 66,964.30 | 1 | $9.31 |
| long | 3 Apr 2026 66,902.00 | 4 Apr 2026 66,964.30 | 1 | $9.31 |
| long | 3 Apr 2026 66,902.00 | 4 Apr 2026 66,964.30 | 1 | $9.31 |
| long | 3 Apr 2026 66,902.00 | 4 Apr 2026 66,964.30 | 1 | $9.31 |
| long | 3 Apr 2026 66,902.00 | 4 Apr 2026 66,964.30 | 1 | $9.31 |
| long | 6 Apr 2026 69,034.20 | 7 Apr 2026 68,853.70 | 1 | −$26.15 |
| long | 7 Apr 2026 68,853.70 | 7 Apr 2026 68,853.70 | 0 | $0.00 |
| long | 8 Apr 2026 71,924.20 | 10 Apr 2026 71,788.00 | 2 | −$18.94 |
| long | 8 Apr 2026 71,924.20 | 10 Apr 2026 71,788.00 | 2 | −$18.94 |
| long | 8 Apr 2026 71,924.20 | 10 Apr 2026 71,788.00 | 2 | −$18.94 |
| long | 8 Apr 2026 71,924.20 | 10 Apr 2026 71,788.00 | 2 | −$18.94 |
| long | 29 May 2026 73,617.50 | 30 May 2026 73,460.80 | 1 | −$21.29 |
| long | 29 May 2026 73,617.50 | 30 May 2026 73,460.80 | 1 | −$21.29 |
| long | 31 May 2026 73,884.40 | 1 Jun 2026 73,674.40 | 1 | −$28.42 |
| long | 2 Jun 2026 71,408.90 | 16 Jun 2026 66,328.70 | 14 | −$711.42 |
| long | 2 Jun 2026 71,408.90 | 16 Jun 2026 66,328.70 | 14 | −$711.42 |
| long | 2 Jun 2026 71,408.90 | 16 Jun 2026 66,328.70 | 14 | −$711.42 |
| long | 2 Jun 2026 71,408.90 | 16 Jun 2026 66,328.70 | 14 | −$711.42 |
| long | 2 Jun 2026 71,408.90 | 16 Jun 2026 66,328.70 | 14 | −$711.42 |
| long | 2 Jun 2026 71,408.90 | 16 Jun 2026 66,328.70 | 14 | −$711.42 |
| long | 3 Jun 2026 66,760.80 | 16 Jun 2026 66,328.70 | 13 | −$64.72 |
| long | 3 Jun 2026 66,760.80 | 16 Jun 2026 66,328.70 | 13 | −$64.72 |
| long | 3 Jun 2026 66,760.80 | 16 Jun 2026 66,328.70 | 13 | −$64.72 |
| long | 3 Jun 2026 66,760.80 | 16 Jun 2026 66,328.70 | 13 | −$64.72 |
| long | 3 Jun 2026 66,760.80 | 16 Jun 2026 66,328.70 | 13 | −$64.72 |
| long | 4 Jun 2026 64,142.80 | 16 Jun 2026 66,328.70 | 12 | $340.80 |
| long | 4 Jun 2026 64,142.80 | 16 Jun 2026 66,328.70 | 12 | $340.80 |
| long | 4 Jun 2026 64,142.80 | 16 Jun 2026 66,328.70 | 12 | $216.46 |
| long | 4 Jun 2026 64,142.80 | 16 Jun 2026 66,328.70 | 12 | $124.34 |
| long | 4 Jun 2026 64,142.80 | 16 Jun 2026 66,328.70 | 12 | $340.80 |
| long | 5 Jun 2026 63,886.00 | 16 Jun 2026 66,328.70 | 11 | $382.36 |
| long | 5 Jun 2026 63,886.00 | 16 Jun 2026 66,328.70 | 11 | $382.36 |
| long | 5 Jun 2026 63,886.00 | 16 Jun 2026 66,328.70 | 11 | $382.36 |
| long | 5 Jun 2026 63,886.00 | 16 Jun 2026 66,328.70 | 11 | $382.36 |
| long | 5 Jun 2026 63,886.00 | 16 Jun 2026 66,328.70 | 11 | $382.36 |
| long | 5 Jun 2026 63,886.00 | 16 Jun 2026 66,328.70 | 11 | $382.36 |
| long | 17 Jun 2026 65,675.00 | 18 Jun 2026 64,509.40 | 1 | −$177.48 |
| long | 17 Jun 2026 65,675.00 | 18 Jun 2026 64,509.40 | 1 | −$177.48 |
| long | 17 Jun 2026 65,675.00 | 18 Jun 2026 64,509.40 | 1 | −$177.48 |
| long | 18 Jun 2026 64,509.40 | 22 Jun 2026 63,312.00 | 4 | −$185.62 |
| long | 18 Jun 2026 64,509.40 | 22 Jun 2026 63,312.00 | 4 | −$185.62 |
| long | 18 Jun 2026 64,509.40 | 22 Jun 2026 63,312.00 | 4 | −$185.62 |
| long | 19 Jun 2026 62,958.00 | 22 Jun 2026 63,312.00 | 3 | $56.23 |
| long | 19 Jun 2026 62,958.00 | 22 Jun 2026 63,312.00 | 3 | $56.23 |
| long | 19 Jun 2026 62,958.00 | 22 Jun 2026 63,312.00 | 3 | $56.23 |
Similar scripts
No other indicator or strategy here shares enough of its calculations.
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 80 ms | With a workaround 1,000 bars in 8,624 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. 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/// © XaviZ//@version=4strategy(title = "CoGrid Management", shorttitle = "CoGrid💹", overlay = true, pyramiding = 1000, default_qty_value = 0)// ———————————————————— InputsWOption = input('PRICE', " 》 WIDTH TYPE", options = ['PRICE','% PP'])Width = input(500, " 》 WIDTH", type = input.float, minval = 0)ppPeriod = input('Month', " 》 PP PERIOD", options = ['Day','Week','15D','Month'])BuyType = input("CASH", " 》 BUY TYPE", options = ["CONTRACTS","CASH","% EQUITY"])BuyQ = input(10000, " 》 QUANTITY TO BUY", type = input.float, minval = 0)SellType = input('CONTRACTS', " 》 SELL TYPE", options = ["CONTRACTS","CASH","% EQUITY"])SellQ = input(2, " 》 QUANTITY TO SELL", type = input.float, minval = 0)// ———————————————————— Vars// ————— Final Buy Price & Final Sell Pricevar float FinalBuyPrice = navar float FinalSellPrice = navar float FinalOpenPrice = na// ————— Average Pricevar int nBuys = nanBuys := nz(nBuys[1])var int nSells = nanSells := nz(nSells[1])var float sumBuy = nasumBuy := nz(sumBuy[1])var float sumSell = nasumSell := nz(sumSell[1])var float sumQtyBuy = nasumQtyBuy := nz(sumQtyBuy[1])var float sumQtySell = nasumQtySell := nz(sumQtySell[1])var float AveragePrice = nacolor Color = na// ————— Fibonacci Pivots Level Calculationvar float PP = na// ————— BacktestBuyFactor = BuyType == "CONTRACTS" ? 1 : BuyType == "% EQUITY" ? (100 / (strategy.equity / close)) : closeSellFactor = SellType == "CASH" ? close : 1BuyQuanTity = BuyQ / BuyFactorSellQuanTity = SellQ / SellFactor// ————— Width between levelsfloat GridWidth = WOption == 'PRICE' ? Width : PP * (Width/100)// ————— Origin from Rounded Pivot Points or last Sellvar float PPdownOrigin = naPPdownOrigin := floor(PP / GridWidth) * GridWidthPPdownOrigin := (fixnan(FinalSellPrice[1]) <= PP ? (floor(fixnan(FinalSellPrice[1]) / GridWidth) * GridWidth) - GridWidth : floor(PP / GridWidth) * GridWidth)// ————— Origin from Rounded Position Pricevar float PPupOrigin = naPPupOrigin := nz(PPupOrigin[1])PPupOrigin := (ceil(fixnan(AveragePrice[1]) / GridWidth) * GridWidth) + GridWidth// ———————————————————— Pivot Points// ————— Pivot Points Periodres = ppPeriod == '15D' ? '15D' : ppPeriod == 'Week' ? 'W' : ppPeriod == 'Day' ? 'D' : 'M'// ————— High, Low, Close Calc. // "Function to securely and simply call `security()` so that it never repaints and never looks ahead" (@PineCoders)f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)phigh = f_secureSecurity(syminfo.tickerid, res, high)plow = f_secureSecurity(syminfo.tickerid, res, low)pclose = f_secureSecurity(syminfo.tickerid, res, close)// ————— Fibonacci Pivots Level CalculationPP := (phigh + plow + pclose) / 3// ———————————————————— Grid Strategy// ————— Grid CalculationfGrid(_1, _2, _n) => _a = _1, _b = _2, _c = 0.0 for _i = 1 to _n if _i == 1 _c := _a else _c := _a + _b _a := _c// ————— Buy at better PricefBuyCondition(_n) => var float _ldown = na _ldown := nz(_ldown[1]) var bool _pb = na _pb := nz(_pb[1]) var bool _BuyCondition = na _BuyCondition := nz(_BuyCondition[1]) for _i = 1 to _n _ldown := fGrid(PPdownOrigin, -GridWidth, _i) _pb := crossunder(low, _ldown) and high >= _ldown _BuyCondition := na(FinalOpenPrice) ? _pb : _pb and _ldown < (FinalOpenPrice[1] - GridWidth / 4) _BuyCondition// ————— Sell at better PricefSellCondition(_n) => var float _lup = na _lup := nz(_lup[1]) var bool _ps = na _ps := nz(_ps[1]) var bool _SellCondition = na _SellCondition := nz(_SellCondition[1]) for _i = 1 to _n _lup := fGrid(PPupOrigin, GridWidth, _i) _ps := crossover(high, _lup) and low <= _lup _SellCondition := na(FinalOpenPrice) ? _ps : _ps and _lup > (FinalOpenPrice[1] + GridWidth / 4) _SellCondition// ————— Final Trade Price, Average Price & Backtestfor _i = 1 to 15 // Buys if fBuyCondition(_i) FinalBuyPrice := fGrid(PPdownOrigin, -GridWidth, _i) FinalSellPrice := na FinalOpenPrice := fGrid(PPdownOrigin, -GridWidth, _i) nBuys := nBuys + 1 nSells := na sumBuy := FinalOpenPrice * BuyQuanTity + nz(sumBuy[1]) sumQtyBuy := BuyQuanTity + nz(sumQtyBuy[1]) AveragePrice := sumBuy / sumQtyBuy Color := color.lime // Numbering label.new(x = time, y = FinalOpenPrice, text = ' ' + tostring(nBuys), textcolor = color.lime, style = label.style_none, xloc = xloc.bar_time, yloc = yloc.price, size = size.small) // Backtest Buys strategy.entry("BUY", strategy.long, qty = BuyQuanTity) else // Sells if fSellCondition(_i) FinalBuyPrice := na FinalSellPrice := fGrid(PPupOrigin, GridWidth, _i) FinalOpenPrice := fGrid(PPupOrigin, GridWidth, _i) nBuys := na nSells := nSells + 1 sumBuy := na sumQtyBuy := na Color := color.orange // Numbering label.new(x = time, y = FinalOpenPrice, text = ' ' + tostring(nSells), textcolor = color.orange, style = label.style_none, xloc = xloc.bar_time, yloc = yloc.price, size = size.small) // Backtest Sells strategy.close("BUY", qty = SellType != "% EQUITY" ? SellQuanTity : na, qty_percent = (SellType == "% EQUITY" ? SellQuanTity : na), comment = "SELL")// ———————————————————— Plotting// ————— Plotting Pivot Pointsplot(PP, title = "PP", style = plot.style_circles, color = color.aqua, linewidth = 2)// ————— Plotting the average priceplot(nBuys > 1 ? AveragePrice[1] : na, title = "Average Price", style = plot.style_circles, color = color.fuchsia, linewidth = 2)// ————— Buy ShapesfPlotBuySell(_n) => if fBuyCondition(_n) fGrid(PPdownOrigin, -GridWidth, _n) else if fSellCondition(_n) fGrid(PPupOrigin, GridWidth, _n) plotshape(fPlotBuySell(1), title = "Buy/Sell 1", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(2), title = "Buy/Sell 2", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(3), title = "Buy/Sell 3", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(4), title = "Buy/Sell 4", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(5), title = "Buy/Sell 5", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(6), title = "Buy/Sell 6", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(7), title = "Buy/Sell 7", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(8), title = "Buy/Sell 8", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(9), title = "Buy/Sell 9", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(10), title = "Buy/Sell 10", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(11), title = "Buy/Sell 11", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(12), title = "Buy/Sell 12", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(13), title = "Buy/Sell 13", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(14), title = "Buy/Sell 14", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)plotshape(fPlotBuySell(15), title = "Buy/Sell 15", style = shape.diamond, location = location.absolute, color = Color, size = size.tiny)// ————— Plotting Lines under PPfBuySellLevel(_n) => if low < fGrid(PPdownOrigin, -GridWidth, _n) and PP > fGrid(PPdownOrigin, -GridWidth, _n) fGrid(PPdownOrigin, -GridWidth, _n) else if high > fGrid(PPupOrigin, GridWidth, _n) and PP < fGrid(PPupOrigin, GridWidth, _n) fGrid(PPupOrigin, GridWidth, _n) plot(fBuySellLevel(1), title = "Level down/up 1", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(2), title = "Level down/up 2", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(3), title = "Level down/up 3", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(4), title = "Level down/up 4", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(5), title = "Level down/up 5", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(6), title = "Level down/up 6", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(7), title = "Level down/up 7", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(8), title = "Level down/up 8", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(9), title = "Level down/up 9", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(10), title = "Level down/up 10", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(11), title = "Level down/up 11", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(12), title = "Level down/up 12", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(13), title = "Level down/up 13", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(14), title = "Level down/up 14", style = plot.style_circles, color = high > PP ? color.green : color.red)plot(fBuySellLevel(15), title = "Level down/up 15", style = plot.style_circles, color = high > PP ? color.green : color.red)// by XaviZ💤