0

Im trying to plot the percentage change from previous daily close to current price. Everything works fine, but when a new candle closes the value returns to 0 and only plots the current bar change instead of the daily change. If I refresh the page then its back to normal, until another candle closes.

study("Percent change from daily", precision=2)
a = input(title="Symbol", type=input.symbol, defval="ftx:btcperp")
b = input(title="Resolution", type=input.resolution, defval="D")
c = security(a, b, close)
d = (close - c[1]) / c[1] * 100
plot(d)
tart.t
  • 5
  • 2

1 Answers1

0

This calculates the ROC between the chart's close and the non-repainting HTF value fetched with the security() call, which is the close of the last completed day of symbol a:

//@version=4
study("Percent change from daily", precision=2)
a = input(title="Symbol", type=input.symbol, defval="ftx:btcperp")
b = input(title="Resolution", type=input.resolution, defval="D")

f_security(_sym, _res, _src, _rep) => security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]
// Non-repainting HTF value.
c = f_security(a, b, close, false)
d = (close - c) / c * 100
plot(d)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21