diff --git a/links/24h_volume.pine.link b/links/24h_volume.pine.link index 8202ca371..8d0c15a6e 100644 --- a/links/24h_volume.pine.link +++ b/links/24h_volume.pine.link @@ -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