database - How can I query last price before 15:30 in QuestDB for every day last month? - Stack Overflow

I've been trying to get near closing prices in NYSE for the stock prices every day for the last mo

I've been trying to get near closing prices in NYSE for the stock prices every day for the last month. The trading hours are before 16:00 EST and I want to get prices at 15:30. My simplified schema is

CREATE TABLE "trades" ( 
    symbol SYMBOL,
    side SYMBOL,
    price DOUBLE,
    amount DOUBLE,
    timestamp TIMESTAMP
) timestamp(timestamp) PARTITION BY

Is there a way to query the last price before 15:30 every day?

I've been trying to get near closing prices in NYSE for the stock prices every day for the last month. The trading hours are before 16:00 EST and I want to get prices at 15:30. My simplified schema is

CREATE TABLE "trades" ( 
    symbol SYMBOL,
    side SYMBOL,
    price DOUBLE,
    amount DOUBLE,
    timestamp TIMESTAMP
) timestamp(timestamp) PARTITION BY

Is there a way to query the last price before 15:30 every day?

Share Improve this question edited Mar 25 at 11:37 Javier Ramirez 4,0851 gold badge27 silver badges36 bronze badges asked Mar 24 at 18:20 Doncarleone512Doncarleone512 4022 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can probably do this to get the latest price per symbol before 15:30 EST every day for the last month. I convert a random date (2025-01-01) with the desired timestamp 15:30:00 from EST to UTC, as QuestDB stores timestamps at UTC. Then I extract the HH:mm:ss component from both this date and the table timestamp column, to get all records below that timestamp. I group by date_trunc('day', timestamp), so I will get a single result per day, then I get the last timestamp and price for each day.

Note for the group by I am not converting to EST first. I am assuming you have frequent entries for each symbol, so there should be data for each of them in the few hours before 15:30 EST always. In this case it is safe to ignore the timezone offset between UTC and EST for teh group by. If some symbols had infrequent values, it might be a good idea to add the timezone conversion to the group by, at the expense of performance.

select  symbol, to_timezone(last(timestamp), 'EST'), last(price) 
from trades
where timestamp > dateadd('M', -1, now()) 
and 
to_str(timestamp, 'HH:mm:ss') < 
to_str(to_utc('2025-01-01T15:30:00', 'EST'), 'HH:mm:ss')
group by date_trunc('day', timestamp), symbol;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744234421a4564406.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信