Strategy · Pine Script v6 · On the chart

Grid Spot Trading Algorithm [The Quant Science]

by Al0010· code credited to thequantscience

This strategy builds a spot grid between user-set high and low prices and draws it on the price chart.

View on GitHub

Repaints

GitHub stars87on the repository
Pine versionv6current
Lines of code181
Settings5inputs
LicenceNone statedfile: MPL-2.0
Last update6 weeks ago16 Sep 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

181 lines of Pine Script v6 from main.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

main.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thequantscience
                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
//@version=6
strategy(
     title = 'Grid Spot Trading Algorithm V2 [The Quant Science]', 
     overlay = true,
     calc_on_every_tick = true,
     initial_capital = 10000, 
     commission_type = strategy.commission.percent, 
     commission_value = 0.10, 
     pyramiding = 5,
     default_qty_type = strategy.percent_of_equity,
     close_entries_rule = 'ANY'
     )

high_price  = input.price(defval = 0.00, title = 'High price: ', group = "TOP PRICE GRID", confirm = true, tooltip = "Top grid price.")
close_price = input.price(defval = 0.00, title = 'Low price: ',  group = "BOTTOM PRICE GRID", confirm = true, tooltip = "Bottom grid price.")

grid_destroyer = input.bool(defval = false,title = 'Active grid destroyer:',group = "STOP LOSS CONFIGURATION", inline = "SL", tooltip = "Destroy the grid when price crossunder the stop loss level.", confirm = false)
stop_level     = input.price(defval = 0.00, title = '',group = "STOP LOSS CONFIGURATION", inline = "SL", confirm = false, tooltip = "Stop loss price level. Set price level to destroy the grid and close all the positions.")

grid_range = high_price - close_price

var float grid_1  = 0
var float grid_2  = 0
var float grid_3  = 0
var float grid_4  = 0
var float grid_5  = 0
var float grid_6  = 0
var float grid_7  = 0
var float grid_8  = 0
var float grid_9  = 0
var float grid_10 = 0

var float grid_factor = 0 

if (high_price > 0 or close_price > 0)

    grid_factor := grid_range / 9

    grid_1 := (high_price)

    grid_2 := (high_price - (grid_factor * 1)) 
    grid_3 := (high_price - (grid_factor * 2)) 
    grid_4 := (high_price - (grid_factor * 3)) 
    grid_5 := (high_price - (grid_factor * 4))

    grid_6  := (high_price - (grid_factor * 5)) 
    grid_7  := (high_price - (grid_factor * 6)) 
    grid_8  := (high_price - (grid_factor * 7)) 
    grid_9  := (high_price - (grid_factor * 8))

    grid_10 := (close_price)
    
var float lots = 0
var float account_balance = 0 

long = (ta.cross(close, grid_5))

if (close < high_price or close > close_price)

    if strategy.opentrades==0

        account_balance := strategy.equity
        lots := (account_balance / close) / 5
        strategy.entry(id = "O #1", direction = strategy.long, qty = lots, limit = grid_6)
        strategy.entry(id = "O #2", direction = strategy.long, qty = lots, limit = grid_7)
        strategy.entry(id = "O #3", direction = strategy.long, qty = lots, limit = grid_8)
        strategy.entry(id = "O #4", direction = strategy.long, qty = lots, limit = grid_9)
        strategy.entry(id = "O #5", direction = strategy.long, qty = lots, limit = grid_10)
        
        if (close < grid_6)
            strategy.cancel(id = "O #1")
        if (close < grid_7)
            strategy.cancel(id = "O #2")
        if (close < grid_8)
            strategy.cancel(id = "O #3")
        if (close < grid_9)
            strategy.cancel(id = "O #4")
        if (close < grid_10)
            strategy.cancel(id = "O #5")

Exit(type) => 
    
    switch type

        "Grid level 5" => grid_1
        "Grid level 4" => grid_2
        "Grid level 3" => grid_3
        "Grid level 2" => grid_4
        "Grid level 1" => grid_5

exit_input = input.string("Grid level 1", title = "Select exit", options = ["Grid level 1", "Grid level 2", "Grid level 3", "Grid level 4", "Grid level 5"], tooltip = "Starting from the middle to the top.", group = "Exit Configuration")
exit_price = Exit(exit_input)

if ta.crossover(close, exit_price)

    strategy.close_all("Close-Position")

    strategy.cancel("O #1")
    strategy.cancel("O #2")
    strategy.cancel("O #3")
    strategy.cancel("O #4")
    strategy.cancel("O #5")

if (high_price > 0 or close_price > 0) and grid_destroyer==true 
    if ta.crossunder(close, stop_level)
        strategy.close_all("Grid Destroyed") 

var float new_ten_grid_1  = 0 
var float new_ten_grid_2  = 0 
var float new_ten_grid_3  = 0 
var float new_ten_grid_4  = 0 
var float new_ten_grid_5  = 0 
var float new_ten_grid_6  = 0 
var float new_ten_grid_7  = 0 
var float new_ten_grid_8  = 0 
var float new_ten_grid_9  = 0 
var float new_ten_grid_10 = 0 

var float grid_destroyed_ten = 0 

if (high_price > 0 or close_price > 0)

    new_ten_grid_1  := grid_1
    new_ten_grid_2  := grid_2
    new_ten_grid_3  := grid_3
    new_ten_grid_4  := grid_4
    new_ten_grid_5  := grid_5
    new_ten_grid_6  := grid_6
    new_ten_grid_7  := grid_7
    new_ten_grid_8  := grid_8
    new_ten_grid_9  := grid_9
    new_ten_grid_10 := grid_10

    if grid_destroyer == true 
        grid_destroyed_ten := stop_level

else 

    new_ten_grid_1  := na
    new_ten_grid_2  := na
    new_ten_grid_3  := na
    new_ten_grid_4  := na
    new_ten_grid_5  := na
    new_ten_grid_6  := na
    new_ten_grid_7  := na
    new_ten_grid_8  := na
    new_ten_grid_9  := na
    new_ten_grid_10 := na

    if grid_destroyer == false 
        grid_destroyed_ten := na

color GREEN_BASE = #35f83b
color RED_BASE   = #fc3636

p1  = plot(new_ten_grid_1,  "Grid Line 1",  color = color.new(GREEN_BASE, 90))
p2  = plot(new_ten_grid_2,  "Grid Line 2",  color = color.new(GREEN_BASE, 90))
p3  = plot(new_ten_grid_3,  "Grid Line 3",  color = color.new(GREEN_BASE, 85))
p4  = plot(new_ten_grid_4,  "Grid Line 4",  color = color.new(GREEN_BASE, 80))
p5  = plot(new_ten_grid_5,  "Grid Line 5",  color = color.new(GREEN_BASE, 70))
p6  = plot(new_ten_grid_6,  "Grid Line 6",  color = color.new(GREEN_BASE, 60))
p7  = plot(new_ten_grid_7,  "Grid Line 7",  color = color.new(RED_BASE, 60))
p8  = plot(new_ten_grid_8,  "Grid Line 8",  color = color.new(RED_BASE, 70))
p9  = plot(new_ten_grid_9,  "Grid Line 9",  color = color.new(RED_BASE, 80))
p10 = plot(new_ten_grid_10, "Grid Line 10", color = color.new(RED_BASE, 85))

plot(grid_destroyed_ten, "Grid Destroyed", color = color.new(RED_BASE, 80), linewidth = 10)

fill(p1, p2, color = color.new(GREEN_BASE, 90), title = "Fill Zone 1-2")
fill(p2, p3, color = color.new(GREEN_BASE, 85), title = "Fill Zone 2-3")
fill(p3, p4, color = color.new(GREEN_BASE, 80), title = "Fill Zone 3-4")
fill(p4, p5, color = color.new(GREEN_BASE, 70), title = "Fill Zone 4-5")
fill(p5, p6, color = color.new(GREEN_BASE, 60), title = "Fill Zone 5-6")

fill(p6, p7, color = color.new(RED_BASE, 60), title = "Fill Zone 6-7")
fill(p7, p8, color = color.new(RED_BASE, 70), title = "Fill Zone 7-8")
fill(p8, p9, color = color.new(RED_BASE, 80), title = "Fill Zone 8-9")
fill(p9, p10, color = color.new(RED_BASE, 85), title = "Fill Zone 9-10")

Overview

Grid Spot Trading Algorithm [The Quant Science] 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 are cancelled. An optional grid destroyer can close all positions when price crosses a stop level, but it is off by default and the shown logic has no take profit. It recalculates on every tick, so signals can change on the live bar.

Details

Published by

Al0010 on GitHub

Code credit

thequantscience, as written in the script's header

Draws

On the price chart

Markets

Any symbol

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

bot-tradinggrid-tradingpinescriptpinescript-strategiestrading-algorithmstradingview-pine-scriptsgrid-botpinescript-algorithmspinescript-v6

Settings

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

SettingTypeDefaultVariable
High price: price0.00high_price
Low price: price0.00close_price
Active grid destroyer:boolfalsegrid_destroyer
stop_levelprice0.00stop_level
Select exitstringGrid level 1exit_input
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

330 stars

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. It marks a buy when price crosses down through a grid and a sell when price crosses up through a grid, using either

Strategy v5 Support & resistanceMean reversion Repaints
Updated 2 months ago · None stated GitHub repository of Grid Bot Simulator [Alorse]

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

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 5 months ago · MIT GitHub repository of PDL — Prior Day Level Retest (Bull)

More Support & resistance scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers