3M_RANGE/ErkOzi/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ErkOzi

//@version=5
indicator(title='3M_RANGE', shorttitle='3M_RANGE', overlay=true)

// Define the range period
rangePeriod = '3M'
openPrice = request.security(syminfo.tickerid, rangePeriod, open)
highPrice = request.security(syminfo.tickerid, rangePeriod, high)
lowPrice = request.security(syminfo.tickerid, rangePeriod, low)
midPrice = (highPrice + lowPrice) / 2

// Function to check if it's Monday
isMonday() =>
    dayofweek(time) == dayofweek.monday ? 1 : 0

// Store the 3-month range levels
var float mondayHighRange = na
var float mondayLowRange = na

if isMonday()
    mondayHighRange := highPrice
    mondayLowRange := lowPrice

// Plot the Monday open price levels
plot(isMonday() ? openPrice : na, title='3-M Eq / openPrice', style=plot.style_circles, linewidth=2, color=color.new(color.purple, 0))

// Extend the lines until the next Monday
var float nextMondayHigh = na
var float nextMondayLow = na

if isMonday()
    nextMondayHigh := highPrice
    nextMondayLow := lowPrice
else
    nextMondayHigh := nz(nextMondayHigh[1], mondayHighRange)
    nextMondayLow := nz(nextMondayLow[1], mondayLowRange)

// Plot the next 3-month high and low ranges
plot(nextMondayHigh, title='Next 3-M High Range', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
plot(nextMondayLow, title='Next 3-M Low Range', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))

// Plot the 0.50 line
plot((nextMondayHigh + nextMondayLow) / 2, title='0.50', style=plot.style_linebr, linewidth=1, color=color.new(color.red, 0))

// Calculate Fibonacci levels
fib272 = nextMondayHigh + 0.272 * (nextMondayHigh - nextMondayLow)
fib414 = nextMondayHigh + 0.414 * (nextMondayHigh - nextMondayLow)
fib500 = nextMondayHigh + 0.5 * (nextMondayHigh - nextMondayLow)
fibNegative272 = nextMondayLow - 0.272 * (nextMondayHigh - nextMondayLow)
fibNegative414 = nextMondayLow - 0.414 * (nextMondayHigh - nextMondayLow)
fibNegative500 = nextMondayLow - 0.5 * (nextMondayHigh - nextMondayLow)
fibNegative1 = nextMondayLow - 1 * (nextMondayHigh - nextMondayLow)
fib2 = nextMondayHigh + 1 * (nextMondayHigh - nextMondayLow)

// Plot Fibonacci levels
plot(fib272, title='0.272 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.blue, 0))
plot(fib414, title='0.414 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.blue, 0))
plot(fib500, title='0.500 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.red, 0))
plot(fibNegative272, title='-0.272 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.blue, 0))
plot(fibNegative414, title='-0.414 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.blue, 0))
plot(fibNegative500, title='-0.500 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.red, 0))
plot(fibNegative1, title='-1 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.white, 0))
plot(fib2, title='1 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.white, 0))

// Calculate the values for lines 0.25 above and below
var float lineAbove = na
var float lineBelow = na
lineAbove := (nextMondayHigh + nextMondayLow) / 2 + 0.25 * (nextMondayHigh - nextMondayLow)
lineBelow := (nextMondayHigh + nextMondayLow) / 2 - 0.25 * (nextMondayHigh - nextMondayLow)

// Plot lines 0.25 above and below with dashed line style
plot(lineAbove, title='0.25 Above', color=color.new(color.yellow, 0), linewidth=1, style=plot.style_stepline)
plot(lineBelow, title='0.25 Below', color=color.new(color.yellow, 0), linewidth=1, style=plot.style_stepline)
