How is Performance calculated in the Screener?

Screener Performance data is calculated using the formula:

``````//@version=4
study("Screener Performance")

fastSearchN(xs, x) => // xs - sorted, ascending
max_bars_back(xs, 366)
left  = 0
right = min(bar_index,366)
mid = 0
if xs < x
0
else
for i = 0 to 9
mid := ceil((left+right) / 2)
if left == right
break
else if xs[mid] < x
right := mid
continue
else if xs[mid] > x
left := mid
continue
else
break
mid

week1 = 7
week_ago = timenow - 1000*60*60*24*week1
countOfBarsWeekAgo = fastSearchN(time, week_ago)

month1 = 30
month_ago = timenow - 1000*60*60*24*month1
countOfBars1MonthAgo = fastSearchN(time, month_ago)

month3 = 90
months3_ago = timenow - 1000*60*60*24*month3
countOfBars3MonthAgo = fastSearchN(time, months3_ago)

month6 = 180
months6_ago = timenow - 1000*60*60*24*month6
countOfBars6MonthAgo = fastSearchN(time, months6_ago)

weeks52 = 7*52
weeks52_ago = timenow - 1000*60*60*24*weeks52
countOfBars52WeekAgo = fastSearchN(time, weeks52_ago)

// performance
rateOfreturn(v1, v2) => (v1 - v2) * 100 / abs(v2)
rr(bb) =>
if bb == 0
na
else
max_bars_back(close, 366)
rof = rateOfreturn(close, close[bb])
rof

plot(rr(countOfBarsWeekAgo), title="Perf.W")
plot(rr(countOfBars1MonthAgo), title="Perf.1M")
plot(rr(countOfBars3MonthAgo), title="Perf.3M")
plot(rr(countOfBars6MonthAgo), title="Perf.6M")
plot(rr(countOfBars52WeekAgo), title="Perf.Y")
var lastYearClose = float(na)
if year > year[1]
lastYearClose := close[1]
plot(rateOfreturn(close, lastYearClose), title="Perf.YTD")``````

Note: this script values are different on history and realtime because of timenow, see https://www.tradingview.com/pine-script-docs/en/v4/essential/Indicator_repainting.html

For visual displays, you can add this script to your chart through the Pine Editor using the chart's daily timeframe. An indicator will appear on the chart, the plots of which will show the values for each type of performance.