I know I'm late, but in v6 there is a built-in function for this.
https://www.tradingview.com/pine-script-reference/v5/#fun_ticker.standard