Skip to content

Commit

Permalink
HBS-4111: screener: change 24h_vol calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
arylkou committed Feb 21, 2024
1 parent b57b2b0 commit cea0458
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions links/24h_volume.pine.link
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
// 24h volume
price = close
currency = "USD"
msIn24h = 24*60*60*1000
countOfFiveMinsInDay = 24*60/5
maxBufferSize = 2*countOfFiveMinsInDay
cumVolTF = "5"
cum24hVol(s) =>
src = s
if bar_index==0
src := src[maxBufferSize] * time[maxBufferSize] * 0
var cumSum = 0.
var int firstBarTimeIndex = na
if na(firstBarTimeIndex) // 24 H have not elapsed yet
sum = 0.
for i = 0 to countOfFiveMinsInDay
if (time - time[i]) >= msIn24h
firstBarTimeIndex := bar_index - i + 1
break
sum += src[i]
cumSum := sum
else
cumSum += nz(src)
for i = firstBarTimeIndex to bar_index
if (time - time[bar_index - i]) < msIn24h
firstBarTimeIndex := i
break
cumSum -= nz(src[bar_index - i])
if cumSum <= 0
cumSum := 0
cumSum
expr = syminfo.volumetype == "quote" ? volume : ( syminfo.volumetype == "base" ? price*volume : na )
vol24h = request.security(syminfo.tickerid, cumVolTF, cum24hVol(expr), lookahead = barmerge.lookahead_off, currency = currency, ignore_invalid_symbol=true)
sumVolTF = "5"
// rollOnTimeWhen is internal function from PineCoders/getSeries/1 library
rollOnTimeWhen(series float src, simple int timeWindow, series bool cond = true, simple int minBars = 1) =>
var float[] sources = array.new_float(0)
var int[] times = array.new_int(0)
if cond
array.push(sources, src)
array.push(times, time)
if array.size(sources) > 0
while time - array.get(times, 0) >= timeWindow and array.size(sources) > minBars
array.shift(sources)
array.shift(times)
float[] result = sources
sum24hVol(src) =>
sourceValues = rollOnTimeWhen(src, msIn24h)
sourceValues.sum()
var cumVol = 0.
cumVol += nz(volume)
expr = syminfo.volumetype == "quote" ? volume : close * volume
vol24h = request.security(syminfo.tickerid, sumVolTF, sum24hVol(expr * request.currency_rate(syminfo.currency, currency)))
if barstate.islast
if syminfo.volumetype == "tick" and syminfo.type == "crypto" or cumVol == 0
vol24h := na
plot(vol24h, title = "24h_vol", style = plot.style_columns)

// volume in base and quote currencies
Expand Down

0 comments on commit cea0458

Please sign in to comment.