Supply and Demand
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © StockJustice

//@version=5
indicator("Supply and Demand (Responsive)", shorttitle="S&D", overlay=true)

// Get high and low for the last 10 bars
pivotHigh_1 = ta.highest(high, 10)
pivotLow_1 = ta.lowest(low, 10)

// Get high and low for the 10th to 19th last bars
pivotHigh_2 = ta.highest(high[10], 10)
pivotLow_2 = ta.lowest(low[10], 10)

// Get high and low for the 20th to 29th last bars
pivotHigh_3 = ta.highest(high[20], 10)
pivotLow_3 = ta.lowest(low[20], 10)

// Get high and low for the 30th to 39th last bars
pivotHigh_4 = ta.highest(high[30], 10)
pivotLow_4 = ta.lowest(low[30], 10)

// Get high and low for the 40th to 49th last bars
pivotHigh_5 = ta.highest(high[40], 10)
pivotLow_5 = ta.lowest(low[40], 10)

// Get high and low for the 50th to 59th last bars
pivotHigh_6 = ta.highest(high[50], 10)
pivotLow_6 = ta.lowest(low[50], 10)

// Get high and low for the 60th to 69th last bars
pivotHigh_7 = ta.highest(high[60], 10)
pivotLow_7 = ta.lowest(low[60], 10)

// Get high and low for the 70th to 79th last bars
pivotHigh_8 = ta.highest(high[70], 10)
pivotLow_8 = ta.lowest(low[70], 10)

// Get high and low for the 80th to 89th last bars
pivotHigh_9 = ta.highest(high[80], 10)
pivotLow_9 = ta.lowest(low[80], 10)

// Get high and low for the 90th to 99th last bars
pivotHigh_10 = ta.highest(high[90], 10)
pivotLow_10 = ta.lowest(low[90], 10)

// Create an input for the toggle
toggle = input(false, "Limit to specific timeframes? (15m, 30m, 1hr, 4hr, 1d)")
plotCircles = input(true, title="Plot Circles?")
plotLines = input(true, title="Plot Lines?")


is_1h = (timeframe.period == "15" or timeframe.period == "30" or timeframe.period == "60" or timeframe.period == "240" or timeframe.period == "D")

// Get user input
ranges_to_plot = input(6, title="Number of Ranges to Plot (1-10)")
candlesToPlot = input(5, title="Number of Last Ranges to Show for Circles(1-?)")

// Plot pivot points
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 1 ? pivotHigh_1 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 0-9")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 1 ? pivotLow_1 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 0-9")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 2 ? pivotHigh_2: na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 10-19")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 2 ? pivotLow_2: na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 10-19")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 3 ? pivotHigh_3 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 20-29")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 3 ? pivotLow_3: na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 20-29")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 4 ? pivotHigh_4 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 30-39")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 4 ? pivotLow_4 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 30-39")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 5 ? pivotHigh_5 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 40-49")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 5 ? pivotLow_5 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 40-49")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 6 ? pivotHigh_6 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 50-59")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 6 ? pivotLow_6 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 50-59")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 7 ? pivotHigh_7 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 60-69")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 7 ? pivotLow_7 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 60-69")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 8 ? pivotHigh_8 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 70-79")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 8 ? pivotLow_8 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 70-79")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 9 ? pivotHigh_9 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 80-89")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 9 ? pivotLow_9 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 80-89")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 10 ? pivotHigh_10 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.red, 55), linewidth=2, title="Pivot High 90-99")
plot((toggle and is_1h or not toggle) and plotCircles and ranges_to_plot >= 10 ? pivotLow_10 : na, show_last = candlesToPlot, style=plot.style_circles, color=color.new(color.green, 55), linewidth=2, title="Pivot Low 90-99")


// 
var line line_pivotHigh_1 = na
var line line_pivotLow_1 = na
var line line_pivotHigh_2 = na
var line line_pivotLow_2 = na
var line line_pivotHigh_3 = na
var line line_pivotLow_3 = na
var line line_pivotHigh_4 = na
var line line_pivotLow_4 = na
var line line_pivotHigh_5 = na
var line line_pivotLow_5 = na
var line line_pivotHigh_6 = na
var line line_pivotLow_6 = na
var line line_pivotHigh_7 = na
var line line_pivotLow_7 = na
var line line_pivotHigh_8 = na
var line line_pivotLow_8 = na
var line line_pivotHigh_9 = na
var line line_pivotLow_9 = na
var line line_pivotHigh_10 = na
var line line_pivotLow_10 = na

if barstate.islast and (toggle and is_1h or not toggle)
    if ranges_to_plot >= 1 and plotLines
        line.delete(line_pivotHigh_1)
        line_pivotHigh_1 := line.new(x1=bar_index[9], y1=pivotHigh_1, x2=bar_index, y2=pivotHigh_1, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_1)
        line_pivotLow_1 := line.new(x1=bar_index[9], y1=pivotLow_1, x2=bar_index, y2=pivotLow_1, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 2 and plotLines
        line.delete(line_pivotHigh_2)
        line_pivotHigh_2 := line.new(x1=bar_index[19], y1=pivotHigh_2, x2=bar_index, y2=pivotHigh_2, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_2)
        line_pivotLow_2 := line.new(x1=bar_index[19], y1=pivotLow_2, x2=bar_index, y2=pivotLow_2, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 3 and plotLines
        line.delete(line_pivotHigh_3)
        line_pivotHigh_3 := line.new(x1=bar_index[29], y1=pivotHigh_3, x2=bar_index, y2=pivotHigh_3, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_3)
        line_pivotLow_3 := line.new(x1=bar_index[29], y1=pivotLow_3, x2=bar_index, y2=pivotLow_3, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 4 and plotLines
        line.delete(line_pivotHigh_4)
        line_pivotHigh_4 := line.new(x1=bar_index[39], y1=pivotHigh_4, x2=bar_index, y2=pivotHigh_4, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_4)
        line_pivotLow_4 := line.new(x1=bar_index[39], y1=pivotLow_4, x2=bar_index, y2=pivotLow_4, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 5 and plotLines
        line.delete(line_pivotHigh_5)
        line_pivotHigh_5 := line.new(x1=bar_index[49], y1=pivotHigh_5, x2=bar_index, y2=pivotHigh_5, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_5)
        line_pivotLow_5 := line.new(x1=bar_index[49], y1=pivotLow_5, x2=bar_index, y2=pivotLow_5, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 6 and plotLines
        line.delete(line_pivotHigh_6)
        line_pivotHigh_6 := line.new(x1=bar_index[59], y1=pivotHigh_6, x2=bar_index, y2=pivotHigh_6, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_6)
        line_pivotLow_6 := line.new(x1=bar_index[59], y1=pivotLow_6, x2=bar_index, y2=pivotLow_6, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 7 and plotLines
        line.delete(line_pivotHigh_7)
        line_pivotHigh_7 := line.new(x1=bar_index[69], y1=pivotHigh_7, x2=bar_index, y2=pivotHigh_7, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_7)
        line_pivotLow_7 := line.new(x1=bar_index[69], y1=pivotLow_7, x2=bar_index, y2=pivotLow_7, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 8 and plotLines
        line.delete(line_pivotHigh_8)
        line_pivotHigh_8 := line.new(x1=bar_index[79], y1=pivotHigh_8, x2=bar_index, y2=pivotHigh_8, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_8)
        line_pivotLow_8 := line.new(x1=bar_index[79], y1=pivotLow_8, x2=bar_index, y2=pivotLow_8, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 9 and plotLines
        line.delete(line_pivotHigh_9)
        line_pivotHigh_9 := line.new(x1=bar_index[89], y1=pivotHigh_9, x2=bar_index, y2=pivotHigh_9, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_9)
        line_pivotLow_9 := line.new(x1=bar_index[89], y1=pivotLow_9, x2=bar_index, y2=pivotLow_9, color=color.new(color.green, 55), width=1)

    if ranges_to_plot >= 10 and plotLines
        line.delete(line_pivotHigh_10)
        line_pivotHigh_10 := line.new(x1=bar_index[99], y1=pivotHigh_10, x2=bar_index, y2=pivotHigh_10, color=color.new(color.red, 55), width=1)
        line.delete(line_pivotLow_10)
        line_pivotLow_10 := line.new(x1=bar_index[99], y1=pivotLow_10, x2=bar_index, y2=pivotLow_10, color=color.new(color.green, 55), width=1)






// Check if the current timeframe is 1 minute
// is_1m = timeframe.period == "1" or timeframe.period == "5"

// Get high and low for the last 10 bars on hourly timeframe
// pivotHigh_1_hourly = request.security(syminfo.tickerid, "60", ta.highest(high, 10))
// pivotLow_1_hourly = request.security(syminfo.tickerid, "60", ta.lowest(low, 10))

// Plot pivot points if the timeframe is 1 minute
// plot(is_1m ? pivotHigh_1_hourly : na, show_last = 1, style=plot.style_circles, color=color.red, linewidth=2, title="Pivot High 0-9 Hourly")
// plot(is_1m ? pivotLow_1_hourly : na, show_last = 1, style=plot.style_circles, color=color.green, linewidth=2, title="Pivot Low 0-9 Hourly")



//

// plot(is_1m ? pivotHigh_2_hourly : na, show_last = 1, color=color.red)
// plot(is_1m ? pivotLow_2_hourly : na, show_last = 1, color=color.blue)

// //










// 

// //


// //