Zeal Terminal Open in Zeal Terminal

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
2 3 4 5 1 2 3 4 1 2 3 4 5 6 7 1 2 1 2 1 NaN NaN 1 2 1 2 1 2 3 4 1 2 3 1 2 1 NaN 1 2 3 4 5 6 7 8 9 10 11 12 1340,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$118,058$202,527Equity from $100,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. Lines, boxes and labels are the script's own drawings, the most recent ones its limits keep. 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. The live re-run is not offered here: the script requests BINANCE:BTCUSDT M, which this page cannot load in your browser.
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

Verified

Same 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,⋯

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
》 WIDTH TYPEPRICEstring · options ['PRICE','% PP']GridWidth
》 WIDTH500float · min 0GridWidth
》 PP PERIODMonthstring · options ['Day','Week','15D','Month']res
》 BUY TYPECASHstring · options ["CONTRACTS","CASH","% EQUITY"]BuyFactor
》 QUANTITY TO BUY10000float · min 0BuyQuanTity
》 SELL TYPECONTRACTSstring · options ["CONTRACTS","CASH","% EQUITY"]SellFactor close BUY
》 QUANTITY TO SELL2float · min 0SellQuanTity

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

OrderFires whenBy defaultLine
Open BUY longfBuyCondition(_i)On123
Close BUYfSellCondition(_i)On149

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.

180/180trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+18.1%+102.5%19559%1.8520.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+3.8%+5.5%3394%3.782.2%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+14.0%+9.8%1292%144.6338.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h0.0%+8.4%00%—2.4%8 May 2026 – 21 Oct 2026
SOLUSDT 1D0.0%+16.7%00%—0.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h0.0%+21.5%00%—0.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D0.0%+148.3%00%—2.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h0.0%+17.1%00%—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 barsTradesWonP&L
Rising market1100%$1,138.10
Sideways market8754%$1,964.48
Falling market10764%$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.

0%8%
The first 150 of 195 trades
SideEntryExitBarsP&L
long3 Feb 2024 43,200.006 Feb 2024 42,708.703−$113.72
long5 Feb 2024 42,582.906 Feb 2024 42,708.701$29.55
long7 Feb 2024 43,099.008 Feb 2024 44,349.601$290.18
long6 Aug 2024 54,018.809 Sep 2024 54,869.9034$157.56
long6 Aug 2024 54,018.809 Sep 2024 54,869.9034$157.56
long12 Sep 2024 57,338.0014 Sep 2024 60,498.002$551.12
long12 Sep 2024 57,338.0014 Sep 2024 60,498.002$551.12
long17 Sep 2024 58,214.0018 Sep 2024 60,314.001$360.74
long3 Oct 2024 60,649.304 Oct 2024 60,752.701$17.06
long4 Oct 2024 60,752.705 Oct 2024 62,086.001$219.46
long10 Oct 2024 60,636.0011 Oct 2024 60,326.401−$51.06
long11 Oct 2024 60,326.4012 Oct 2024 62,540.001$366.94
long11 Oct 2024 60,326.4012 Oct 2024 62,540.001$366.94
long11 Oct 2024 60,326.4012 Oct 2024 62,540.001$366.94
long5 Nov 2024 67,850.007 Nov 2024 75,572.002$1,138.10
long4 Feb 2026 75,770.206 Feb 2026 62,909.902−$1,697.28
long4 Feb 2026 75,770.206 Feb 2026 62,909.902−$1,697.28
long4 Feb 2026 75,770.206 Feb 2026 62,909.902−$1,697.28
long5 Feb 2026 73,165.806 Feb 2026 62,909.901−$1,401.74
long5 Feb 2026 73,165.806 Feb 2026 62,909.901−$1,401.74
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long6 Feb 2026 62,909.908 Feb 2026 69,289.402$1,014.07
long9 Feb 2026 70,330.4010 Feb 2026 70,138.001−$27.35
long9 Feb 2026 70,330.4010 Feb 2026 70,138.001−$27.35
long9 Feb 2026 70,330.4010 Feb 2026 70,138.001−$27.35
long9 Feb 2026 70,330.4010 Feb 2026 70,138.001−$27.35
long9 Feb 2026 70,330.4010 Feb 2026 70,138.001−$27.35
long10 Feb 2026 70,138.0011 Feb 2026 68,841.301−$184.88
long11 Feb 2026 68,841.3012 Feb 2026 67,082.501−$255.48
long12 Feb 2026 67,082.5014 Feb 2026 68,854.002$264.07
long12 Feb 2026 67,082.5014 Feb 2026 68,854.002$264.07
long12 Feb 2026 67,082.5014 Feb 2026 68,854.002$264.07
long12 Feb 2026 67,082.5014 Feb 2026 68,854.002$264.07
long13 Feb 2026 66,272.2014 Feb 2026 68,854.001$389.57
long16 Feb 2026 68,832.6017 Feb 2026 68,892.401$8.69
long16 Feb 2026 68,832.6017 Feb 2026 68,892.401$8.69
long16 Feb 2026 68,832.6017 Feb 2026 68,892.401$8.69
long16 Feb 2026 68,832.6017 Feb 2026 68,892.401$8.69
long17 Feb 2026 68,892.4018 Feb 2026 67,503.501−$201.61
long17 Feb 2026 68,892.4018 Feb 2026 67,503.501−$201.61
long18 Feb 2026 67,503.5021 Feb 2026 68,020.003$76.51
long19 Feb 2026 66,461.0021 Feb 2026 68,020.002$234.57
long19 Feb 2026 66,461.0021 Feb 2026 68,020.002$234.57
long23 Feb 2026 67,643.4026 Feb 2026 67,988.003$50.95
long23 Feb 2026 67,643.4026 Feb 2026 67,988.003$50.95
long24 Feb 2026 64,656.0026 Feb 2026 67,988.002$515.35
long24 Feb 2026 64,656.0026 Feb 2026 67,988.002$515.35
long24 Feb 2026 64,656.0026 Feb 2026 67,988.002$515.35
long24 Feb 2026 64,656.0026 Feb 2026 67,988.002$515.35
long24 Feb 2026 64,656.0026 Feb 2026 67,988.002$515.35
long24 Feb 2026 64,656.0026 Feb 2026 67,988.002$515.35
long24 Feb 2026 64,656.0026 Feb 2026 67,988.002$515.35
long25 Feb 2026 64,058.2026 Feb 2026 67,988.001$613.49
long25 Feb 2026 64,058.2026 Feb 2026 67,988.001$613.49
long27 Feb 2026 67,485.2028 Feb 2026 65,872.101−$239.03
long27 Feb 2026 67,485.2028 Feb 2026 65,872.101−$239.03
long27 Feb 2026 67,485.2028 Feb 2026 65,872.101−$239.03
long27 Feb 2026 67,485.2028 Feb 2026 65,872.101−$239.03
long28 Feb 2026 65,872.101 Mar 2026 66,973.301$167.17
long28 Feb 2026 65,872.101 Mar 2026 66,973.301$167.17
long28 Feb 2026 65,872.101 Mar 2026 66,973.301$167.17
long28 Feb 2026 65,872.101 Mar 2026 66,973.301$167.17
long1 Mar 2026 66,973.302 Mar 2026 65,776.501−$178.69
long1 Mar 2026 66,973.302 Mar 2026 65,776.501−$178.69
long1 Mar 2026 66,973.302 Mar 2026 65,776.501−$178.69
long3 Mar 2026 68,830.105 Mar 2026 72,666.802$557.42
long3 Mar 2026 68,830.105 Mar 2026 72,666.802$557.42
long3 Mar 2026 68,830.105 Mar 2026 72,666.802$557.42
long3 Mar 2026 68,830.105 Mar 2026 72,666.802$557.42
long3 Mar 2026 68,830.105 Mar 2026 72,666.802$557.42
long4 Mar 2026 68,338.005 Mar 2026 72,666.801$633.43
long4 Mar 2026 68,338.005 Mar 2026 72,666.801$633.43
long4 Mar 2026 68,338.005 Mar 2026 72,666.801$633.43
long4 Mar 2026 68,338.005 Mar 2026 72,666.801$633.43
long4 Mar 2026 68,338.005 Mar 2026 72,666.801$633.43
long7 Mar 2026 68,114.008 Mar 2026 67,262.901−$124.95
long7 Mar 2026 68,114.008 Mar 2026 67,262.901−$124.95
long8 Mar 2026 67,262.909 Mar 2026 65,971.201−$192.04
long8 Mar 2026 67,262.909 Mar 2026 65,971.201−$192.04
long9 Mar 2026 65,971.2010 Mar 2026 68,432.201$373.04
long9 Mar 2026 65,971.2010 Mar 2026 68,432.201$373.04
long11 Mar 2026 69,948.6014 Mar 2026 70,930.003$140.30
long23 Mar 2026 67,859.0024 Mar 2026 70,906.401$449.09
long23 Mar 2026 67,859.0024 Mar 2026 70,906.401$449.09
long23 Mar 2026 67,859.0024 Mar 2026 70,906.401$449.09
long27 Mar 2026 68,820.3028 Mar 2026 66,407.301−$350.63
long28 Mar 2026 66,407.3031 Mar 2026 66,797.403$58.74
long28 Mar 2026 66,407.3031 Mar 2026 66,797.403$58.74
long28 Mar 2026 66,407.3031 Mar 2026 66,797.403$58.74
long28 Mar 2026 66,407.3031 Mar 2026 66,797.403$58.74
long28 Mar 2026 66,407.3031 Mar 2026 66,797.403$58.74
long30 Mar 2026 66,010.9031 Mar 2026 66,797.401$119.14
long1 Apr 2026 68,284.502 Apr 2026 68,113.901−$24.98
long1 Apr 2026 68,284.502 Apr 2026 68,113.901−$24.98
long1 Apr 2026 68,284.502 Apr 2026 68,113.901−$24.98
long1 Apr 2026 68,284.502 Apr 2026 68,113.901−$24.98
long3 Apr 2026 66,902.004 Apr 2026 66,964.301$9.31
long3 Apr 2026 66,902.004 Apr 2026 66,964.301$9.31
long3 Apr 2026 66,902.004 Apr 2026 66,964.301$9.31
long3 Apr 2026 66,902.004 Apr 2026 66,964.301$9.31
long3 Apr 2026 66,902.004 Apr 2026 66,964.301$9.31
long3 Apr 2026 66,902.004 Apr 2026 66,964.301$9.31
long6 Apr 2026 69,034.207 Apr 2026 68,853.701−$26.15
long7 Apr 2026 68,853.707 Apr 2026 68,853.700$0.00
long8 Apr 2026 71,924.2010 Apr 2026 71,788.002−$18.94
long8 Apr 2026 71,924.2010 Apr 2026 71,788.002−$18.94
long8 Apr 2026 71,924.2010 Apr 2026 71,788.002−$18.94
long8 Apr 2026 71,924.2010 Apr 2026 71,788.002−$18.94
long29 May 2026 73,617.5030 May 2026 73,460.801−$21.29
long29 May 2026 73,617.5030 May 2026 73,460.801−$21.29
long31 May 2026 73,884.401 Jun 2026 73,674.401−$28.42
long2 Jun 2026 71,408.9016 Jun 2026 66,328.7014−$711.42
long2 Jun 2026 71,408.9016 Jun 2026 66,328.7014−$711.42
long2 Jun 2026 71,408.9016 Jun 2026 66,328.7014−$711.42
long2 Jun 2026 71,408.9016 Jun 2026 66,328.7014−$711.42
long2 Jun 2026 71,408.9016 Jun 2026 66,328.7014−$711.42
long2 Jun 2026 71,408.9016 Jun 2026 66,328.7014−$711.42
long3 Jun 2026 66,760.8016 Jun 2026 66,328.7013−$64.72
long3 Jun 2026 66,760.8016 Jun 2026 66,328.7013−$64.72
long3 Jun 2026 66,760.8016 Jun 2026 66,328.7013−$64.72
long3 Jun 2026 66,760.8016 Jun 2026 66,328.7013−$64.72
long3 Jun 2026 66,760.8016 Jun 2026 66,328.7013−$64.72
long4 Jun 2026 64,142.8016 Jun 2026 66,328.7012$340.80
long4 Jun 2026 64,142.8016 Jun 2026 66,328.7012$340.80
long4 Jun 2026 64,142.8016 Jun 2026 66,328.7012$216.46
long4 Jun 2026 64,142.8016 Jun 2026 66,328.7012$124.34
long4 Jun 2026 64,142.8016 Jun 2026 66,328.7012$340.80
long5 Jun 2026 63,886.0016 Jun 2026 66,328.7011$382.36
long5 Jun 2026 63,886.0016 Jun 2026 66,328.7011$382.36
long5 Jun 2026 63,886.0016 Jun 2026 66,328.7011$382.36
long5 Jun 2026 63,886.0016 Jun 2026 66,328.7011$382.36
long5 Jun 2026 63,886.0016 Jun 2026 66,328.7011$382.36
long5 Jun 2026 63,886.0016 Jun 2026 66,328.7011$382.36
long17 Jun 2026 65,675.0018 Jun 2026 64,509.401−$177.48
long17 Jun 2026 65,675.0018 Jun 2026 64,509.401−$177.48
long17 Jun 2026 65,675.0018 Jun 2026 64,509.401−$177.48
long18 Jun 2026 64,509.4022 Jun 2026 63,312.004−$185.62
long18 Jun 2026 64,509.4022 Jun 2026 63,312.004−$185.62
long18 Jun 2026 64,509.4022 Jun 2026 63,312.004−$185.62
long19 Jun 2026 62,958.0022 Jun 2026 63,312.003$56.23
long19 Jun 2026 62,958.0022 Jun 2026 63,312.003$56.23
long19 Jun 2026 62,958.0022 Jun 2026 63,312.003$56.23

Similar scripts

No other indicator or strategy here shares enough of its calculations.

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 80 msWith a workaround 1,000 bars in 8,624 msNot measured
PineTSNo 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💤