Taos数据库计算k线数据

一、计算1分钟K线数据

1、因成交量是个累计值 ,计算1分钟的成交量,需要每一分钟减去上一分钟才能计算出来,diff函数计算成交量,会将第一分钟的数据去掉,因此要单独拿出来第一分钟 与其他结果合并

2、后期将根据是否有无夜盘,进行每天第一分钟的计算,
商品期货中的无夜盘的第一分钟是
ts>=“2024-06-17 09:00:00.000” and ts<“2024-06-17 09:01:00.000”
金融期货 第一分钟是
ts>=“2024-06-17 09:30:00.000” and ts<“2024-06-17 09:31:00.000”
有夜盘的第一分钟是
ts>=“2024-06-17 21:00:00.000” and ts<“2024-06-17 21:01:00.000”

3、为避免干扰,需要将一些时间段数据清除掉,比如:
(商品期货)日盘:10.15 11.30 15.00以后
(金融期货): 11.30 15.00以后 几个债卷的再查一下收盘时间

select ts,open,high,low,close ,volume ,openinterest from 
	(
		SELECT _wstart as ts ,first(lastprice) as open,max(lastprice) as high,min(lastprice) as low, last(lastprice) as close   , last(volume)  as volume, last(openinterest)  as openinterest  
		FROM quote.lh2409 
		WHERE  ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 09:01:00.000"     
		INTERVAL (1m) 
		 
		union  all
		
		select ts,open,high,low,close ,DIFF(volume) as volume,openinterest from 
		(
			SELECT _wstart as ts ,first(lastprice) as open,max(lastprice) as high,min(lastprice) as low, last(lastprice) as close, last(volume)  as volume , last(openinterest)  as openinterest  
			FROM quote.lh2409 
			WHERE 
			   --tradingday="20240617"  and 
			   ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 15:00:00.000" and 
			   TO_CHAR(ts, 'hh24MI')<>'1015' and TO_CHAR(ts, 'hh24MI')<>'1130'  
			INTERVAL (1m) 
	    )
	)

结果如下图
在这里插入图片描述

二、计算N分钟K线数据

因交易时间的不连续性,所以计算N分钟K线数据,在1分钟K线数据的基础上进行合成

select _wstart,  first(open) as open,max(high) as high,min(low) as low, last(close) as close ,sum(volume) as volume, last(openinterest)  as openinterest 
from  
(
    --  1分钟K线计算代码=====开始=============
	select ts,open,high,low,close ,volume ,openinterest from 
	(
		SELECT _wstart as ts ,first(lastprice) as open,max(lastprice) as high,min(lastprice) as low, last(lastprice) as close   , last(volume)  as volume, last(openinterest)  as openinterest  
		FROM quote.lh2409 
		WHERE  ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 09:01:00.000"     
		INTERVAL (1m) 
		 
		union  all
		
		select ts,open,high,low,close ,DIFF(volume) as volume,openinterest from 
		(
			SELECT _wstart as ts ,first(lastprice) as open,max(lastprice) as high,min(lastprice) as low, last(lastprice) as close, last(volume)  as volume , last(openinterest)  as openinterest  
			FROM quote.lh2409 
			WHERE 
			   --tradingday="20240617"  and 
			   ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 15:00:00.000" and 
			   TO_CHAR(ts, 'hh24MI')<>'1015' and TO_CHAR(ts, 'hh24MI')<>'1130'  
			INTERVAL (1m) 
	    )
	)
    --  1分钟K线计算代码=====结束=============
)  
count_window(5);

结果如下图:
在这里插入图片描述

三、计算MA均线

只计算简单平均,ma均线利用的是每个周期的close,所以将上面的脚本中的只要close就可以

1 计算1分钟收盘价

SELECT _wstart as ts , last(lastprice) as close 
FROM quote.lh2409 
WHERE 
   ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 15:00:00.000" and 
   TO_CHAR(ts, 'hh24MI')<>'1015' and TO_CHAR(ts, 'hh24MI')<>'1130'  
INTERVAL (1m) 

如下图
在这里插入图片描述

2 计算N分钟收盘价

select _wstart as ts,    last(close) as close  
from  
(
	SELECT _wstart as ts , last(lastprice) as close 
	FROM quote.lh2409 
	WHERE 
	   ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 15:00:00.000" and 
	   TO_CHAR(ts, 'hh24MI')<>'1015' and TO_CHAR(ts, 'hh24MI')<>'1130'  
	INTERVAL (1m) 
)  
count_window(5)

如下图
在这里插入图片描述

3、计算ma均线

select ts,close, mavg(close,3) 
from 
(	
	select _wstart as ts,    last(close) as close  
	from  
	(
		SELECT _wstart as ts , last(lastprice) as close 
		FROM quote.lh2409 
		WHERE 
		   ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 15:00:00.000" and 
		   TO_CHAR(ts, 'hh24MI')<>'1015' and TO_CHAR(ts, 'hh24MI')<>'1130'  
		INTERVAL (1m) 
	)  
	count_window(5)
)  ;

注意:如果计算1分钟k线的ma均线,不能使用计数窗口函数。

例如:

  1. 在1分钟K线基础上,计算ma(3)
select ts,close, mavg(close,3) 
from 
(		 
	SELECT _wstart as ts , last(lastprice) as close 
	FROM quote.lh2409 
	WHERE 
	   ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 15:00:00.000" and 
	   TO_CHAR(ts, 'hh24MI')<>'1015' and TO_CHAR(ts, 'hh24MI')<>'1130'  
	INTERVAL (1m)  
)  ;
  1. 在3分钟K线基础上,计算ma(5)
select ts,close, mavg(close,5) 
from 
(	
	select _wstart as ts,    last(close) as close  
	from  
	(
		SELECT _wstart as ts , last(lastprice) as close 
		FROM quote.lh2409 
		WHERE 
		   ts>="2024-07-16 09:00:00.000" and ts <"2024-07-16 15:00:00.000" and 
		   TO_CHAR(ts, 'hh24MI')<>'1015' and TO_CHAR(ts, 'hh24MI')<>'1130'  
		INTERVAL (1m) 
	)  
	count_window(3)
)  ;
  • 对TDengine 的这个数据库,还是不太熟悉,只能先凑合实现自己的想法,应当还有更好的脚本写法,后期学到后,再更新。
### 如何在 TAOS 数据库中查询最早的记录或数据TAOS 数据库(即 TDengine)中,可以通过标准 SQL 查询语句来获取最早的数据记录。具体方法是利用 `ORDER BY` 和 `LIMIT` 子句组合实现这一目标。 以下是具体的解决方案: #### 使用 SQL 查询语法 为了找到表中的最早记录,可以按照时间字段升序排列并限制返回的第一条记录。假设有一个名为 `your_table_name` 的表,并且该表的时间戳列命名为 `timestamp_column`,那么查询语句如下所示: ```sql SELECT * FROM your_table_name ORDER BY timestamp_column ASC LIMIT 1; ``` 这条语句的作用是从指定的表中读取所有列的内容,并按时间戳从小到大排序,最后通过 `LIMIT 1` 只提取第一条记录[^1]。 如果只需要特定几列而不是整个行,则可以在 SELECT 后面明确写出所需的列名代替星号 (`*`)。例如只关心 id 列和数值 data_col: ```sql SELECT id, data_col FROM your_table_name ORDER BY timestamp_column ASC LIMIT 1; ``` #### 考虑分区情况下的查询优化 当处理大规模分布式存储环境或者存在分片机制的情况下,可能需要额外注意性能调优问题。TDengine 支持基于超级表(Super Table)的概念来进行高效管理海量子表(Sub Tables),此时若要跨多个设备寻找全局最小值,可采用以下方式完成操作: ```sql SELECT _wstart AS earliest_time, first(data_col), device_id FROM super_table_name GROUP BY device_id ; ``` 这里 `_wstart` 表达的是窗口起始时刻;first() 函数用于捕获每个组内的首个非 NULL 值对应的其他属性信息。此命令会针对每一个独立设备计算其自身的最旧观测点[^3]。 ### 注意事项 - 确认所使用的日期/时间字段确实代表事件发生顺序。 - 如果有重复项并且希望排除它们,请考虑增加 DISTINCT 关键字或其他去重逻辑。 - 对于超大型表格而言,执行全表扫描可能会消耗较多资源,因此建议合理设计索引结构以及定期维护统计数据以便加速访问速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值