在QuestDB中计算累积Tick和Trin以进行市场情绪分析

问题描述 投票:0回答:1

我正在尝试计算tick和trin,如Databento的这个python脚本所示,但使用QuestDB的SQL。

下面的 sql 工作正常(创建表 SQL 和下面的演示行),但只给我总的 Tick 和 Trin。我希望获得当天每一行的累积值。

WITH tick_vol AS ( SELECT SUM(CASE WHEN side = 'sell' THEN 1 END) as downtick, SUM(CASE WHEN side = 'buy' THEN 1 END) as uptick, SUM(CASE WHEN side = 'sell' THEN amount END) as downvol, SUM(CASE WHEN side = 'buy' THEN amount END) as upvol FROM trades where timestamp in '2023-12-01' ) SELECT *, uptick::double / downtick as tick, (uptick::double / downtick) / (upvol::double / downvol) as trin FROM tick_vol;

query results: downtick 2, uptick 3, downvol 250, upvol 350, tick 1.5, trin 1.071428571428

CREATE TABLE 'trades' ( symbol SYMBOL capacity 256 CACHE, side SYMBOL capacity 256 CACHE, price DOUBLE, amount DOUBLE, timestamp TIMESTAMP ) timestamp (timestamp) PARTITION BY DAY WAL;
INSERT INTO trades (timestamp, side, amount)
VALUES 
('2023-12-01T10:00:00.000Z', 'sell', 100),
('2023-12-01T10:01:00.000Z', 'buy', 50),
('2023-12-01T10:02:00.000Z', 'sell', 150),
('2023-12-01T10:03:00.000Z', 'buy', 100),
('2023-12-01T10:04:00.000Z', 'buy', 200);
    
database time-series questdb
1个回答
0
投票
我们可以将已有的

SUM

 重新用作窗口函数,并且它可以工作。请注意,我已经将 downtick/uptick 设置为 1.0,因此它将被解释为 double,我们不再需要进行任何显式转换

WITH tick_vol AS ( SELECT timestamp, side, amount, SUM(CASE WHEN side = 'sell' THEN 1.0 END) OVER (ORDER BY timestamp) as downtick, SUM(CASE WHEN side = 'buy' THEN 1.0 END) OVER (ORDER BY timestamp) as uptick, SUM(CASE WHEN side = 'sell' THEN amount END) OVER (ORDER BY timestamp) as downvol, SUM(CASE WHEN side = 'buy' THEN amount END) OVER (ORDER BY timestamp) as upvol FROM trades where timestamp in '2023-12-01' ) SELECT *, uptick / downtick as tick, (uptick / downtick) / (upvol / downvol) as trin FROM tick_vol;
查询返回滚动刻度和 trin,并且正如预期的那样,最后一行与初始查询具有相同的结果

"timestamp","side","amount","downtick","uptick","downvol","upvol","tick","trin" "2023-12-01T10:00:00.000000Z","sell",100.0,1.0,,100.0,,, "2023-12-01T10:01:00.000000Z","buy",50.0,1.0,1.0,100.0,50.0,1.0,2.0 "2023-12-01T10:02:00.000000Z","sell",150.0,2.0,1.0,250.0,50.0,0.5,2.5 "2023-12-01T10:03:00.000000Z","buy",100.0,2.0,2.0,250.0,150.0,1.0,1.666666666666 "2023-12-01T10:04:00.000000Z","buy",200.0,2.0,3.0,250.0,350.0,1.5,1.071428571428
    
© www.soinside.com 2019 - 2024. All rights reserved.