Timescale Cloud:性能、规模、企业级
自托管产品
MST
要查看 OHLCV 值,最有效的方法是创建连续聚合。您可以创建一个连续聚合以每小时聚合数据,然后将该聚合设置为每小时刷新一次,并聚合过去两小时的数据。
连接到包含 Twelve Data 股票数据集的 Timescale 数据库 `tsdb`。
在 psql 提示符下,创建连续聚合以每分钟聚合数据
CREATE MATERIALIZED VIEW one_hour_candleWITH (timescaledb.continuous) ASSELECTtime_bucket('1 hour', time) AS bucket,symbol,FIRST(price, time) AS "open",MAX(price) AS high,MIN(price) AS low,LAST(price, time) AS "close",LAST(day_volume, time) AS day_volumeFROM crypto_ticksGROUP BY bucket, symbol;创建连续聚合后,它默认会刷新。
设置刷新策略,以便在超表中最近两小时有新数据可用时,每小时更新连续聚合。
SELECT add_continuous_aggregate_policy('one_hour_candle',start_offset => INTERVAL '3 hours',end_offset => INTERVAL '1 hour',schedule_interval => INTERVAL '1 hour');
设置好连续聚合后,您可以查询它以获取 OHLCV 值。
连接到包含 Twelve Data 股票数据集的 Timescale 数据库。
在 psql 提示符下,使用此查询按时间桶选择过去 5 小时内所有 `AAPL` 的 OHLCV 数据
SELECT * FROM one_hour_candleWHERE symbol = 'AAPL' AND bucket >= NOW() - INTERVAL '5 hours'ORDER BY bucket;查询结果如下所示
bucket | symbol | open | high | low | close | day_volume------------------------+---------+---------+---------+---------+---------+------------2023-05-30 08:00:00+00 | AAPL | 176.31 | 176.31 | 176 | 176.01 |2023-05-30 08:01:00+00 | AAPL | 176.27 | 176.27 | 176.02 | 176.2 |2023-05-30 08:06:00+00 | AAPL | 176.03 | 176.04 | 175.95 | 176 |2023-05-30 08:07:00+00 | AAPL | 175.95 | 176 | 175.82 | 175.91 |2023-05-30 08:08:00+00 | AAPL | 175.92 | 176.02 | 175.8 | 176.02 |2023-05-30 08:09:00+00 | AAPL | 176.02 | 176.02 | 175.9 | 175.98 |2023-05-30 08:10:00+00 | AAPL | 175.98 | 175.98 | 175.94 | 175.94 |2023-05-30 08:11:00+00 | AAPL | 175.94 | 175.94 | 175.91 | 175.91 |2023-05-30 08:12:00+00 | AAPL | 175.9 | 175.94 | 175.9 | 175.94 |
提取原始 OHLCV 数据后,您可以使用 Grafana 在 K 线图(candlestick chart)中绘制结果。为此,您需要将 Grafana 设置为连接到您的 TimescaleDB 数据库。
确保已安装 Grafana,并且您正在使用包含 Twelve Data 数据集的 TimescaleDB 数据库作为数据源。
在 Grafana 中,从 `Dashboards` 菜单点击 `New Dashboard`。在 `New Dashboard` 页面中,点击 `Add a new panel`。
在右上角的 `Visualizations` 菜单中,从列表中选择 `Candlestick`。确保您已将 Twelve Data 数据集设置为数据源。
点击 `Edit SQL` 并粘贴您用于获取 OHLCV 值的查询。
在 `Format as` 部分,选择 `Table`。
根据需要调整图表元素,然后点击 `Apply` 将图表保存到仪表板。
关键词