ohlc(open,high,low and close) 两个日期范围之间不同时间的记录内部(1,5,15,60,120)分钟,1 天即使没有记录

Posted

技术标签:

【中文标题】ohlc(open,high,low and close) 两个日期范围之间不同时间的记录内部(1,5,15,60,120)分钟,1 天即使没有记录【英文标题】:ohlc(open,high, low and close) record between two date range on different time internal(1,5,15,60,120) minute, 1 day even there are no record 【发布时间】:2017-09-16 07:00:40 【问题描述】:

我有什么

1.我的域“User_Order”

CREATE TABLE `user_order` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created_date` datetime DEFAULT NULL,
  `fees` decimal(19,2) NOT NULL,
  `instrument` varchar(255) NOT NULL,
  `is_limit_order` bit(1) NOT NULL,
  `market_id` bigint(20) NOT NULL,
  `order_id` varchar(255) NOT NULL,
  `order_status` varchar(255) NOT NULL,
  `order_type` varchar(255) NOT NULL,
  `price` decimal(20,8) NOT NULL,
  `quantity` decimal(20,8) NOT NULL,
  `remaining_quantity` decimal(20,8) NOT NULL,
  `updated_date` datetime DEFAULT NULL,
  `user_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UK_t5ah1x4wm9314qclf90dy0lyu` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2。我们的表中有以下记录。

| id | created_date        | fees   | instrument | is_limit_order | market_id | order_id         | order_status | order_type | price       | quantity   | remaining_quantity | updated_date        | user_id |
+----+---------------------+--------+------------+----------------+-----------+------------------+--------------+------------+-------------+------------+--------------------+---------------------+---------+
|  1 | 2017-09-15 05:08:57 | 0.0100 | INBTCINR   |               |         1 | 12:05:52-0000001 | EXECUTED     | BUY        | 10.00000000 | 2.00000000 |         0.00000000 | 2017-09-15 05:08:57 |       3 |
|  2 | 2017-09-15 05:09:34 | 0.0100 | INBTCINR   |               |         1 | 12:25:43-0000001 | EXECUTED     | SELL       | 10.00000000 | 2.00000000 |         0.00000000 | 2017-09-15 05:09:34 |       4 |
|  3 | 2017-09-15 05:11:18 | 0.0100 | INBTCINR   |               |         1 | 12:05:52-0000002 | CANCELLED    | BUY        |  2.00000000 | 1.00000000 |         1.00000000 | 2017-09-15 05:11:18 |       3 |
|  4 | 2017-09-15 05:12:43 | 0.0100 | INBTCINR   |               |         1 | 12:05:52-0000003 | EXECUTED     | BUY        |  4.00000000 | 2.00000000 |         0.00000000 | 2017-09-15 05:12:43 |       3 |
|  5 | 2017-09-15 05:23:10 | 0.0100 | INBTCINR   |               |         1 | 12:25:43-0000002 | EXECUTED     | SELL       |  4.00000000 | 2.00000000 |         0.00000000 | 2017-09-15 05:23:10 |       4 |
|  6 | 2017-09-15 08:34:17 | 0.0100 | INBTCINR   |               |         1 | 12:25:43-0000003 | OPEN         | BUY        |  2.00000000 | 1.00000000 |         1.00000000 | 2017-09-15 08:34:17 |       4 |
|  7 | 2017-09-15 08:36:32 | 0.0100 | INBTCINR   |               |         1 | 12:05:52-0000004 | CANCELLED    | SELL       |  1.00000000 | 2.00000000 |         2.00000000 | 2017-09-15 08:36:32 |       3 |
|  8 | 2017-09-15 09:24:22 | 0.0100 | INBTCINR   |               |         1 | 12:25:43-0000004 | OPEN         | BUY        |  2.00000000 | 1.00000000 |         1.00000000 | 2017-09-15 09:24:22 |       4 |
|  9 | 2017-09-15 09:30:19 | 0.0100 | INBTCINR   |               |         1 | 12:25:43-0000005 | OPEN         | BUY        |  1.00000000 | 2.00000000 |         2.00000000 | 2017-09-15 09:30:19 |       4 |
| 10 | 2017-09-15 09:32:10 | 0.0100 | INBTCINR   |               |         1 | 12:05:52-0000005 | CANCELLED    | SELL       |  1.00000000 | 2.00000000 |         2.00000000 | 2017-09-15 09:32:10 |       3 |
| 11 | 2017-09-15 10:02:57 | 0.0100 | INBTCINR   |               |         1 | 12:05:52-0000006 | OPEN         | SELL       |  2.00000000 | 4.00000000 |         4.00000000 | 2017-09-15 10:02:57 |       3 |
| 12 | 2017-09-15 10:16:19 | 0.0100 | INBTCINR   |               |         1 | 12:25:43-0000006 | OPEN         | BUY        |  4.00000000 | 2.00000000 |         2.00000000 | 2017-09-15 10:16:19 |       4 |

3.我正在为每 5(300 秒)分钟间隔记录写下面的查询。 开始日期 = '2017-09-11 00:00:00' 结束日期 = '2017-09-15 23:59:59'

select  FROM_UNIXTIME(floor(min(UNIX_TIMESTAMP(created_date))/300)*300)
                 as timestampDate,
        sum(quantity) as volume,sum(price*quantity)/sum(quantity) as wavg_price,
        substring_index(min(concat('created_date','_',price)),
                '_',-1) as open,
        max(price) as high,
        min(price) as low,
        substring_index(max(concat ('created_date','_',price)),
                '_',-1) as 'close'
    from  user_order
    where  created_date between '2017-09-11 00:00:00' AND '2017-09-15 23:59:59'
    group by  floor(unix_timestamp(created_date)/300)
    order by  created_date;

上述查询的结果:-

 timestampDate       | volume     | wavg_price              | open        | high        | low         | close       |
+---------------------+------------+-------------------------+-------------+-------------+-------------+-------------+
| 2017-09-15 05:05:00 | 4.00000000 | 10.00000000000000000000 | 10.00000000 | 10.00000000 | 10.00000000 | 10.00000000 |
| 2017-09-15 05:10:00 | 3.00000000 |  3.33333333333333333333 | 2.00000000  |  4.00000000 |  2.00000000 | 4.00000000  |
| 2017-09-15 05:20:00 | 2.00000000 |  4.00000000000000000000 | 4.00000000  |  4.00000000 |  4.00000000 | 4.00000000  |
| 2017-09-15 08:30:00 | 1.00000000 |  2.00000000000000000000 | 2.00000000  |  2.00000000 |  2.00000000 | 2.00000000  |
| 2017-09-15 08:35:00 | 2.00000000 |  1.00000000000000000000 | 1.00000000  |  1.00000000 |  1.00000000 | 1.00000000  |
| 2017-09-15 09:20:00 | 1.00000000 |  2.00000000000000000000 | 2.00000000  |  2.00000000 |  2.00000000 | 2.00000000  |
| 2017-09-15 09:30:00 | 4.00000000 |  1.00000000000000000000 | 1.00000000  |  1.00000000 |  1.00000000 | 1.00000000  |
| 2017-09-15 10:00:00 | 4.00000000 |  2.00000000000000000000 | 2.00000000  |  2.00000000 |  2.00000000 | 2.00000000  |
| 2017-09-15 10:15:00 | 2.00000000 |  4.00000000000000000000 | 4.00000000  |  4.00000000 |  4.00000000 | 4.00000000  |
+---------------------+------------+-------------------------+-------------+-------------+-------------+-------------+

4.即使我需要每个间隔的所有具有默认值或先前记录值(打开、关闭、高、低及其时间戳)的记录,上面的结果也只给出了表永久记录

预期输出:- 日期范围 = '2017-09-11 00:00:00' 和 '2017-09-15 23:59:59'

 timestampDate       | volume     | wavg_price              | open        | high        | low         | close       |
+---------------------+------------+-------------------------+-------------+-------------+-------------+-------------+
| 2017-09-11 00:05:00 | 4.00000000 | 10.00000000000000000000 | 10.00000000 | 10.00000000 | 10.00000000 | 10.00000000 |
| 2017-09-11 00:10:00 | 3.00000000 |  3.33333333333333333333 | 2.00000000  |  4.00000000 |  2.00000000 | 4.00000000  |
| 2017-09-11 00:15:00 | 2.00000000 |  4.00000000000000000000 | 4.00000000  |  4.00000000 |  4.00000000 | 4.00000000  |
................................................
.................................................
.................................................
| 2017-09-15 23:05:00 | 1.00000000 |  2.00000000000000000000 | 2.00000000  |  2.00000000 |  2.00000000 | 2.00000000  |
| 2017-09-15 23:10:00 | 1.00000000 |  2.00000000000000000000 | 2.00000000  |  2.00000000 |  2.00000000 | 2.00000000  |
..........................................................
..........................................................
..........................................................

| 2017-09-15 23:50:00 | 1.00000000 |  2.00000000000000000000 | 2.00000000  |  2.00000000 |  2.00000000 | 2.00000000  |
| 2017-09-15 23:55:00 | 1.00000000 |  2.00000000000000000000 | 2.00000000  |  2.00000000 |  2.00000000 | 2.00000000  |

我想要什么

    即使我需要每个间隔的所有具有默认值或先前记录值(打开、关闭、高、低及其时间戳)的记录,输出也只给出表永久记录case a) 这里,没有关于 '2017-09-11 00:05:00' 的记录。在这种情况下,开、关、高、低值将设置为 0(零)。case b) 在这里,“2017-09-15 05:25:00”、“2017-09-15 05:30:00”等没有记录,但它位于“2017-09-15 05”这两个记录之间: 20:00' 和 2017-09-15 08:30:00。在这种情况下,“2017-09-15 05:25:00”的 ohlc 值应该是前一个日期时间(“2017-09-15 05:15:00”)设置的 ohlc 值。

谢谢

【问题讨论】:

【参考方案1】:

要获取丢失的记录,您需要

    使用所有需要的值创建一个额外的表 LEFT JOIN 从额外的表到您已有的查询 对将显示在输出中的NULLs 执行一些操作。

如果您使用 MariaDB,有一个方便的“序列”生成器。对于第 1 步。在 mysql 中,我建议您预先构建一个表格,例如从 0 到一百万的数字,然后使用 + INTERVAL ... SECOND 创建 5 分钟的日期时间

【讨论】:

以上是关于ohlc(open,high,low and close) 两个日期范围之间不同时间的记录内部(1,5,15,60,120)分钟,1 天即使没有记录的主要内容,如果未能解决你的问题,请参考以下文章

Canvas绘制股票K线图

OHCL (Open-high-low-close) T-SQL 查询

Optimizing web servers for high throughput and low latency

将嵌套 JSON 转换为 Pandas 列

如何在 MySQL 中读取 JSON 值

Horns provide high gain, low VSWR, relatively wide bandwidth, low weight, and are easy to construct