volumeBar
Syntax
volumeBar(X, interval, [label='seq'])
Details
This function sequentially accumulates the elements in X, and then groups them based on the specified threshold. Once a group is determined, the accumulation starts from the next element and data grouping is performed in the same logic. It returns a vector of the same size as X containing the corresponding group number for each element.
Elements are divided into groups based on the threshold specified by interval.
-
If interval is positive, elements are labeled into groups when the cumulative sum is no smaller than the threshold;
-
If interval is in (0, 1), the threshold is sum(X) * interval. Note that the threshold is converted to the same data type as X for comparison. For example, if X is an integer, then the threshold will be set at floor(sum(X) * interval).
-
Otherwise, the threshold takes the value of interval.
-
-
If interval is negative, the threshold takes the value of interval. Elements are labeled into groups when the cumulative sum is no greater than the threshold.
Parameters
X is a numeric vector.
interval is a non-zero number that represents a constant value or percentage that determines the threshold for data grouping.
label (optional) is a string used to label the groups. It can be:
-
'seq'(default): label the groups with a sequence of 0, 1, 2, 3...
-
'left': label each group with the sum of all elements before the first element in the group. The first group is labeled with 0.
-
'right': label each group with the sum of all elements up to and including the last element in the group.
Returns
A LONG/INT vector.
Examples
X = 1 3 4 2 2 1 1 1 1 6 8
volumeBar(X, 4)
// output: [0,0,1,2,2,3,3,3,3,4,5]
volumeBar(X, 4, 'left')
// output: [0,0,4,8,8,12,12,12,12,16,22]
volumeBar(X, 4, 'right')
// output: [4,4,8,12,12,16,16,16,16,22,30]
volumeBar(X, 0.3)
// output: [0,0,0,0,1,1,1,1,1,1,2]
X = -6 2 -4 -5 -1 3 -2 -1
volumeBar(X, -2)
// output: [0,1,1,2,3,3,3,3]
volumeBar(X, -2, 'left')
// output: [0,-6,-6,-8,-13,-13,-13,-13]
volumeBar(X, -2, 'right')
// output: [-6,-8,-8,-13,-14,-14,-14,-14]
time = [09:30:00, 09:32:15, 09:35:00, 09:37:30, 09:40:00, 09:45:00, 09:47:30, 09:50:00, 09:55:00, 10:00:00]
price = [100.0, 100.1, 100.05, 99.9, 100.2, 100.15, 100.1, 100.05, 100.0, 99.95]
volume = [200, 800, 250, 300, 1200, 250, 180, 400, 600, 350]
t = table(time, price, volume)
select
first(time) as barStartTime,
first(price) as open,
max(price) as high,
min(price) as low,
last(price) as close,
sum(volume) as totalVolume
from t
group by volumeBar(tradeVolume, 1500) as volumeBarNo
|
volumeBarNo |
barStartTime |
open |
high |
low |
close |
totalVolume |
|---|---|---|---|---|---|---|
| 0 | 09:30:00 | 100 | 100.1 | 99.9 | 99.9 | 1,550 |
| 1 | 09:40:00 | 100.2 | 100.2 | 100.1 | 100.1 | 1,630 |
| 2 | 09:50:00 | 100.05 | 100.05 | 99.95 | 99.95 | 1,350 |
