Monday_Weekly_Range/ErkOzi/Deviation Level/V1
// 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='Monday_Weekly_Range/ErkOzi/Deviation Level/V1', shorttitle='Monday_Range/ErkOzi', overlay=true)

// holds the daily price levels
openPrice = request.security(syminfo.tickerid, 'D', open)
highPrice = request.security(syminfo.tickerid, 'D', high)
lowPrice = request.security(syminfo.tickerid, 'D', low)
midPrice = math.avg(highPrice, lowPrice)

//function which is called by plot to establish if it is Monday
isMonday() =>
    dayofweek(time) == dayofweek.monday ? 1 : 0

// store the Monday range levels
var float mondayHighRange = na
var float mondayLowRange = na

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

// plot the Monday levels
plot(isMonday() and midPrice ? midPrice : na, title='Monday Eq / midPrice', style=plot.style_circles, linewidth=2, color=color.new(color.black, 0))
plot(isMonday() and openPrice ? openPrice : na, title='Monday Eq / openPrice', style=plot.style_circles, linewidth=2, color=color.new(color.purple, 0))
plot(isMonday() and highPrice ? highPrice : na, title='Monday High', style=plot.style_circles, linewidth=2, color=color.new(color.green, 0))
plot(isMonday() and lowPrice ? lowPrice : na, title='Monday Low', style=plot.style_circles, linewidth=2, color=color.new(color.green, 0))

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

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

plot(nextMondayHigh, title='Next Monday High Range', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
plot(nextMondayLow, title='Next Monday 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)
fib618 = nextMondayHigh + 0.618 * (nextMondayHigh - nextMondayLow)
fibNegative272 = nextMondayLow - 0.272 * (nextMondayHigh - nextMondayLow)
fibNegative414 = nextMondayLow - 0.414 * (nextMondayHigh - nextMondayLow)
fibNegative500 = nextMondayLow - 0.5 * (nextMondayHigh - nextMondayLow)
fibNegative618 = nextMondayLow - 0.618 * (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(fib618, title='0.618 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.yellow, 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(fibNegative618, title='-0.618 Fibonacci', style=plot.style_linebr, linewidth=1, color=color.new(color.yellow, 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=0, style=plot.style_circles)
plot(lineBelow, title='0.25 Below', color=color.new(color.yellow, 0), linewidth=0, style=plot.style_circles)

