Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBS-4111: screener: change 24h_vol calculation #464

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading