// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © julzen2

//@version=5
indicator(title="Weekly Pivot", shorttitle="WeeklyPivot", overlay=true)

//--- Global variables for pivot values (will retain their value across bars)
var float P_val  = na
var float S1_val = na
var float R1_val = na
var float S2_val = na
var float R2_val = na
var float S3_val = na
var float R3_val = na

//--- Global variables for labels (will hold label IDs)
var label P_label  = na
var label S1_label = na
var label R1_label = na
var label S2_label = na
var label R2_label = na
var label S3_label = na
var label R3_label = na

// --- Input for font size (from MQL4's fontsize)
var int fontsize_input = input.int(10, "Label Font Size", minval=6, maxval=24)

// --- Detect start of a new week (typically Monday, first bar of the week)
// `ta.change(time("W"))` returns true on the first bar of a new weekly period.
new_week = ta.change(time("W"))

// --- Get previous week's data
// request.security is used to fetch historical High, Low, and Close from the *completed* previous week.
// `[1]` refers to the previous weekly bar.
// `lookahead=barmerge.lookahead_on` ensures we get the value from the *closed* previous week.
[prev_w_high, prev_w_low, prev_w_close] = request.security(syminfo.tickerid, "W", [high[1], low[1], close[1]], lookahead=barmerge.lookahead_on)

// --- Main Calculation: Calculate pivots only on the first bar of a new week ---
if new_week
    // The current bar's open is the open of the new week.
    current_week_open = open

    // Calculate Pivot Points based on the MQL4 formula:
    // P = (Previous Week High + Previous Week Low + Current Week Open + Previous Week Close) / 4
    P_val  := (prev_w_high + prev_w_low + current_week_open + prev_w_close) / 4
    R1_val := (2 * P_val) - prev_w_low
    S1_val := (2 * P_val) - prev_w_high
    R2_val := P_val + (prev_w_high - prev_w_low)
    S2_val := P_val - (prev_w_high - prev_w_low)
    R3_val := (2 * P_val) + (prev_w_high - (2 * prev_w_low))
    S3_val := (2 * P_val) - ((2 * prev_w_high) - prev_w_low)

    // --- Create/Update Labels for Pivot Levels ---
    // If labels don't exist yet (first run of script, or after reset), create them.
    // Otherwise, update their position and value.
    if na(P_label) // Check if the main pivot label is 'na' to know if all labels need creating
        // Create labels with transparent background (`color.new(color.white, 100)`)
        P_label  := label.new(x=time, y=P_val, text="Weekly Pivot Point", xloc=xloc.bar_time, yloc=yloc.price,
                             color=color.new(color.white, 100), textcolor=color.rgb(255, 0, 255), size=size.small, style=label.style_label_left) // Magenta
        S1_label := label.new(x=time, y=S1_val, text="wS 1", xloc=xloc.bar_time, yloc=yloc.price,
                              color=color.new(color.white, 100), textcolor=color.rgb(65, 105, 225), size=size.small, style=label.style_label_left) // RoyalBlue
        R1_label := label.new(x=time, y=R1_val, text="wR 1", xloc=xloc.bar_time, yloc=yloc.price,
                              color=color.new(color.white, 100), textcolor=color.rgb(220, 20, 60), size=size.small, style=label.style_label_left) // Crimson
        S2_label := label.new(x=time, y=S2_val, text="wS 2", xloc=xloc.bar_time, yloc=yloc.price,
                              color=color.new(color.white, 100), textcolor=color.rgb(65, 105, 225), size=size.small, style=label.style_label_left) // RoyalBlue
        R2_label := label.new(x=time, y=R2_val, text="wR 2", xloc=xloc.bar_time, yloc=yloc.price,
                              color=color.new(color.white, 100), textcolor=color.rgb(220, 20, 60), size=size.small, style=label.style_label_left) // Crimson
        S3_label := label.new(x=time, y=S3_val, text="wS 3", xloc=xloc.bar_time, yloc=yloc.price,
                              color=color.new(color.white, 100), textcolor=color.rgb(46, 139, 87), size=size.small, style=label.style_label_left) // SeaGreen
        R3_label := label.new(x=time, y=R3_val, text="wR 3", xloc=xloc.bar_time, yloc=yloc.price,
                              color=color.new(color.white, 100), textcolor=color.rgb(46, 139, 87), size=size.small, style=label.style_label_left) // SeaGreen
   
    // Update labels (for existing ones, and for newly created ones on the same bar)
    label.set_xy(P_label, x=time, y=P_val)
    label.set_xy(S1_label, x=time, y=S1_val)
    label.set_xy(R1_label, x=time, y=R1_val)
    label.set_xy(S2_label, x=time, y=S2_val)
    label.set_xy(R2_label, x=time, y=R2_val)
    label.set_xy(S3_label, x=time, y=S3_val)
    label.set_xy(R3_label, x=time, y=R3_val)

    // Update label text if you want to include values (MQL4 only used fixed text)
    // For example: `label.set_text(P_label, "Weekly Pivot Point: " + str.tostring(P_val))`
    // But sticking to MQL4 original, fixed text is used.

    // Update font size (MQL4 only sets this on init, but we can make it dynamic here if desired)
    // For a constant font size, it's better to pass it directly to label.new and avoid updating every week.
    // However, if we added an input for fontsize, we would set it here.
    // Example: label.set_size(P_label, f_get_label_size(fontsize_input))
    // For now, size.small is hardcoded based on MQL4's fontsize=10.

// --- Plotting the Pivot Lines ---
// The `P_val`, `S1_val`, etc. variables retain their last calculated value thanks to `var`.
// This makes them horizontal lines spanning the entire week until a new calculation occurs.

plot(P_val,  title="Weekly Pivot Point", color=color.rgb(255, 0, 255), linewidth=2, style=plot.style_line) // Magenta
plot(S1_val, title="W_S 1",              color=color.rgb(65, 105, 225), linewidth=2, style=plot.style_line) // RoyalBlue
plot(R1_val, title="W_R 1",              color=color.rgb(220, 20, 60),  linewidth=2, style=plot.style_line) // Crimson
plot(S2_val, title="W_S 2",              color=color.rgb(65, 105, 225), linewidth=2, style=plot.style_line) // RoyalBlue
plot(R2_val, title="W_R 2",              color=color.rgb(220, 20, 60),  linewidth=2, style=plot.style_line) // Crimson
plot(S3_val, title="W_S 3",              color=color.rgb(46, 139, 87),  linewidth=2, style=plot.style_line) // SeaGreen
plot(R3_val, title="W_R 3",              color=color.rgb(46, 139, 87),  linewidth=2, style=plot.style_line) // SeaGreen
