Time to look into Julia for your financial datamining adventures. The following snippet uses Julia release-0.1, and will not work the cutting-edge Julia.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Time to look into Julia for your financial datamining adventures. The following snippet uses Julia release-0.1, and will not work the cutting-edge Julia.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
She’s still in chef school, but this Julia will graduate sooner than many are expecting. She’s at the head of her class,
has the basics down and is well on her way to making mouth-watering packages. A nice start for trader/hackers is the Thyme
package. It’s not the perfect omelette, but it’s a tasty start. You can get your favorite csv data into a DataFrame and
make some basic transformations to it, such as calculating returns, moving averages, equity curves and lag/lead observations.
There is already similar functionality in R and Python’s pandas, so we’ll take a look at all three at the end in a little
competition. Let’s use a common dataset for comparison, Yahoo’s GSPC time series from 1950 to 2012. This dataset is SPX
daily data and includes 15,851 rows.
To demonstrate our first Thyme function, let’s import that data into Julia.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
R’s quantmod and Python’s pandas have similar methods of getting this data into the proper structure. Here’s a hint on how that’s done.
1 2 3 | |
1 2 3 | |
The basic transformations you can do with quantmod and pandas can be done with Thyme. Lagging and leading functions
are not in Julia base, but they are in Thyme
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
The lag and lead functions also have bang versions, lag! and lead!. These modify the DataFrame by adding a column.
1 2 3 4 5 6 7 8 | |
Every quant wants to know about returns. There are two that you’d expect, log_return and simple_return, and a speciality
function called equity that generates an equity curve. log_return and simple_return are padded with 0.0 instead of NA
while equity is padded with NA. All three of these functions also have bang versions, which we’ll use to continue to modify our
spx dataset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Moving averages are another staple in the quant cookbook. Instead of creating a special function called sma, Thyme
generalizes the function in moving and allows the passing of any valid function. To create a simple moving average,
pass in mean. You can also pass in max, min, var, kurtosis, skewness, etc. Let’s use the bang version on our
spx object.
1 2 3 4 5 6 7 8 | |
Time for some speed trials. Let’s take the original dataset for the challenge. Thyme will use the moving function and pass
in the base Julian skewness. For R we’ll use zoo::rollapply and pass in the PerformanceAnalytics::skewness function. pandas
has a dedicated function for this called rolling_skew. Let the Iron Programming Languages begin!
1 2 3 | |
1 2 3 | |
1 2 | |
R is a bit slow. Mainly because rollapply uses an R loop so this really isn’t fair.
Python is so fast that you need to use timeit. I’m sure it’s a Cython loop and not a Python loop.
Julia’s Thyme package did okay. Not even close to pandas but over 40 times faster than the
R method.
The nice thing about moving! is that, apart from some housekeeping code, it’s a fairly simple function. It’s a
loop defined in a nice single line of code. The entire code below shows moving! calling mvg. This was done for
DRY reasons since the mvg code is also in moving. But aside from some (admittedly) bizarre and expensive NA
padding, it’s fairly straight-forward.
1 2 3 4 5 6 7 8 9 10 11 | |
Julia is the new kid in the kitchen. Time to start paying attention. She may cook your bacon when you’re not looking.

RUT monthly returns for December.

NDX monthly returns for December.

SPX monthly returns for December.

SPX monthly returns for September

SPX returns the day after a 2% rally in September.

SPX returns for the first day in September.

SPX returns for the last day in August.

NDX returns for the last day in August.