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

//@version=5

// @description Week of Month function.
library(title='FunctionWeekofmonth')

// @function Week of month for provided unix time.
// @param utime int, unix timestamp.
// @returns int
export weekofmonth(int utime) => //{
    int _year = year(utime)
    int _month = month(utime)
    int _yweek = weekofyear(utime)
    int _sweek = weekofyear(timestamp(_year, _month, 1, 0, 0, 0))
    _o = _yweek - _sweek + 1
    if _o > 0
        _o
    else
        math.round(dayofmonth(utime) / 7) + 1
//{ usage:
m = weekofmonth(input.time(timestamp('2021-01-01')))
m2 = weekofmonth(time)
plot(m, title='M', color=color.silver)
plot(m2, title='M', color=color.yellow)
//}}