Menu

Wednesday, December 7, 2016

Testing and Analysis of Algorithmic Trading Strategies in MATLAB (Part 4) – Genetic Algorithms

This post is about how important is to use different types of optimisation methods such as genetic algorithms and parallelisation to get results faster.

Genetic Algorithms Optimisation


Despite the fact that the genetic (evolutionary) algorithm principle is very well explained in the MathWorks webinars, in the examples, however, it is used only for optimisation of the choice of a strategy group from a set. This is a good example of the use of these algorithms, however, it happens that there is a need to set many variables with significant intervals for one strategy, you don't get by with one iteration and the parallelisation of processes – calculations can take several days. Certainly, there are strategies in the final stage of optimisation, when we almost surely know the trading strategy is successful, we can wait for several days as well or rent the whole cluster - the result might be worth it. However, if we need to "estimate" the results of a "bulky" strategy and decide if it is worth it to spend the time, then genetic algorithms may be perfectly suitable.

We provide the possibility to use three methods to optimise the strategy in WFAToolbox:

  • Linear method – it is a usual mode of sorting in which you will see all intermediate (suboptimal) results. It gives maximum accuracy.
  • Parallel method – all kernels of your CPU will be used. It does not allow to see intermediate results, but significantly speeds up the operation. It gives maximum accuracy during increase of computation speed.
  • Genetic method – it uses the evolutionary optimisation algorithm. It allows to see suboptimal values, but gives the result close to the best. It's not a very accurate method, but it's precise enough for the initial "run" of the strategy. Very fast.

genetic algorithms with MATLAB
Genetic Algorithms


We are often asked if WFAToolbox - Walk-Forward Analysis Toolbox for MATLAB has the ability to use the GPU in calculations. Unfortunately, GPU is not suitable for all tasks and its use is very specific. In order to use it, you need to adjust the logic and the code of each strategy for graphic cores testing. Unfortunately, due to such non-universality of the method one cannot use GPU in WFAToolbox.

Monday, December 5, 2016

Testing and Analysis of Algorithmic Trading Strategies in MATLAB (Part 3) – Visualisation of Process

Continuing Part 2 of the discussion of problems and solutions in testing and analysis of algorithmic trading strategy in MATLAB, I invite you to read this post about problem of unavailability of visualisation of the processes in modern software solutions for testing trading systems.

Visualisation of Testing Process


In my work experience, I often analysed other popular platforms for trading strategy testing, such as TradeStation, MetaStock, Multicharts etc. and I was always surprised at how little attention was paid to visualisation of the testing process. The thing is that when we don't see the results of the intermediate, sub-optimal values of optimised parameters, we often throw away gold along with the dirt. The matter is because of an overly broad sampling, the strategy adjusts the parameters the way we either see a "perfect strategy" which fails in real life or see one or two deals, which are supposedly the best because it was selected such time interval data where the best trading strategy would be "buy-and-hold", but why are then other strategies necessary for?

Visualisation of trading strategy testing process in MATLAB (proposed in webinar)
Visualisation of trading strategy testing process in MATLAB (proposed in webinar)


As a result, without seeing intermediate results, we need to «blindly» change the parameters to try to get the better data; or watch it in some 3D or 4D (colour is the 4th dimension), as proposed in webinars. The analysis of values in the N-dimensional spaces can definitely be an alternative, but has several limitations:

  • What if there are more than 4 dimensions?
  • When you see what signals and at what frequency they appear in the price range, you have almost all the necessary visual representation of your strategy: the frequency of the transactions, their profitability (income curve), the accuracy of opening, the similarity with other suboptimal values, etc.; that cannot be said about the performance in N-dimensional space where all useful information is, in fact, that the optimum value is not only one but there is a whole range of suboptimal values in one or more areas.

While optimising a strategy in WFAToolbox – Walk-Forward Analysis Toolbox for MATLAB®, as a new optimum value is found, the trading strategy signals in the period in-sample and out-of-sample immediately appear on the chart, so you can always control what range of options you should assign, and also you can pause the optimisation without waiting for the end of test, as it becomes clear that something went wrong or everything is fine.

Wednesday, November 30, 2016

Testing and Analysis of Algorithmic Trading Strategies in MATLAB (Part 2) – Easy-to-use GUI

In this post, in continuation of Part 1, I will try to describe the most common problems which occur while testing algorithmic trading strategies in MATLAB when using one's own groundwork or the code from the automated trading webinars.


Easy-to-use GUI


Let's start with the fact that there is no graphical interface because if we presume that almost the whole process of testing and analysing trading strategies is standardised (it is 99%), you would like to have the interface which helps you call up the necessary data and start the testing process with one click.

Someone (experienced users of MATLAB, in particular), might argue that the use of ready-made functions is not any worse and actually is even sometimes better and more convenient that the static GUI. It's possible, but a GUI however has a number of advantages:

  1. For new (and not only) users of MATLAB it is much more convenient to use a GUI with buttons and entry fields than to search in the code; therefore, there is a GUI even in the MathWorks Toolboxes in most cases because it is more convenient. 
  2. It allows focusing only on the code of your strategy because use of a GUI does not at all imply that it somehow limits your ability to write a strategy.

Thus, in WFAToolbox, we created a possibility to write any codes for your strategy, using any of MATLAB toolboxes and working with multiple assets for the strategies such as pairs trading, basket trading or triplet arbitrage, etc.; but at the same time this code is easily integrated in the GUI by use of patterns, which are simple enough to apply in the code and they do not limit opportunities.

In order to easily master the patterns of code to create your strategies, not only we created detailed WFAToolbox Documentation, but also WFAToolbox Video Tutorial, which provides an opportunity to a full-scale work with the app in a few minutes.

Easy to use GUI of WFAToolbox - Walk-Forward Analysis Toolbox for MATLAB
Easy-to-use GUI of WFAToolbox




Tuesday, November 29, 2016

Testing and Analysis of Algorithmic Trading Strategies in MATLAB (Part 1) - Introduction

Hello, my name is Igor Volkov, I have been developing algorithmic trading strategies since 2006 and have worked in several hedge funds. In this article, I would like to discuss difficulties arising on the way of MATLAB trading strategies developer during testing and analysis, as well as to offer possible solutions.

I have been using MATLAB for testing of algorithm strategies since 2007 and I have come to conclusion that this is not only the most convenient research tool, but also the most powerful one because it makes possible using of complex statistical and econometric models, neural networks, machine learning, digital filters, fuzzy logic, etc by adding toolbox. The MATLAB language is quite simple and well documented, so even a non-programmer (like me) can master it.

How It All Started...


It was 2008 (if I am not mistaken) when the first webinar on algorithmic trading in MATLAB with Ali Kazaam was released, covering the topic of optimising simple strategies based on technical indicators, etc. in spite of a rather “chaotic” code, tools were interesting enough to use. They served as a starting point for research and enhancement of a testing and analysis model which would allow to use all the power of toolboxes and freedom of MATLAB actions during creation of one's own trade strategies, at the same time it would allow to control the process of testing and the obtained data and their subsequent analysis would choose effective portfolio of robust trading systems.

Subsequently, Mathworks webinars have been updated every year and gradually introduced more and more interesting elements. Thus, the first webinar on pairs trading (statistical arbitrage) using the Econometric Toolbox was held in 2010, although the Toolbox of testing and analysis remained the same.

In 2013, Trading Toolbox from Mathworks appeared which allowed to connect MATLAB to different brokers for execution of their applications. Although there were automatic solutions for execution of the transactions, from that point MATLAB could be considered a system for developing trading strategies with a full cycle: from data loading to the execution of automated trading strategies.

Why Should Every Algotrader Reinvent the Wheel?


However, Mathworks has not offered a complete solution for testing and analysis of the strategies – those codes that you could get out of webinars were the only "elements" of a full system test, and it was necessary to modify them, customise them, and add them to the GUI for ease of use. It was very time consuming, thus posing a question: whatever the strategy was, it must go through the same process of testing and analysis, which would allow it to be classified as stable and usable – so why should every algotrader reinvent the wheel and write his/her own code for proper testing strategies in MATLAB?

So the decision was made to create a product that would allow to perform the whole process associated with the testing and analysis of algorithmic trading strategies using a simple and user-friendly interface. 
WFAToolbox - Walk-Forward Analysis Toolbox for MATLAB®

We  decided to call the solution WFAToolbox - Walk-Forward Analysis Toolbox which demo version has been available on http://wfatoolbox.com since 2013.

Monday, November 7, 2016

Whoa?! What happened with the blog?

Hi,

My name is Igor Volkov and I'm the founder and developer of WFAToolbox - Walk-Forward Analysis Toolbox for MATLAB. WFAToolbox is an add-on that is aimed to ease the life of MATLAB users, who develop their own algorithmic trading strategies. This add-on has lots of benefits that are too many to enumerate here, but I will suggest you try it now by downloading its demo version from our official website: http://wfatoolbox.com.

WFAToolbox - Walk-Forward Analysis Toolbox for MATLAB (Algorithmic Trading)
WFAToolbox - Walk-Forward Analysis Toolbox for MATLAB


First of all, I would like to answer the following questions:

What happened with the blog?


1. Jev Kuznetsov is not the owner anymore


The blog was purchased from our friend, Jev Kuznetsov, who has moved to his other blog http://tradingwithpython.blogspot.com. He concluded that Python is better than MATLAB for trading, which I considered to be false. MATLAB remains one of the best software in the world for algorithmic trading purposes IMHO (I have some facts about this though for future discussion).

2. We have changed the brand


From this moment the blog will be called MatlabTrading, which is much more understandable regarding the topics it will include. Furthermore, the domain name has been changed to www.matlabtrading.com instead of the initial matlab-trading.blogspot.com, although the old domain is still working redirecting from the primary domain name.

What will happen to the blog?


1. More posts and articles


We hope to bring life to this blog by posting relevant contents once or twice a week. In the first few months, we will post mostly those articles and videos that we already have to make it easier for our dear readers to search for information on one resource and have crosslink on them.

Then we have plans to write posts about practical aspects of algorithmic trading in MATLAB. How to create modern automatic trading strategies such as:

  • Statistical arbitrage pairs trading / mean reversion / market neutral trading strategies based on cointegration / bollinger bands / kalman filter etc for commodities, stocks and Forex.
  • Trend following strategies with Jurik Moving Average and other sophisticated digital filters;
  • Forecasting strategies with machine learning (Support Vector Machines) and other methods;
  • Creating robust trading strategies using visual walk-forward testing money management for reinvesting your capital (science on how to get $1M from $10K in a year with maximum, but estimated risk and sweat rewards). Maybe after reading this you've thought this is going to be another dumb article for those poor guys seeking how to become rich through "trading on forex" and all that. Well, that is totally false! We are working in MATLAB, and majority of us are scientists and experts in that aspect so everything is serious.

2. More interactivity


I will be happy if we can all relate through comments in posts. Subscribe to our news to get alerted about the newest posts and events. Later on, we have plans to make Google Hangouts webinars. Don't miss it, click on "Follow" button at the upper right corner to join our community.

What would you like to read in our blog posts? What topics can you suggest? Please write here in comments.

Tuesday, January 1, 2013

Intraday mean reversion

In my previous post I came to a conclusion that close-to-close pairs trading is not as profitable today as it used to be before 2010. A reader pointed out that it could be that mean-reverting nature of spreads just shifted towards shorter timescales. I happen to share the same idea, so I decided to test this hypothesis.

This time only one pair is tested: 100$ SPY vs -80$ IWM. Backtest is performed on 30-second bar data from 11.2011 to 12.2012.
The rules are simple and similar to strategy I tested in the last post:
if bar return of the pair exceeds  1 on z-score, trade the next bar.
The result looks very pretty:

I would consider this to be enough proof that there is still plenty of mean-reversion on 30-second scale.
If you think that this chart is too good to be true, that is unfortunately indeed the case. No transaction costs or bid-ask spread were taken into account. In fact, I would doubt that there would be any profit left after subtracting all trading costs.
Still, this kind of charts is the carrot dangling in front of my nose, keeping me going...

Sunday, December 30, 2012

Is pairs trading dead?

Bad news everybody, according to my calculations, ( which I sincerely hope are incorrect) the classical pairs trading is dead. Some people would strongly disagree, but here is what I found:

Let's take a hypothetical strategy that works on a basket of etfs:
['SPY','XLY','XLE','XLF','XLI','XLB','XLK','IWM','QQQ','DIA']
From these etfs 90 unique pairs can be made. Each pair is constructed as a market-neutral spread.

Strategy rules:
On each day, for each pair, calculate z-score based on 25-day standard deviation.
If z-score > threshold, go short, close next day
If z-score < -threshold go long, close next day

To keep it all simple, the calculation is done without any capital management (one can have up to 90 pairs in portfolio on each day) . Transaction costs are not taken into account either.

To put it simply,  this strategy tracks one-day mean reverting nature of market neutral spreads.
Here are the results simulated for several thresholds:


No matter what threshold is used, the strategy is highly profitable in 2008, pretty good throuh 2009 and completely worthless from early 2010.
This is not the first time I came across this change in mean-reverting behavior in etfs. No matter what I've tried, I had no luck in finding a pairs trading strategy that would work on ETFs past 2010. My conclusion is that these types of simple stat-arb models just don't cut it any more.