Simple Grid Lines Visualizer
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lecjacks

//@version=5
indicator("Grid Lines Visualizer", overlay=true)

// Input prices
top_price = input.float(31400., "Top Price")
bottom_price = input.float(24200., "Bottom Price")

use = input.string(title="Use", options=["Num Grid", "Profit per Grid"], defval="Num Grid", inline = "03", tooltip="How grid lines are computed.\nNum grid: Explicit.\nProfit per Grid: First grid is computed from bottom up as\n- Start: bottom price\n- End: bottom price * (1 + percent profit).\nHow many number of these grids may enter the range is computed and then rounded to bottom integer.")

num_grids = input.int(30, "Number of Grids", minval = 2, inline="01", tooltip="Grid SPACES or (Number of Grid LINES - 1)")
profit_per_grid = input.float(0.6, "% Profit per Grid", minval = 0.0000001, inline = "02", tooltip="Minimum profit received buying at one grid line, selling on next one.")

externalLineColour = input.color(color.new(color.orange, 40), title="Top/Bottom Lines Colour")
internalLineColour = input.color(color.new(color.blue, 40), title="Internal Line Colour")

// Compute the price increment between each gridline
var float grid_range = top_price - bottom_price
var float price_increment = 0.0
if use == "Num Grid"
    price_increment := grid_range / (num_grids)
else if use == "Profit per Grid"
    price_increment := math.min(bottom_price * profit_per_grid / 100, grid_range)
    num_grids := int(grid_range / price_increment)
    price_increment := grid_range / (num_grids - 1)

// Draw horizontal lines for top and bottom channels
line.new(x1=bar_index[1], y1=bottom_price, x2=bar_index, y2=bottom_price, style = line.style_solid, color = externalLineColour, extend = extend.both)
line.new(x1=bar_index[1], y1=top_price, x2=bar_index, y2=top_price, style = line.style_solid, color = externalLineColour, extend = extend.both)

for i = 1 to num_grids-1
    price = bottom_price + i * price_increment
    line.new(x1=bar_index[1], y1=price, x2=bar_index, y2=price, style = line.style_solid, color = internalLineColour, extend = extend.both)