https://www.youtube.com/watch?v=2utibYV3oxA
https://www.youtube.com/watch?v=BrYe-4QWjdc
select * from stock where symbol = 'TWTR';
select * from stock_price where stock_id = 15117; select * from stock_price where stock_id = 15117 and date(dt) = '2021-01-25';
delete from stock_price where dt::timestamp::time < '06:30:00' or dt::timestamp::time >= '13:00:00';
select max(high) from stock_price where stock_id = 15117;
select min(low) from stock_price where stock_id = 15117;
select first(open, dt) from stock_price where stock_id = 15117 and date(dt) = '2021-01-26';
select last(close, dt) from stock_price where stock_id = 15117;
select stock_id, symbol, sum(volume) as total_volume from stock_price join stock on stock.id = stock_price.stock_id where date(dt) = '2021-01-29' group by stock_id, symbol order by total_volume asc LIMIT 10;
SELECT histogram(close, 50, 52, 4) FROM stock_price WHERE stock_id = 15117;
histogram
{8481,428,272,439,256,2138}
select count() from stock_price where stock_id = 15117 and close < 50; select count() from stock_price where close >= 50 and close < 50.5 and stock_id = 15117; select count(*) from stock_price where stock_id = 15117 and close > 52;
select time_bucket(INTERVAL '1 hour', dt) AS bucket, first(open, dt), max(high), min(low), last(close, dt) from stock_price where stock_id = 15117 group by bucket order by bucket desc;
select time_bucket(INTERVAL '20 minute', dt) AS bucket, first(open, dt), max(high), min(low), last(close, dt) from stock_price where stock_id = 15117 group by bucket order by bucket desc;
SELECT time_bucket_gapfill('5 min', dt, now() - INTERVAL '5 day', now()) AS bar, avg(close) as close FROM stock_price WHERE stock_id = 7502 and dt > now () - INTERVAL '5 day' group by bar, stock_id order by bar;
SELECT time_bucket_gapfill('5 min', dt, now() - INTERVAL '5 day', now()) AS bar, locf(avg(close)) as close2 FROM stock_price WHERE stock_id = 7502 and dt > now () - INTERVAL '5 day' group by bar, stock_id order by bar;
CREATE MATERIALIZED VIEW hourly_bars WITH (timescaledb.continuous) AS SELECT stock_id, time_bucket(INTERVAL '1 hour', dt) AS day, first(open, dt) as open, MAX(high) as high, MIN(low) as low, last(close, dt) as close, SUM(volume) as volume FROM stock_price GROUP BY stock_id, day;
CREATE MATERIALIZED VIEW daily_bars WITH (timescaledb.continuous) AS SELECT stock_id, time_bucket(INTERVAL '1 day', dt) AS day, first(open, dt) as open, MAX(high) as high, MIN(low) as low, last(close, dt) as close, SUM(volume) as volume FROM stock_price GROUP BY stock_id, day;
SELECT * FROM hourly_bars WHERE stock_id = 15117 ORDER BY hour desc;
SELECT * FROM daily_bars WHERE day > (now() - interval '21 days') AND stock_id = 15117 ORDER BY day;
SELECT avg(close) FROM ( SELECT * FROM daily_bars WHERE stock_id = 15117 ORDER BY day DESC LIMIT 20 ) a;
Window Function - performs calculation across a set of table rows that are related to the current row
Moving average with Window Functions
SELECT day, AVG(close) OVER (ORDER BY day ROWS BETWEEN 19 PRECEDING AND CURRENT ROW) AS sma_20 FROM daily_bars WHERE stock_id = 15117 ORDER BY day DESC;
WITH prev_day_closing AS ( SELECT stock_id, day, close, LEAD(close) OVER (PARTITION BY stock_id ORDER BY day DESC) AS prev_day_closing_price FROM daily_bars ), daily_factor AS ( SELECT stock_id, day, close / prev_day_closing_price AS daily_factor FROM prev_day_closing ) SELECT day, LAST(stock_id, daily_factor) AS stock_id, MAX(daily_factor) AS max_daily_factor FROM daily_factor JOIN stock ON stock.id = daily_factor.stock_id GROUP BY day ORDER BY day DESC, max_daily_factor DESC;
SELECT stock_id, symbol, day, close, LAG (close,1) OVER (ORDER BY close ASC) AS previous_close FROM daily_bars JOIN stock ON stock.id = daily_bars.stock_id WHERE date(day) = '2021-02-05' ORDER BY stock_id, day;
SELECT * FROM ( SELECT stock_id, day, close, LEAD (close,1) OVER (PARTITION BY stock_id ORDER BY day DESC) AS previous_close FROM daily_bars ) a WHERE close < previous_close AND date(day) = '2021-02-05';
SELECT * FROM ( SELECT stock_id, dt, close, LEAD (close,1) OVER (PARTITION BY stock_id ORDER BY dt DESC) AS previous_close FROM stock_price ) a WHERE close < previous_close AND dt = '2021-02-05 12:55:00';
SELECT * FROM (
SELECT
day, open, close, stock_id,
LAG(close, 1) OVER (
PARTITION BY stock_id
ORDER BY day
) previous_close,
LAG(open, 1) OVER (
PARTITION BY stock_id
ORDER BY day
) previous_open
FROM
daily_bars
) a
WHERE previous_close < previous_open
AND close > previous_open AND open < previous_close
AND day = '2021-02-05';
CALL refresh_continuous_aggregate('hourly_bars', '2020-10-01', '2021-03-01'); CALL refresh_continuous_aggregate('daily_bars', '2020-10-01', '2021-03-01');
SELECT * FROM ( SELECT day, close, volume, stock_id, LAG(close, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_close, LAG(volume, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_volume, LAG(close, 2) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_close, LAG(volume, 2) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_volume FROM daily_bars ) a WHERE close > previous_close AND previous_close > previous_previous_close AND volume > previous_volume AND previous_volume > previous_previous_volume AND day = '2021-01-22';
SELECT * FROM ( SELECT day, close, volume, stock_id, LAG(close, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_close, LAG(volume, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_volume, LAG(close, 2) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_close, LAG(volume, 2) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_volume, LAG(close, 3) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_previous_close, LAG(volume, 3) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_previous_volume FROM daily_bars ) a WHERE close > previous_close AND previous_close > previous_previous_close AND previous_previous_close > previous_previous_previous_close AND volume > previous_volume AND previous_volume > previous_previous_volume AND previous_previous_volume > previous_previous_previous_volume AND day = '2021-02-05';
SELECT * FROM ( SELECT day, close, volume, stock_id, LAG(close, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_close, LAG(volume, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_volume, LAG(close, 2) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_close, LAG(volume, 2) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_volume, LAG(close, 3) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_previous_close, LAG(volume, 3) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_previous_volume FROM daily_bars ) a WHERE close > previous_previous_previous_close and previous_close < previous_previous_close and previous_close < previous_previous_previous_close AND volume > previous_volume and previous_volume < previous_previous_volume and previous_previous_volume < previous_previous_previous_volume AND day = '2021-02-05';