'Tether depeg duration

Context

On May 12th, Tether ($USDT) significantly departed from it's peg of $1.

enter image description here

Indicator

I'd like to have an indicator which shows the number of continuous hours that Tether has been depegged.

We'll define depegged a candle where the high is less than 1.

Script

Here's a version of the script that appears to work:

indicator("Tether depeg hours", overlay = true)

n = ta.barssince(high[1] >= 1)

hours = math.round(timeframe.in_seconds() * n / 60 / 60)

if hours > 4   and   (high >= 1   or   barstate.islast)
    label.new(bar_index, na, str.tostring(hours) + "\nhours", yloc = yloc.abovebar, textcolor = color.white)    

Here's what it looks like:

enter image description here

Question

Is this the recommended approach for a script like this?

Or is there a more idiomatic approach?

Also, if there's already scripts out there that do something similar to this, references to those are welcome.



Sources

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

Source: Stack Overflow

Solution Source