Strategy · Pine Script v5 · On the chart

Grid Bot Simulator [Alorse]

by Alorse· code credited to mvs1231 || xxattaxx

This strategy divides a fixed price range between an upper and a lower limit into equally spaced grid levels and plots them on the price chart.

View on GitHub

Repaints

GitHub stars330on the repository
Pine versionv5runs as is
Lines of code125
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update2 months ago25 Aug 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

125 lines of Pine Script v5 from strategies/grid/GridBotDir [Alorse].pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

GridBotDir [Alorse].pine
//@version=5
//------------------------------------------------------------------------------------------------<>
//      This source code is subject to the terms of the Mozilla Public License 2.0 
//      https://mozilla.org/MPL/2.0/ 
//      ©mvs1231 || xxattaxx
//      Updated 06/05/2021 to fix Transp deprecation
// indicator('Grid Bot Simulator [Alorse]', shorttitle='GridBotDir [Alorse]', overlay=true)
strategy("Grid Bot Simulator [Alorse]", shorttitle='GridBotDir [Alorse]', overlay=true, pyramiding=10, currency=currency.USD, default_qty_type=strategy.percent_of_equity, initial_capital=100000, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.06)
//-----<Inputs>-----------------------------------------------------------------------------------<> 
UpperLimit = input.float(48000.0, 'Upper Limit')
LowerLimit = input.float(45000.0, 'Lower Limit')
No_Grids = input.int(10, 'Number of Grids(10)') + 1
Dir = input.string('neutral', 'Market Direction', options=['up', 'neutral', 'down'])
ShowGrids = input(true, 'Show Grids')
HighLight = input(true, 'Highlight Trade Zones')
ShowCurrent = input(false, 'Show Only Current Grids')
Extremes = input(true, 'High/Low for signals')
//-----<Variables>--------------------------------------------------------------------------------<>
LastSignal = 0
LastSignal_Index = 0
LastSignal_Time = time
SignalLine = 0.0
CurrentGrid = 0.0
Buy = false
Sell = false
Direction = Dir == 'up' ? 1 : Dir == 'down' ? -1 : 0
//-----<Colors>-----------------------------------------------------------------------------------<>
RedGrid = color.new(color.red, 90)
GreenGrid = color.new(color.green, 90)
Crimson = #DC143C
LimeGreen = #32CD32
//-----<Calculate Grids>--------------------------------------------------------------------------<>
GridRange = UpperLimit - LowerLimit
GridInterval = GridRange / (No_Grids - 1)
//-----<Grid Array>-------------------------------------------------------------------------------<>
a_grid = array.new_float(No_Grids)
for x = 0 to No_Grids - 1 by 1
    array.set(a_grid, x, LowerLimit + GridInterval * x)
//-----<Get Array Function>-----------------------------------------------------------------------<>
Get_Array_Values(ArrayName, index) =>
    value = array.get(ArrayName, index)
    value
//-----<Calculate Signals>------------------------------------------------------------------------<>
Get_BuyLine_Index() =>
    Index = 0
    BuyValue = 0.0
    for x = 0 to No_Grids - 1 by 1
        BuyValue := LowerLimit + GridInterval * x
        if Extremes
            Index := high[1] > BuyValue and low <= BuyValue ? x : Index
        else
            Index := close[1] > BuyValue and close <= BuyValue ? x : Index
Get_SellLine_Index() =>
    Index = 0
    SellValue = 0.0
    for x = 0 to No_Grids - 1 by 1
        SellValue := LowerLimit + GridInterval * x
        if Extremes
            Index := low[1] < SellValue and high >= SellValue ? x : Index
        else
            Index := close[1] < SellValue and close >= SellValue ? x : Index
BuyLine_Index = Get_BuyLine_Index()
SellLine_Index = Get_SellLine_Index()
//-----<Signals>----------------------------------------------------------------------------------<>
Buy := BuyLine_Index > 0 ? true : Buy
Sell := SellLine_Index > 0 ? true : Sell
//No repeat trades at current level                                                                                     
if Extremes
    Buy := low >= SignalLine[1] - GridInterval ? false : Buy
    Sell := high <= SignalLine[1] + GridInterval ? false : Sell
    Sell
else
    Buy := close >= SignalLine[1] - GridInterval ? false : Buy
    Sell := close <= SignalLine[1] + GridInterval ? false : Sell
    Sell
//No trades outside of grid limits
Buy := close >= UpperLimit ? false : Buy
Buy := close < LowerLimit ? false : Buy
Sell := close <= LowerLimit ? false : Sell
Sell := close > UpperLimit ? false : Sell
//Direction Filter (skip one signal if against market direction)
Buy := Direction == -1 and low >= SignalLine[1] - GridInterval * 2 ? false : Buy
Sell := Direction == 1 and high <= SignalLine[1] + GridInterval * 2 ? false : Sell
//-----<Trackers>---------------------------------------------------------------------------------<>
LastSignal := Buy ? 1 : Sell ? -1 : LastSignal[1]
LastSignal_Index := Buy ? BuyLine_Index : Sell ? SellLine_Index : LastSignal_Index[1]
LastSignal_Time := Buy or Sell ? time : LastSignal_Time[1]
SignalLine := LowerLimit + GridInterval * LastSignal_Index
//-----<Plot Grids>-------------------------------------------------------------------------------<>
for i = 1 to No_Grids - 1 by 1
    Array_Value = Get_Array_Values(a_grid, i)
    ClosestGrid = Array_Value - close
    color linecolor = ClosestGrid > 0 ? RedGrid : ClosestGrid < 0 ? GreenGrid : na
    linecolor := i == LastSignal_Index ? na : linecolor
    if ShowGrids
        if ShowCurrent
            LineAbove = line.new(x1=LastSignal_Time, y1=SignalLine + GridInterval, x2=time + 1000000, y2=SignalLine + GridInterval, color=color.new(color.red, 50), width=1, extend=extend.left, xloc=xloc.bar_time)
            LineBelow = line.new(x1=LastSignal_Time, y1=SignalLine - GridInterval, x2=time + 1000000, y2=SignalLine - GridInterval, color=color.new(color.green, 50), width=1, extend=extend.left, xloc=xloc.bar_time)
            LineBelow
        else
            color colCurrent = LastSignal == 1 ? color.new(color.green, 95) : color.new(color.red, 95)
            GridLines = line.new(x1=time, y1=Array_Value, x2=time[1], y2=Array_Value, color=linecolor, width=1, extend=extend.right, xloc=xloc.bar_time)
            //draw line before latest signal
            CurrentLine = line.new(x1=LastSignal_Time, y1=SignalLine, x2=LastSignal_Time - 100000, y2=SignalLine, color=colCurrent, width=1, extend=extend.right, xloc=xloc.bar_time)
            CurrentLine
UL = ShowGrids and not ShowCurrent
c1Up = close >= UpperLimit ? color.green : color.red
c1Lo = close <= LowerLimit ? color.red : color.green
plot(UL ? UpperLimit : na, color=color.new(c1Up, 50), linewidth=2)
plot(UL ? LowerLimit : na, color=color.new(c1Lo, 50), linewidth=2)
//Lines One Above & One Below
LineAbove = SignalLine == UpperLimit ? SignalLine : SignalLine + GridInterval
LineBelow = SignalLine == LowerLimit ? SignalLine : SignalLine - GridInterval
a = plot(LineAbove, color=color.new(color.red, 100), style=plot.style_circles)
b = plot(LineBelow, color=color.new(color.green, 100), style=plot.style_circles)
boxColor = LastSignal == -1 ? color.new(color.green, 90) : color.new(color.red, 90)
boxColor := HighLight ? boxColor : na
fill(a, b, color=color.new(boxColor, 80))

strategy.entry("Long", strategy.long, when=Buy, comment=str.tostring(SignalLine))
strategy.entry("Short", strategy.short, when=Sell)
//-----<Plot Signals>-----------------------------------------------------------------------------<>
plotchar(Buy, 'Buy', color=color.new(LimeGreen, 0), size=size.tiny, location=location.belowbar, char='▲')
plotchar(Sell, 'Sell', color=color.new(Crimson, 0), size=size.tiny, location=location.abovebar, char='▼')
//-----<Alerts>-----------------------------------------------------------------------------------<>

Overview

Grid Bot Simulator [Alorse] marks a buy when price crosses down through a grid and a sell when price crosses up through a grid, using either high and low or close, and can bias entries up, down, or neutral. Settings control the range, grid count, direction, and which grids or zones are drawn. It allows several stacked entries and has no stop loss by default. Signals can change on the live bar.

Details

Published by

Alorse on GitHub

Code credit

mvs1231 || xxattaxx, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Support & resistance, Mean reversion

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy can add several entries in the same direction
  • The strategy enters trades without a stop loss

Topics

pinescriptpinescript-indicatorspinescript-strategiestrading-strategiestradingviewtradingview-alerttradingview-strategy

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Upper Limitfloat48000.0UpperLimit
Lower Limitfloat45000.0LowerLimit
Number of Grids(10)int10No_Grids
Market DirectionstringneutralDir
Show GridsbooltrueShowGrids
Highlight Trade ZonesbooltrueHighLight
Show Only Current GridsboolfalseShowCurrent
High/Low for signalsbooltrueExtremes
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

3C QFL Mean Reversal

by hasnocool
106 stars

3C QFL Mean Reversal is a strategy based on the Quickfinger Luc (QFL) trading methodology. It identifies breakdown buying opportunities below established price base levels and enters mean-reversion trades. The strategy includes multi-safety order DCA scaling,

Strategy v5 Mean reversionSupport & resistance Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3C QFL Mean Reversal

This strategy builds a spot grid between user-set high and low prices and draws it on the price chart. It splits that range into ten levels and places up to five long limit orders at the lower levels, sizing each from current equity. Orders already below price

Strategy v6 Support & resistanceMean reversion Repaints
Updated 6 weeks ago · None stated GitHub repository of Grid Spot Trading Algorithm [The Quant Science]

FairValueGap E003

by ruckard
13 stars

Calculates the distance between the current price and the opposite end of the latest fair value gap, entering trades on the expectation that price will partially fill the gap. It incorporates risk management with customizable stop loss, take profit based on ri

Strategy v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · None stated GitHub repository of FairValueGap E003

Wyckoff Phase

by muammeryldrm42
7 stars

This strategy identifies Wyckoff consolidation ranges directly on the price chart to trade potential spring and upthrust events. It calculates highest highs and lowest lows over a customizable lookback period, filtering setups by average volume spikes and ATR

Strategy v5 SMC / ICT / price actionSupport & resistance
Updated 3 months ago · MIT GitHub repository of Wyckoff Phase
5 stars

This strategy enters one long trade when the bar low comes within a set point tolerance of the prior day's low and the close finishes above that level. Entries are allowed only inside a UTC session and are blocked after a Friday cutoff hour. It stays flat unti

Strategy v5 Support & resistanceMean reversion Repaints
Updated 4 months ago · MIT GitHub repository of PDL — Prior Day Level Retest (Bull)

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers