kdb+ A-share database

Source: Internet
Author: User

Kdb+ (K-based DB, enhanced version, or KDB) is a very small database, it has very high performance, rich and efficient time series functions, in the access and real-time analysis of large quantities of stocks, foreign exchange and other high-frequency data in this field, only KDB alone.

kdb+ is a column-based, in-memory database developed and sold by Kxsystems. It is typically used for high-frequency trading and is ideal for high-speed storage, analysis, processing, and retrieval of large datasets. Kdb+ can process billions of records and analyze data in the database. The database is available in 32-bit (free) and 64-bit versions through various operating systems. Financial institutions use kdb+ to analyze time series data, such as stock or commodity trading data. The database is also used for other time-sensitive data applications, including stock and commodity trading, telecommunications, sensor data, log data, and monitoring of machine and network usage.
The Q language is a proprietary array processing language developed by Arthur Whitney and commercialized by KX. The language is used as the query language for kdb+, kdb+ is based on the K language (a variant of the APL language). Q language is K concise packaging, which provides some readability.

Kdb+ 's rich time series function can help us to easily carry out stock calculations, I have a A-share database based on KDB, daily table stores the Shanghai A-shares and Shenzhen A-shares of the daily data (pre-restoration rights). Let's take a look at kdb+ 's data processing, and the subsequent article will explain how to use KDB and how to create the database.


1. Inquire about the open Price (O), High (O), Low (L), closing price (c) of a day's stock (SYM). The following results show only 5 data, and other data is ignored.

Q) Select from daily where date = 2016.11.10date sym o h L C-----------------------------------------2 016.11.10 000001 9.1 9.16 9.1 9.142016.11.10 000002 26 28.28 25.58 26.562016.11.10 000004 45 46.87 44.85 45.320 16.11.10 000005 7.45 7.68 7.44 7.532016.11.10 000006 10.09 10.17 10.06 10.12..

2. Inquire about the price of stock 002695 (Huang-huang) for the last 10 trading days.

Q) -10#select from daily where sym= ' 002695date       sym     o     h     l      c-----------------------------------------2016.10.25 002695 26.03 27.5   24.55 26.732016.10.26 002695 25.9  27.89 25.82 26.852016.10.27 002695  26.7  29.54 26.33 29.542016.11.02 002695 32.49 32.49 30.86  32.492016.11.03 002695 34    35.74 32.21 32.772016.11.04 002695  32.31 33.69 30.15 30.512016.11.07 002695 30.18 31     29.6  30.592016.11.08 002695 30.48 32.56 29.78 31.42016.11.09 002695  30.53 31.99 30.05 31.52016.11.10 002695 31.8  31.99 30.8   31.19

3. Calculate the simple moving average price of 002695 (Huang-Huang) 5th, 10th, 20th

Q) -10#select date,sym,o,h,l,c, ma5:mavg[5;c],ma10:mavg[10;c], ma20:mavg[20;c] from  Daily where sym= ' 002695date       sym    o      h     l     c      MA5    MA10    MA20---------------------------------------------------------------2016.10.25 002695 26.03 27.5   24.55 26.73 23.044 20.108 18.70452016.10.26 002695 25.9   27.89 25.82 26.85 24.588 21.082 19.11552016.10.27 002695 26.7   29.54 26.33 29.54 26.288 22.351 19.632016.11.02 002695 32.49 32.49  30.86 32.49 28.184 23.86  20.3422016.11.03 002695 34     35.74 32.21 32.77 29.676 25.426 21.15352016.11.04 002695 32.31 33.69 30.15 30.51 30.432 26.738  21.8342016.11.07 002695 30.18 31    29.6  30.59 31.18   27.884 22.52952016.11.08 002695 30.48 32.56 29.78 31.4  31.552  28.92  23.2892016.11.09 002695 30.53 31.99 30.05 31.5   31.354 29.769 24.052016.11.10 002695 31.8  31.99 30.8  31.19  31.038 30.357 24.757

4. Calculates the 10th exponential moving average of 002695 (Wong Huang)

Q) -10#select date,sym,o,h,l,c, ma5:ema[2%1+10;c] from daily where sym= ' 002695date       sym    o      h     l     c      MA5--------------------------------------------------2016.10.25 002695 26.03 27.5   24.55 26.73 21.396752016.10.26 002695 25.9  27.89 25.82 26.85  22.388252016.10.27 002695 26.7  29.54 26.33 29.54 23.688572016.11.02  002695 32.49 32.49 30.86 32.49 25.288832016.11.03 002695 34     35.74 32.21 32.77 26.649042016.11.04 002695 32.31 33.69 30.15  30.51 27.351032016.11.07 002695 30.18 31    29.6   30.59 27.939942016.11.08 002695 30.48 32.56 29.78 31.4  28.569042016.11.09 002695 30.53 31.99 30.05  31.5  29.101942016.11.10 002695 31.8  31.99 30.8  31.19  29.48159

5. Find the ticker symbol for the highest record in the last 20 trading days

Q) 10 cut exec distinct sym from daily where 1b=  ({(max -250# x)  = max -20#x};c)  fby sym,2016.11.10= (last;date)  fby sym000019 000034  000338 000404 000503 000510 000513 000518 000550 000567000568  000581 000607 000615 000635 000639 000661 000678 000721 000723000731  000780 000818 000820 000823 000881 000889 000895 000910  000912000913 000915 000935 000937 000951 000953 000959 000960 000993  002013002050 002051 002082 002085 002088 002094 002113 002120  002122 002124002132 002141 002150 002167 002194 002206 002212 002213  002264 002317002319 002333 002352 002365 002374 002386 002421  002456 002476 002486.. 


This article is from the "Grow to Tomorrow" blog, so be sure to keep this source http://growtotomorrow.blog.51cto.com/12220652/1872013

kdb+ A-share database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.