//@version=5
indicator("ATR-Levels", overlay=true)

// Input for ATR period
atrPeriod = input.int(10, title="ATR Period")

// Fetch monthly time frame high, low, and open
[monthlyHigh, monthlyLow, monthlyOpen, monthlyTime] = request.security(syminfo.tickerid, "M", [high, low, open, time])

// ATR Calculation
H_L = array.new_float()
openATR = array.new_float()

for i = 0 to atrPeriod - 1
    h = monthlyHigh[i]
    l = monthlyLow[i]
    o = monthlyOpen[i]
    array.push(H_L, h - l)
    array.push(openATR, o)

atrSum = 0.0

for i = 0 to array.size(H_L) - 2
    Sum = (array.get(H_L, i) + array.get(H_L, i+1))
    atrSum := atrSum + Sum / 2 / array.get(openATR, i+1) * 100

ATRavg = atrSum / (array.size(H_L) - 1)
ATRavg := na(ATRavg) ? 0 : ATRavg // Handle potential NA values

priceHigh = array.get(openATR, 0) * (1 + ATRavg / 100)
priceLow = array.get(openATR, 0) * (1 - ATRavg / 100)
priceHigh_5 = array.get(openATR, 0) * (1 + ATRavg / 200)
priceLow_5 = array.get(openATR, 0) * (1 - ATRavg / 200)

// Текущее время
currentTime = timenow
start_time = timestamp(year(currentTime), month(currentTime), 1)
end_time = timestamp(year(currentTime), month(currentTime) + 1, 1)

// Ensure to adjust the timeframe of lines rendering to a reasonable range of current time and future time for visibility in the script
var line priceHighLine = na
var line priceLowLine = na
var line priceHighHalfLine = na
var line priceLowHalfLine = na

var label priceHighLabel = na
var label priceLowLabel = na
var label priceHighHalfLabel = na
var label priceLowHalfLabel = na

if not na(priceHighLine)
    line.delete(priceHighLine)
if not na(priceLowLine)
    line.delete(priceLowLine)
if not na(priceHighHalfLine)
    line.delete(priceHighHalfLine)
if not na(priceLowHalfLine)
    line.delete(priceLowHalfLine)

if not na(priceHighLabel)
    label.delete(priceHighLabel)
if not na(priceLowLabel)
    label.delete(priceLowLabel)
if not na(priceHighHalfLabel)
    label.delete(priceHighHalfLabel)
if not na(priceLowHalfLabel)
    label.delete(priceLowHalfLabel)

// Plot lines using specific time positions with xloc.bar_time
priceHighLine := line.new(x1=start_time, y1=priceHigh, x2=end_time, y2=priceHigh, color=color.red, width=2, style=line.style_solid, xloc=xloc.bar_time)
priceLowLine := line.new(x1=start_time, y1=priceLow, x2=end_time, y2=priceLow, color=color.red, width=2, style=line.style_solid, xloc=xloc.bar_time)
priceHighHalfLine := line.new(x1=start_time, y1=priceHigh_5, x2=end_time, y2=priceHigh_5, color=color.blue, width=2, style=line.style_solid, xloc=xloc.bar_time)
priceLowHalfLine := line.new(x1=start_time, y1=priceLow_5, x2=end_time, y2=priceLow_5, color=color.blue, width=2, style=line.style_solid, xloc=xloc.bar_time)

// Plot labels for prices
priceHighLabel := label.new(x=start_time, y=priceHigh, text=str.tostring(priceHigh, format.mintick), color=color.red, textcolor=color.white, style=label.style_label_down, xloc=xloc.bar_time)
priceLowLabel := label.new(x=start_time, y=priceLow, text=str.tostring(priceLow, format.mintick), color=color.red, textcolor=color.white, style=label.style_label_up, xloc=xloc.bar_time)
priceHighHalfLabel := label.new(x=start_time, y=priceHigh_5, text=str.tostring(priceHigh_5, format.mintick), color=color.blue, textcolor=color.white, style=label.style_label_down, xloc=xloc.bar_time)
priceLowHalfLabel := label.new(x=start_time, y=priceLow_5, text=str.tostring(priceLow_5, format.mintick), color=color.blue, textcolor=color.white, style=label.style_label_up, xloc=xloc.bar_time) нужно сделать то же самое только для недели с периодом 52