开发者

5 minute bars using mysql

开发者 https://www.devze.com 2023-03-25 00:33 出处:网络
i have collcted tick data in format like follows \"instrument\",\"tick_time\",\"ask\",\"bid\",\"askVol\",\"bidVol\"

i have collcted tick data in format like follows

"instrument","tick_time","ask","bid","askVol","bidVol"
"EUR/USD","2011-07-24 20:00:01","1.43676","1.43666","1.13","4.13"
"EUR/USD开发者_StackOverflow","2011-07-24 20:00:02","1.43676","1.43666","1.13","4.5"
"EUR/USD","2011-07-24 20:00:03","1.43674","1.43664","1.13","1.65"
"EUR/USD","2011-07-24 20:00:06","1.43675","1.43665","1.13","5.4"
"EUR/USD","2011-07-24 20:00:06","1.43677","1.43668","1.13","4.28"
"EUR/USD","2011-07-24 20:00:10","1.43676","1.43666","1.13","6.15"
"EUR/USD","2011-07-24 20:00:11","1.43679","1.43669","1.13","4.13"

NOW i am trying to create 5 minute bars out of it. i tried the following sql.

select    ROUND(tick_time / (60*5)) * 60 * 5 as bar_time,
          max(bid)  as high,
          min(bid) as low
from ticks
group by bar_time

I get the following data

"bar_time","high","low"
"20110724199900","1.43669","1.43661"
"20110724200200","1.4366","1.4358"
"20110724200500","1.43625","1.43579"
"20110724200800","1.4365","1.43608"
"20110724201100","1.43645","1.43601"
"20110724201400","1.43624","1.43573"
"20110724201700","1.43619","1.43591"
"20110724202000","1.43644","1.4359"

How do i do the following

  1. format the bar_time in datetime format?
  2. aggregate first and last data as columns.


Other solution:

select 
    date_format( 
        date_sub(
            tick_time, 
            INTERVAL (extract(MINUTE from tick_time) % 5) MINUTE
        ), 
        "%m-%d%Y %H:%i"
    ) as  bar_time, 
    MAX(bid) AS high,
    MIN(bid) AS low
from ticks
group by bar_time


1- Format the bar_time in datetime format:

Assuming your time format is %Y%m%d%H%i%s, you can use str_to_date mysql function:

select  str_to_date((ROUND(tick_time / (60*5)) * 60 * 5) , '%Y%m%d%H%i%s') as bar_time,
        max(bid)  as high,
        min(bid) as low
from ticks
group by bar_time

You may need to convert the first parameter of function (i.e. ROUND(...) ) to string. Also, first row in your example contains time "20110724199900", are you sure about that 99 minutes?

2- Didn't understand, can you explain what do you mean?


SELECT    
  STR_TO_DATE
  (
    CONCAT(
      LEFT(ROUND(tick_time / (60*5)) * 60 * 5,10)
      ,MID(ROUND(tick_time / (60*5)) * 60 * 5,10,2)/100*60
      ,RIGHT(ROUND(tick_time / (60*5)) * 60 * 5,2)/100*60
    )
    ,'%Y%m%d%H%i%s'
  ) AS bar_datetime
  , MAX(bid) AS high
  , MIN(bid) AS low
  , CONCAT(MAX(bid), ',', MIN(bid)) as max_min
FROM ticks
GROUP BY bar_datetime
0

精彩评论

暂无评论...
验证码 换一张
取 消