'Pine Script: how do I get highest high of the first five trading days every month

I want to get highest high of the first five trading days every month. Can anyone help me out with this?



Solution 1:[1]

//@version=5
indicator("hh from the beginning of the month", overlay = true)

var float hh = na
var int days_elapsed = na

new_month = ta.change(month) != 0
new_day = ta.change(dayofmonth) != 0

if new_month
    hh := high
    days_elapsed := 1

if new_day and not new_month
    days_elapsed += 1

if days_elapsed <= 5
    hh := math.max(hh, high)

last_months_hh = ta.valuewhen(new_month, hh[1], 0)

bgcolor(new_month ? color.yellow : na)
plot(hh)
plot(last_months_hh, color = color.silver)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rumpypumpydumpy