Archive for the 'qStudio' Category

kdb 3.5 Released with Stack Trace

kdb+ 3.5 had a significant number of changes:

  • Debugger – At long last we can finally get stack traces when errors occur.
  • Concurrent Memory Allocator – Supposedly better performance when returning large results from peach
  • Port Reuse – Allow multiple processes to listen on same port. Assuming Linux Support
  • Improved Performance – of Sorting and Searching
  • Additional ujf function – Similar to uj from v2.x fills from left hand side

kdb Debugger

The feature that most interests us right now is the Debugging functionality. If you are not familiar with how basic errors, exceptions and stack movement is handled in kdb see our first article on kdb debugging here. In this short post we will only look at the new stack trace functionality.

Now when you run a function that causes an error at the terminal you will get the stack trace. Here’s a simple example where the function f fails:

Whatever depth the error occurs at we get the full depth stack trace, showing every function that was called to get there using .Q.bt[]:

qstudio-stack-trace-error

The good news is that this same functionality is availabe in qStudio 1.43. Give it a try: qStudio.

Note: the ability to show stack traces relies on qStudio wrapping every query you send to the server with its own code to perform some analysis and return those values. By default wrapping is on as seen in preferences. If you are accessing a kdb server ran by someone else you may have to turn wrapping off as that server may limit which queries are allowed. Unfortunately stack tracing those queries won’t be easily possible.

That’s just the basics, there are other new exposed functions and variables, such as .Q.trp – for trapping calls and accessing traces that we are going to look at in more detail in future.


qStudio 1.41 Release with Custom Security

qStudio 1.41 is now available to download.

It adds the ability to use custom Security Authentications and custom JDBC drivers.
By automatically loading .jar plugins from libs folder.

After a few users reported issues around “watched expressions” we are removing the ctrl+w shortcut as it was often getting used by mistake. The last change was some internal work to improved startup/shutdown logging for debugging purposes..

qStudio kdb IDE 1.40 Released

qStudio 1.40 is now available to download.

The latest changes include:

  • No need to save changes before shutdown, unsaved changes stored till reopened.
  • Add sqlchart to system path.
  • Fix display of tables with underscore in the name.
  • Database documenter/report enhancements
  • Improved code printing
  • FileTreePanel much more efficient at displaying large number of files.

qStudio adds Nested Server Folder Support

Since our last qStudio kdb+ IDE announcment we have added a lot of new features:

Bulk importing kdb server lists

Bulk importing kdb server lists

There’s a lot of new features to allow supporting a huge number of servers efficiently:

  • Support importing HUGE number of servers:
    • 5000+ server connections are now supported
    • To prevent massive memory use, the object tree for a server is no longer refreshed at startup only on connection.
    • Allow specifying default username/password once for all servers
    • Allow nested connection folders
    • Add critical color option – servers with prod in name get highlighted in red
  • Sort File Tree Alphabetically
  • Numerous bugfixes including:
    • Fix critical Mac bug that prevented launching in some instances
    • Fix query cancelling

qStudio kdb+ GUI adds Dark Theme and Chinese Language

Based on user requests we have released a number of new features with qStudio 1.36:

Download the latest ->qStudio<- now.

Dark Code Editor Themes

qstudio-kdb-dark-theme-gui

Which can be set under settings->preferences

qstudio-settings-preferences

Open Results and Charts in New Window

To expand a panel into a new window click the “pop-out” icon.
pop-out

This will bring up the result in a new window:

mutliple-chart-windows

UTF-8 Chinese Language Support

qstudio-utf8

Command Line Kdb+ Charts

sqlDashboards are included as a bundle with qStudio, part of that package is a command line utility called sqlChart that allows generating customized sql charts from the command line.

Checkout the video to see how you can create a chart based on data from a kdb+ database in 2 minutes:

The sqlChart page has all the documentation you need, Download the qstudio.zip to try it now.

The q Code

Help Screen

RSI Relative Strength Index in Kdb

In this tutorial we are going to recreate this example RSI calculation in q, the language of the kdb database.

The relative strength index (RSI) is a technical indicator used in the analysis of financial markets. It is intended to chart the current and historical strength or weakness of a stock or market based on the closing prices of a recent trading period. The RSI is classified as a momentum oscillator, measuring the velocity and magnitude of directional price movements. Momentum is the rate of the rise or fall in price.

The RSI computes momentum as the ratio of higher closes to lower closes: stocks which have had more or stronger positive changes have a higher RSI than stocks which have had more or stronger negative changes. The RSI is most typically used on a 14 day timeframe, measured on a scale from 0 to 100, with high and low levels marked at 70 and 30, respectively. Shorter or longer timeframes are used for alternately shorter or longer outlooks. More extreme high and low levels—80 and 20, or 90 and 10—occur less frequently but indicate stronger momentum.

.

Stock Price Time-Series Data

We are going to use the following example data, you can download the csv here or the excel version here.

Date QQQQ Close Change Gain Loss Avg Gain Avg Loss RS 14-day RSI
2009-12-14 44.34              
2009-12-15 44.09 -0.25 0.00 0.25        
2009-12-16 44.15 0.06 0.06 0.00        
2009-12-17 43.61 -0.54 0.00 0.54        
2009-12-18 44.33 0.72 0.72 0.00        
2009-12-21 44.83 0.50 0.50 0.00        
2009-12-22 45.10 0.27 0.27 0.00        
2009-12-23 45.42 0.33 0.33 0.00        
2009-12-24 45.84 0.42 0.42 0.00        
2009-12-28 46.08 0.24 0.24 0.00        
2009-12-29 45.89 -0.19 0.00 0.19        
2009-12-30 46.03 0.14 0.14 0.00        
2009-12-31 45.61 -0.42 0.00 0.42        
2010-01-04 46.28 0.67 0.67 0.00     RS RSI
2010-01-05 46.28 0.00 0.00 0.00 0.24 0.10 2.39 70.53
2010-01-06 46.00 -0.28 0.00 0.28 0.22 0.11 1.97 66.32
2010-01-07 46.03 0.03 0.03 0.00 0.21 0.10 1.99 66.55
2010-01-08 46.41 0.38 0.38 0.00 0.22 0.10 2.27 69.41
2010-01-11 46.22 -0.19 0.00 0.19 0.20 0.10 1.97 66.36
2010-01-12 45.64 -0.58 0.00 0.58 0.19 0.14 1.38 57.97
2010-01-13 46.21 0.57 0.57 0.00 0.22 0.13 1.70 62.93
2010-01-14 46.25 0.04 0.04 0.00 0.20 0.12 1.72 63.26
2010-01-15 45.71 -0.54 0.00 0.54 0.19 0.15 1.28 56.06
2010-01-19 46.45 0.74 0.74 0.00 0.23 0.14 1.66 62.38
2010-01-20 45.78 -0.67 0.00 0.67 0.21 0.18 1.21 54.71
2010-01-21 45.35 -0.43 0.00 0.43 0.20 0.19 1.02 50.42
2010-01-22 44.03 -1.33 0.00 1.33 0.18 0.27 0.67 39.99
2010-01-25 44.18 0.15 0.15 0.00 0.18 0.26 0.71 41.46
2010-01-26 44.22 0.04 0.04 0.00 0.17 0.24 0.72 41.87
2010-01-27 44.57 0.35 0.35 0.00 0.18 0.22 0.83 45.46
2010-01-28 43.42 -1.15 0.00 1.15 0.17 0.29 0.59 37.30
2010-01-29 42.66 -0.76 0.00 0.76 0.16 0.32 0.49 33.08
2010-02-01 43.13 0.47 0.47 0.00 0.18 0.30 0.61 37.77

RSI Formulas

The formulas behind the calculations used in the table are:

  • First Average Gain = Sum of Gains over the past 14 periods / 14.
  • First Average Loss = Sum of Losses over the past 14 periods / 14

All subsequent gains than the first use the following:

  • Average Gain = [(previous Average Gain) x 13 + current Gain] / 14.
  • Average Loss = [(previous Average Loss) x 13 + current Loss] / 14.
  • RS = Average Gain / Average Loss
  • RSI = 100 – 100/(1+RS)

Writing the Analytic in q

We can load our data (rsi.csv) in then apply updates at each step to recreate the table above:

Code kindly donated by Terry Lynch

Rather than create all the intermediate columns we can create a calcRsi function like so:

Finally we can visualize our data using the charting functionality of qStudio (an IDE for kdb):

RSI Relative Strength Index stock chart for QQQQ created using qStudio

RSI Relative Strength Index stock chart for QQQQ created using qStudio

Or to plot RSI by itself (similar to original article

RSI Line Graph in qStudio

RSI Line Graph in qStudio

Writing kdb analytics such as Relative Strength Index is covered in our kdb training course, we offer both public kdb training courses in New York, London, Asia and on-site kdb courses at your offices, tailored to your needs.

Exponential Moving Average EMA in Kdb

Let’s look at how to write moving average analytics in q for the kdb database. As example data (mcd.csv) we are going to use stock price data for McDonalds MCD. The below code will download historical stock data for MCD and place it into table t:

Simple Moving Average

The simple moving average can be used to smooth out fluctuating data to identify overall trends and cycles. The simple moving average is the mean of the data points and weights every value in the calculation equally. For example to find the moving average price of a stock for the past ten days, we simply add the daily price for those ten days and divide by ten. This window of size ten days then moves across the dates, using the values within the window to find the average. Here’s the code in kdb for 10/20 day moving average and the resultant chart.

Simple Moving Average Stock Chart Kdb for MCD

Simple Moving Average Stock Chart Kdb (Produced using qStudio)

What Exponential Moving Average is and how to calculate it

One of the issues with the simple moving average is that it gives every day an equal weighting. For many purposes it makes more sense to give the more recent days a higher weighting, one method of doing this is by using the Exponential Moving Average. This uses an exponentially decreasing weight for dates further in the past.The simplest form of exponential smoothing is given by the formula:

exponential-moving-average-formula

where α is the smoothing factor, and 0 < α < 1. In other words, the smoothed statistic st is a simple weighted average of the previous observation xt-1 and the previous smoothed statistic st−1.

This table displays how the various weights/EMAs are calculated given the values 1,2,3,4,8,10,20 and a smoothing factor of 0.7: (excel spreadsheet)

Values EMA   Power Weight Power*Weight   EMA (text using previous value)
1 1   6 0.0005103 0.0005103   1
2 1.7   5 0.001701 0.003402   (0.7*2)+(0.3*1)
3 2.61   4 0.00567 0.01701   (0.7*3)+(0.3*1.7)
4 3.583   3 0.0189 0.0756   (0.7*4)+(0.3*2.61)
8 6.6749   2 0.063 0.504   (0.7*8)+(0.3*3.583)
10 9.00247   1 0.21 2.1   (0.7*10)+(0.3*6.6749)
20 16.700741   0 0.7 14   (0.7*20)+(0.3*9.00247)

To perform this calculation in kdb we can do the following:


(This code was originally posted to the google mail list by Attila, the full discussion can be found here)

This backslash adverb works as

The alternate syntax generalizes to functions of 3 or more arguments where the first argument is used as the initial value and the arguments are corresponding elements from the lists:

Exponential Moving Average Chart

Finally we take our formula and apply it to our stock pricing data, allowing us to see the exponential moving average for two different smoothing factors:

Exponential Moving Average Stock Price Chart produced using qStudio

Exponential Moving Average Stock Price Chart produced using qStudio

As you can see with EMA we can prioritize more recent values using a chosen smoothing factor to decide the balance between recent and historical data.

Writing kdb analytics such as Exponential Moving Average is covered in our kdb training course, we regularly provide training courses in London, New York, Asia or our online kdb course is available to start right now.

qStudio kdb GUI 1.31 Released – the Command Palette

qStudio is an IDE for kdb+, you can download it now.

The latest 1.31 release introduces a number of nice interface additions as requested by users:

Command Palette

Quick change server -> Hit Ctrl+P to try the new Command Palette
It lets you run common qStudio actions by fuzzy matching on keywords:

quick-kdb-server-change

Jump to Definition

Hit Ctrl+U Ctrl+I to get an outline of the current file.

qstudio-jump-to-function-definition
Or press Ctrl+D on a function call to jump to where it is defined.

Added User Preferences

Users had mentioned a number of connection and interface issues they wanted to be able to customize, these have been added:
– Customize the editor font and it’s background color (per server setting, useful for red warning color when connected to prod machines)
– keep same connection open for every query
– Wrap every query with selected text before and after

qstudio-kdb-custom-font-color

Eval Line by Line

Do you ever query a line, move down, query next line, move down ….
Well try control+shift+enter within the code editor.
It evaluates the current line, returns it’s value and shifts to the next line.

e.g. for the code:
a:11
b:a*rand[10]
c:b*a

The console would show:
q)a:11
11
q)b:a*rand[10]
55
q)c:b*a
605

The qStudio help guide contains more details of all functionality.

qStudio v1.29 Release – Kdb+ IDE

qStudio is an IDE for kdb+ database by kx systems that allows querying kdb+ servers, charting results and browsing server objects from within the GUI.

Version 1.29 of qStudio is now available:
http://www.timestored.com/qstudio/

Changes in the latest version include a new dark theme for charts and the ability to run multiple instances of qStudio.

The changes were added as a few people had asked for a dark theme due to eye strain from staring at the white charts.
If you have any ideas for what you would like to see in the next version please let me know.

john AT timestored DOT com

Here’s a preview of a time series graph:

qStudio with a dark time series chart

qStudio with a dark time series chart

dark-chart-theme-2

sql dashboard with dark time series charts