【问题标题】:SQL Server : tick-by-tick data to OHLCSQL Server:逐个滴答数据到 OHLC
【发布时间】:2018-04-25 22:24:14
【问题描述】:

任何人都可以帮助使用 SQL 查询以每 1 分钟将一个滴答数据转换为 OHLC(打开\高\低\关闭)吗?我能够获得高低数据,但在打开和关闭时遇到了困难。

我的示例数据如下所示:

  idc   ServerDateTime          iDateTime   sSymbol cAsk    cBid    Spread
---------------------------------------------------------------------------
2539581 2017-11-13 00:14:56.357 1510539296  EURUSD  1.16473 1.16460 0.00013
2539582 2017-11-13 00:14:56.373 1510539296  EURUSD  1.16475 1.16461 0.00014
2539583 2017-11-13 00:14:56.423 1510539296  EURUSD  1.16476 1.16462 0.00014
2539584 2017-11-13 00:14:56.520 1510539296  EURUSD  1.16477 1.16463 0.00014
2539585 2017-11-13 00:14:56.643 1510539296  EURUSD  1.16478 1.16463 0.00015
2539586 2017-11-13 00:14:58.207 1510539298  EURUSD  1.16478 1.16464 0.00014
2539587 2017-11-13 00:14:59.477 1510539299  EURUSD  1.16477 1.16464 0.00013
2539588 2017-11-13 00:15:00.337 1510539300  EURUSD  1.16477 1.16463 0.00014
2539589 2017-11-13 00:15:00.747 1510539300  EURUSD  1.16478 1.16463 0.00015
2539590 2017-11-13 00:15:00.873 1510539300  EURUSD  1.16477 1.16463 0.00014
2539591 2017-11-13 00:15:01.510 1510539301  EURUSD  1.16477 1.16464 0.00013

有一些类似的问题有答案,但这些是针对 python 和 MySQL 的。

【问题讨论】:

  • 什么版本的 SQL Server?我可以理解为什么您在开盘和收盘时遇到问题,因为这些是从您的数据中缺失的最后交易价格得出的。
  • 版本是 SQL Server 2017。上面我从我的数据中给出了新的示例。我的数据实际上有超过 200 万。行。

标签: sql-server


【解决方案1】:

我终于在此链接中找到了解决方案。 https://data.stackexchange.com/stackoverflow/query/61772/new

select  min(ServerDateTime) as DateTime
,       max(cBid) as Highest
,       min(cBid) as Lowest
,       min(case when rn_asc = 1 then [cBid] end) as first
,       min(case when rn_desc = 1 then [cBid] end) as Last
from    (
        select  row_number() over (
                    partition by cast(cast(ServerDateTime as float) * 60 * 24 as int)
                    order by ServerDateTime) as rn_asc
        ,       row_number() over (
                    partition by cast(cast(ServerDateTime as float) * 60 * 24 as int)
                    order by ServerDateTime desc) as rn_desc
        ,       *
        from    dbo.MT4TICK
        ) as SubQueryAlias

group by
        cast(cast(ServerDateTime as float) * 60 * 24 as int)
order by DateTime

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多