Stock Dividend Periodicity
// 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
indicator(title='Stock Dividend Periodicity')

int day_ms = 24 * 60 * 60 * 1000
int month_ms = 30 * day_ms

select_period (float interval) =>
    switch
        (interval > 13) => 'unknown'
        (interval > 8) => 'annually'
        (interval > 5) => 'semi-annually'
        (interval > 2) => 'quarterly'
        (interval > 0) => 'monthly'
        => 'unknown'
select_stability (float shift) =>
    switch
        (shift > 1.5) => 'expanding'
        (shift < 0.5) => 'contracting'
        => 'stable'
float div = request.dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on)
var int date = time
var int diff = 0
var float stability = 1.0
if div
    date := time
    diff := date - date[1]
    stability := diff / nz(diff[1], time)
float cum_avg = ta.cum(diff) / (bar_index + 1)
float interval = cum_avg / month_ms
float stability_avg = ta.cum(stability) / (bar_index + 1)
float stability_ratio = stability / stability_avg

plot(series=interval)
plot(series=stability_ratio)

var table T = table.new(position.bottom_right, 1, 1)
table.cell(T, 0, 0, select_period(interval) + ', ' + select_stability(stability_ratio), text_color=color.blue)
