Skip to content

Instantly share code, notes, and snippets.

@jonatas
Created October 27, 2021 20:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonatas/1f0eeda3ab29a1d34a325112901f78b2 to your computer and use it in GitHub Desktop.
Save jonatas/1f0eeda3ab29a1d34a325112901f78b2 to your computer and use it in GitHub Desktop.
set search_path to public, toolkit_experimental, timescaledb_experimental ;
DROP TABLE if exists ticks CASCADE;
CREATE TABLE ticks ( time TIMESTAMP NOT NULL, symbol varchar, price decimal, volume int);
SELECT create_hypertable('ticks', 'time');
CREATE MATERIALIZED VIEW tv_1m
WITH (timescaledb.continuous) AS
SELECT time_bucket('1 minute', time) as bucket,
symbol,
timevector(time AT TIME ZONE 'GMT', price) as tv_price,
timevector(time AT TIME ZONE 'GMT', volume) as tv_volume
FROM ticks
GROUP BY 1, 2
WITH DATA;
SELECT add_continuous_aggregate_policy('tv_1m', start_offset => INTERVAL '1 month', end_offset => INTERVAL '1 minute', schedule_interval => INTERVAL '1 minute');
\timing on
INSERT INTO ticks
SELECT time, 'SYMBOL', (random()*30)::int, 100*(random()*10)::int
FROM generate_series(TIMESTAMP '2000-01-01 00:00:00',
TIMESTAMP '2000-01-01 23:59:59',
INTERVAL '1 second') AS time;
CREATE OR REPLACE FUNCTION volatility()
RETURNS pipelinethensum IMMUTABLE PARALLEL SAFE LANGUAGE SQL AS $$
SELECT ( sort() -> delta() -> abs() -> sum()) ;
$$;
SELECT time_bucket('15 min', bucket),
sum(tv_price -> volatility()) as price_volatility,
sum(tv_volume -> sum()) as traded_volume
FROM tv_1m group by 1 order by 1;
@jonatas
Copy link
Author

jonatas commented Oct 27, 2021

Output:

┌─────────────────────┬──────────────────┬───────────────┐
│     time_bucket     │ price_volatility │ traded_volume │
├─────────────────────┼──────────────────┼───────────────┤
│ 2000-01-01 00:00:00 │             8629 │        448100 │
│ 2000-01-01 00:15:00 │             8666 │        442200 │
│ 2000-01-01 00:30:00 │             8206 │        445700 │
│ 2000-01-01 00:45:00 │             8400 │        455900 │
│ 2000-01-01 01:00:00 │             8738 │        444400 │
│ 2000-01-01 01:15:00 │             8715 │        439900 │
│ 2000-01-01 01:30:00 │             8786 │        457800 │
│ 2000-01-01 01:45:00 │             8910 │        462500 │
│ 2000-01-01 02:00:00 │             8962 │        456700 │
│ 2000-01-01 02:15:00 │             8935 │        448500 │
│ 2000-01-01 02:30:00 │             9204 │        440900 │
│ 2000-01-01 02:45:00 │             9208 │        449300 │
│ 2000-01-01 03:00:00 │             8901 │        446600 │
│ 2000-01-01 03:15:00 │             8707 │        449300 │
│ 2000-01-01 03:30:00 │             8736 │        438900 │
│ 2000-01-01 03:45:00 │             9039 │        442400 │
│ 2000-01-01 04:00:00 │             9234 │        449100 │
│ 2000-01-01 04:15:00 │             8604 │        459000 │
│ 2000-01-01 04:30:00 │             9097 │        434600 │
│ 2000-01-01 04:45:00 │             8736 │        430300 │
│ 2000-01-01 05:00:00 │             8928 │        443700 │
│ 2000-01-01 05:15:00 │             8416 │        469800 │
│ 2000-01-01 05:30:00 │             9152 │        451100 │
│ 2000-01-01 05:45:00 │             9143 │        430800 │
│ 2000-01-01 06:00:00 │             8359 │        444800 │
│ 2000-01-01 06:15:00 │             8746 │        444600 │
│ 2000-01-01 06:30:00 │             8611 │        461000 │
│ 2000-01-01 06:45:00 │             8862 │        447000 │
│ 2000-01-01 07:00:00 │             8755 │        450100 │
│ 2000-01-01 07:15:00 │             8392 │        449400 │
│ 2000-01-01 07:30:00 │             8721 │        458100 │
│ 2000-01-01 07:45:00 │             9154 │        455500 │
│ 2000-01-01 08:00:00 │             8867 │        458000 │
│ 2000-01-01 08:15:00 │             8659 │        446900 │
│ 2000-01-01 08:30:00 │             9008 │        456300 │
│ 2000-01-01 08:45:00 │             8820 │        450800 │
│ 2000-01-01 09:00:00 │             8890 │        442400 │
│ 2000-01-01 09:15:00 │             9171 │        463900 │
│ 2000-01-01 09:30:00 │             9174 │        450800 │
│ 2000-01-01 09:45:00 │             9067 │        451700 │
│ 2000-01-01 10:00:00 │             8698 │        450500 │
│ 2000-01-01 10:15:00 │             8844 │        454400 │
│ 2000-01-01 10:30:00 │             8545 │        453300 │
│ 2000-01-01 10:45:00 │             9070 │        456100 │
│ 2000-01-01 11:00:00 │             9190 │        435900 │
│ 2000-01-01 11:15:00 │             8977 │        446200 │
│ 2000-01-01 11:30:00 │             8729 │        451500 │
│ 2000-01-01 11:45:00 │             8641 │        450900 │
│ 2000-01-01 12:00:00 │             8579 │        454400 │
│ 2000-01-01 12:15:00 │             8897 │        441600 │
│ 2000-01-01 12:30:00 │             9247 │        454800 │
│ 2000-01-01 12:45:00 │             8909 │        465300 │
│ 2000-01-01 13:00:00 │             8524 │        448200 │
│ 2000-01-01 13:15:00 │             9081 │        438100 │
│ 2000-01-01 13:30:00 │             8861 │        446800 │
│ 2000-01-01 13:45:00 │             9093 │        440500 │
│ 2000-01-01 14:00:00 │             9158 │        454500 │
│ 2000-01-01 14:15:00 │             9172 │        443600 │
│ 2000-01-01 14:30:00 │             9302 │        449500 │
│ 2000-01-01 14:45:00 │             8560 │        441900 │
│ 2000-01-01 15:00:00 │             8798 │        432300 │
│ 2000-01-01 15:15:00 │             8998 │        454200 │
│ 2000-01-01 15:30:00 │             8957 │        456400 │
│ 2000-01-01 15:45:00 │             8688 │        458800 │
│ 2000-01-01 16:00:00 │             9050 │        448500 │
│ 2000-01-01 16:15:00 │             8878 │        445700 │
│ 2000-01-01 16:30:00 │             8739 │        451300 │
│ 2000-01-01 16:45:00 │             9011 │        464500 │
│ 2000-01-01 17:00:00 │             8724 │        453400 │
│ 2000-01-01 17:15:00 │             9091 │        447600 │
│ 2000-01-01 17:30:00 │             9004 │        443600 │
│ 2000-01-01 17:45:00 │             8632 │        441500 │
│ 2000-01-01 18:00:00 │             9036 │        449100 │
│ 2000-01-01 18:15:00 │             8744 │        459000 │
│ 2000-01-01 18:30:00 │             8448 │        444900 │
│ 2000-01-01 18:45:00 │             8970 │        459200 │
│ 2000-01-01 19:00:00 │             8697 │        432300 │
│ 2000-01-01 19:15:00 │             9079 │        456500 │
│ 2000-01-01 19:30:00 │             9162 │        464700 │
│ 2000-01-01 19:45:00 │             9234 │        456100 │
│ 2000-01-01 20:00:00 │             8620 │        464100 │
│ 2000-01-01 20:15:00 │             8779 │        447900 │
│ 2000-01-01 20:30:00 │             9133 │        454100 │
│ 2000-01-01 20:45:00 │             9121 │        446900 │
│ 2000-01-01 21:00:00 │             8754 │        450900 │
│ 2000-01-01 21:15:00 │             8607 │        440000 │
│ 2000-01-01 21:30:00 │             9063 │        442100 │
│ 2000-01-01 21:45:00 │             8925 │        460800 │
│ 2000-01-01 22:00:00 │             8756 │        448700 │
│ 2000-01-01 22:15:00 │             8614 │        450500 │
│ 2000-01-01 22:30:00 │             9071 │        456400 │
│ 2000-01-01 22:45:00 │             8567 │        457100 │
│ 2000-01-01 23:00:00 │             8994 │        447600 │
│ 2000-01-01 23:15:00 │             8955 │        462000 │
│ 2000-01-01 23:30:00 │             9105 │        458400 │
│ 2000-01-01 23:45:00 │             8889 │        449400 │
└─────────────────────┴──────────────────┴───────────────┘
(96 rows)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment