Aggregations#
This section contains reference for the available aggregation functions.
However here listed some basic examples of how you can specify bucket intervals for aggregations.
Find average for selected ticks for the 5 second interval with sliding window:
data = otp.Ticks(
X=[10, 9, 14, 14, 8, 11],
offset=[0, 1000, 2000, 3000, 4000, 5000],
)
data = data.agg({'RESULT': otp.agg.average('X')}, running=True, bucket_interval=otp.Second(5))
otp.run(data)
Time RESULT
0 2003-12-01 00:00:00 10.00
1 2003-12-01 00:00:01 9.50
2 2003-12-01 00:00:02 11.00
3 2003-12-01 00:00:03 11.75
4 2003-12-01 00:00:04 11.00
5 2003-12-01 00:00:05 11.20
6 2003-12-01 00:00:06 11.75
7 2003-12-01 00:00:07 11.00
8 2003-12-01 00:00:08 9.50
9 2003-12-01 00:00:09 11.00
10 2003-12-01 00:00:10 NaN
Find total volume of trades, minimal and maximum price for a day for a symbol AAA
:
data = otp.DataSource(db='DEMO_L1', tick_type='TRD', symbol='AAA')
data = data.agg({
'SUM': otp.agg.sum('SIZE'),
'MIN': otp.agg.min('PRICE'),
'MAX': otp.agg.max('PRICE'),
}, bucket_interval=otp.Day(1))
otp.run(data)
Time SUM MIN MAX
0 2003-12-02 1600 59.72 60.24
Find an average in buckets of 5 ticks:
data = otp.Ticks(X=[21, 20, 22, 25, 18, 17, 19, 23, 21, 21, 16, 20, 15])
data = data.agg({'AVG': otp.agg.average('X')}, bucket_interval=5, bucket_units='ticks')
otp.run(data)
Time AVG
0 2003-12-01 00:00:00.004 21.2
1 2003-12-01 00:00:00.009 20.2
2 2003-12-04 00:00:00.000 17.0