Yearly Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © iravan

//@version=5
indicator("Yearly Candles", overlay=false, max_boxes_count=500, max_lines_count=500)

var matched = timeframe.ismonthly and timeframe.multiplier == 1
if not matched and barstate.islast
    label.new(bar_index, close, "Please change timeframe to 1M", color=color.yellow, style=label.style_label_center)
    
var float o = na
var float h = na
var float l = na
var float c = na
var float po = na
var float ph = na
var float pl = na
var float pc = na

var pos = color.teal
var neg = color.red

var start_index = 0
var prev_year = 0
if prev_year != year(time_close) and matched
    prev_year := year(time_close)
    c := close[1]
    if not barstate.isfirst
        mid_index = math.floor((start_index + bar_index) / 2)
        box.new(start_index + 1, o, bar_index - 1, c, bgcolor=c>=o?pos:neg, border_width=0)
        line.new(start_index + 1, o, bar_index - 1, o, color=c>=o?pos:neg)
        line.new(mid_index, l, mid_index, h, color=c>=o?pos:neg)

    po := o
    ph := h
    pl := l
    pc := c

    o := open
    h := high
    l := low

    start_index := bar_index > 0 ? bar_index: 0
    
l := low < l ? low : l
h := high > h  ? high : h    

if barstate.islast and matched
    status = "O:" + str.tostring(o) +",  H:" + str.tostring(h) + ",  L:" + str.tostring(l) +",  C:" + str.tostring(close) + ",  " + str.tostring(close - c) +",  (" + str.tostring(math.round((close - c)/c*100, 2)) + "%)"
    c := close
    
    box.new(start_index + 1, o, bar_index + (12 - month(time)), c, bgcolor=c>=o?pos:neg, border_color=color.rgb(0,0,0,100))
    line.new(bar_index - month(time) + 6 + 1, l, bar_index - month(time) + 6 + 1, h, color=c>=o?pos:neg)
    label.new(bar_index - month(time) + 6 + 8, close, status, color=color.rgb(255,255,0,90), textcolor=c>=o?pos:neg, style=label.style_label_left)

plot(year(time_close) == year(timenow)?year(time_close[12]):year(time_close), title="Year", display=display.status_line+display.data_window, editable=false)
plot(po, offset=-12, title="Open", color=pc>=po?pos:neg, display=display.status_line+display.data_window, editable=false)
plot(ph, offset=-12, title="High", color=pc>=po?pos:neg, display=display.status_line+display.data_window, editable=false)
plot(pl, offset=-12, title="Low", color=pc>=po?pos:neg, display=display.status_line+display.data_window, editable=false)
plot(pc, offset=-12, title="Close", color=pc>=po?pos:neg, display=display.status_line+display.data_window, editable=false)
plot(pc - pc[12], offset=-12, title="Change", color=pc>=po?pos:neg, display=display.status_line+display.data_window, editable=false)
plot(math.round((pc - pc[12]) / pc[12] * 100, 2), offset=-12, title="Change %", color=pc>=po?pos:neg, display=display.status_line+display.data_window, editable=false)
