Menu

Sunday, September 25, 2011

Getting started with Matlab & finance

I am thinking about creating a series of screencasts showing how to use Matlab for financial research. This is the first one, covering the basics and aimed at people not yet familiar with Matlab.
The whole series would include (among others) topics like:

  • Getting data from the web (yahoo, google, CBOE etc)
  • Aligning and filtering datasets
  • Nearest-neighbor classification
  • Designing & backtesting strategies
  • Interfacing with Interactive Brokers
  • Keeping track of strategy performance

Creating quality material takes a lot of time and effort, so I don't think that I will be able to offer whole material for free.
If you are interested in the series please let me know what you think by filling in the poll on the right.

Get source files

Wednesday, August 10, 2011

XIV is stealing my money!

With current volatility exceeding 40, it seemed like a good idea to short the VIX. Two very popular ways to do this is either to short VXX or go long XIV. I choose to go long XIV a couple of days ago, after making sure that it tracks the inverse daily return of VXX.
Now a strange thing has happened that both puzzles me and pisses me off: yesterday there was a tracking error of 2% between the two and today another 1%, both in my disadvantage. It seemed like somebody 'stole' 3% of my position! At this moment I am less than happy with this XIV product.
Take a look at this chart:

Here I plotted daily returns of VXX and XIV against each other, where the outliers have been plotted in red. Please note that there are another 2 outliers of similar magnitude, occuring on  4 and 5 January 2011, but these two cancel each other out pretty nicely.
I thought that both etfs were based on the same SPVXSTR index, but while their intraday path relative to each other is very  stable (no arbitrage possibilities), the tracking offset was present throughout the whole day.
I understand that XIV could get a positive tracking error because it was banned from short selling on both days (just like VXX), but a negative tracking error is a mystery to me.

Can someone shed a light on what is going on here???

Thursday, August 4, 2011

Are we in for a rebound?

Today we've had a pretty heavy drop in the SPY, that has been preceded by a series of down days. It could be tempting to join the panicking crowd, but the crowd usually gets it wrong.
The put/call ratio and the VIX/VXV ratio tell their own story:
Both of them are pretty high, the situation that is usually accompanied by a pop in the market.
Disclosure: I'm long.

Wednesday, June 15, 2011

Implied vs Realized volatility premium

There is a funny thing that I've come across while trying to build a volatility model. According to my estimations the VIX is usually higher than the actual volatility (RV)  realized for that same period. Just take a look at the following chart:
In the top chart I've plotted the VIX (IV) and RV estimate for a 3-year period. The bottom chart is the difference between the IV & RV . To estimate the RV I've used a function from  tradingwithmatlab blog. It uses several common estimators for RV (I've used the average value for RV ).
It looks like IV on average  is 7% higher than the RV. So one could just sell volatility and get a steady profit, right? Knowing that there is no free lunch in trading, it seems that I'm missing out something. Is my estimation of RV incorrect? Is it unjustified to compare VIX and SP500 volatility? All feedback is very welcome...

Monday, May 16, 2011

Guess what, leveraged etfs don't decay!

While writing the previous posts and doing the math I still had a feeling that something in my reasoning was not quite right. I could not put a finger on it, but with the help of the author of OnlyVix blog, I seem to have figured it all out.
Let me start with an old problem of 50/50 chance of winning 1% every timestep. Here is a puzzle from E.Chans blog (and book):
"Here is a little puzzle that may stymie many a professional trader. Suppose a certain stock exhibits a true (geometric) random walk, by which I mean there is a 50-50 chance that the stock is going up 1% or down 1% every minute. If you buy this stock, are you most likely, in the long run, to make money, lose money, or be flat? Most traders will blurt out the answer “Flat!”

...And they would be right. To prove this, let's write down a binomial tree for this case. I'll use 10% step to simplify the math:




Here we start with initial 100$. Every branch has 50% probability. Notice that the expected value at each time step is exactly 100$. However, on the third timestep the most probable value is 99$ .


To double-check it, I've run a Monte-Carlo simulation of the above problem. Here is the result:
The average value is 0, while the median is -0.5% for 100 steps or -0.005% for one step.

The case for 5% change per timestep looks like this:
The distribution shifts to the left, bu again, average value is zero, and median is -0.1176.

So the answer to above puzzle is indeed 'flat'.

Now returning to a leveraged pair like FAS&FAZ, here is a Monte-Carlo simulation of a leveraged pair:
Here I've used a normal distribution for returns of the underlying with sigma = 1%. Once again, the average return over 100 periods is zero, while most of the occurrences are negative.


This means that leveraged etfs don't decay over time, they just look like they do, because that is the most likely outcome.
So here we go, contrary to common belief, the leveraged etfs don't decay after all!

Sunday, May 15, 2011

The problem with shorting leveraged etfs

As I've described in my previous post, inverse etfs decay relative to each other. After looking at their charts it is not difficult to imagine earning 'easy mony' by shorting a pair of leveraged etfs. Max Dama has done this and there are more people doing this according to Google, but I think this type of strategy is pretty risky.

Here is an example of 'easy money'. I've simulated a random walk (upper chart in blue) that has 50/50% chance of going up or down every day. And it always moves exactly by 3% (log, so it should be flat over the long run ). From this reference I've created two leveraged trackers, with +2x and -2x leverage, I'll call them 'up' and 'down'.


Suppose we start a 100 day period with a pair  consisting of equal amounts of capital in 'up' and 'down', both equal to 100$. So the pair value on day 1 is $200. Pair value is plotted in the lower chart.  Without a trend in the reference, the pair value decays at a constant rate exp(0.5log(leverage*(1+daily_delta))+0.5log(leverage*(1-daily_delta))).  If we short both 'up' and 'down' the lower chart will flip upside down, producing pretty good looking pnl.
But before you short sell  every available leveraged etf out there, take a look at the next chart:
All the parameters here are the same, only the underlying has two brief periods of consecutive wins or losses. This results in two heavy spikes in the pair value. If we would have shorted both 'up' and 'down' the result would be a pretty heavy drawdown. No easy money here...

Saturday, May 14, 2011

FAS vs FAZ - inverse etf behavior

There has been a lot of talk about leveraged etf (under) performance . In general, these etfs seem to underperform their benchmark. A google search for 'leveraged etf decay' will provide a couple of hours worth of reading material, so  I will try to limit information redundancy to a minimum. I'll limit myself to a single sentence introduction: 'leveraged and inverse etfs are based on the arithmetic returns of their benchmark, which introduces a negative tracking error'.
If you have little idea about what I'm talking about, take a look here for an explanation of the difference between the arithmetic and geometric returns.
So I'll continue the examination of inverse etf dynamics from what is already known: underperformance.

Let's first take a look at the relation between FAS and FAZ. Both are 3x leveraged versions of the same underlying index, FAZ being the inverse one.
Here can be seen clearly that while the etfs move in the opposite directions, FAS in the long run outperforms FAZ.
Their daily arithmetic returns however are performing exactly as advertised:

However, anybody holding a position for longer than one time period (being a day) should be only interested in geometric returns, or log returns.
When log returns of these two are examined, the picture changes:
Instead of following a straight line, the returns are skewed in favor of FAS. The green line here is a theoretical estimation of inverse relation based on algebraic returns.
For example: FAS gains 10% on a given day and FAZ follows with a 10% decline. In log returns this would translate to FAS: log(1.1) = 0.0953   FAZ: log(0.9)=-0.1054.   The log returns are not equal (duh!) but skewed in favor of FAS . When the position is held for a longer time and the pair moves 10% every day (no matter in which direction), we loose approx 0.5% per day of the total position.
Please take a note that this 'skew' is not about leverage, but inverse algebraic relationship. Leverage only provides more daily movement, exaggerating the skew.
A handy chart below shows the under performance of inverse etf as a function of its underlying daily change. One can see that the error is relatively small for <1% moves, but increases rapidly with bigger moves.


The difference between geometric and algebraic returns has been explained by E.Chan on his blog (and in his book) . However, he made a mistake in the calculation of average loss per time period stating it to be -0.5%.
When we have a 50/50 chance of winning or loosing 1% , in fact the expected return per minute is exp(0.5*log(1.01)+0.5*log(0.99)), which translates to -.005 % per minute, which is equivalent to -7%  in 24 hours ;-).

There are a couple of very interesting strategies that can be derived from this asymmetry, if one can handle the  math and rebalancing logic.

Wednesday, May 11, 2011

In trading no one should ever be absolutely sure

...and the one who is will get punished sooner or later. This has happened countless times, to traders of all skill levels. Once you think you've got a trade on your hands that can't possibly go wrong, you're brewing a recipe for disaster. Everybody knows this, me included, however I just barely escaped such a situation by plain luck.

A couple of days ago I ran my spread scanner and the good old GLD-GDX spread caught my eye. It was so streched up, that I decided to trade it. In fact it was at an extreme that has not happened for a very verly long time. This spread is a 'classic' for spread traders and it has been mean reverting for years now. I'm was sure that it would mean  revert again, and quickly got on board. The only thing that prevented me from betting big was the shortage of free cash and I did not want to close any other positions. So I ended up with a usual size of a spread bet, that I don't allow to produce more than 2% of portfolio volatility.
What happened next is a short story. The spread moved further against me, knocking 2% off my portfolio. A pity, but not a disaster.
The good part is that I can still handle this situation with calmness and make reasonable decisions about keeping this spread or taking a loss. Things could be different if my position was larger and I lost months worth of work on a single trade.
This reminded me again that trading should not be convinced with gambling or one will end up the as >90% of amateur day traders- with a blown account, but more on this later...

Saturday, April 16, 2011

When Sharpe is useless

During development of an intrady strategy I've come across a case where Sharpe ratio (and Sortino too) is a very bad way to benchmark performance. To illustrate this case, imagine a strategy that enters a position and 'brackets' the exit levels with a limit and a stop-loss order. This results in the pnl distribution as shown in the graph:
The position is either closed at the limit or the stop-loss level, but not in between. Because Sharpe is all about normal distribution, it just does does not work for this case. A better solution here is to use win %, especially in the case of symmetric brackets.

Thursday, March 3, 2011

HOWTO: Signal filtering, the better way.

It is hard to find a trading system that does not employ signal filtering through use of sma, ema, wma  and the likes. I'm not going to dive in detail on the inner workings of these filters as this info is widely available.
Filters that prioritize recent data are more suitable taking market dynamics into account but implementing these filters using a for loop in Matlab is anything but efficient. Luckily, Matlab provides the filter function, that can be used to calculate all these types of moving averages and even more.
In the code attached to this post I'm going to show how to filter signals based on an excellent article written by John F.Ehlers. (more articles can be found here) Please read it before playing with the code. I've implemented the different filters mentioned to compare their characteristics.
 Looking at the data, I must draw a conclusion that the hybrid filter provides an improvement in terms of lag over the standard ema.
Using this hybrid filter one can create a simple indicator of the current price imbalance, like shown in the next graph. For this I've used the difference between the current price and emaHibrid(price,0.85,0.3)


Source: demoFilter.m,  emaHybrid.m data: price.mat

Monday, February 28, 2011

Gaps, part 3

Recently I've come to realise that I've made quite a mistake calculating the 'fill' ratio for the gaps. It's time to correct it, before somebody will embarrass me in the comments ;-).The problem is that I've used asymmetric boundaries when calculating the  statistics. By doing this, the statistics shift in favor of the nearest stop. The closer the stop, the higher the chances of hitting it, but by no means does this mean profit. And previous close is usually not far from the open so chances of hitting it are quite high.
To make statistics fair, I've set the levels symmetric around the open, like this:
gap = open-prevClose
winLevel = open+gap
lossLevel = open-gap

With this calculation* the chances are quite different and closer to my results using the intraday data:
down gap: 52% fill
up gap: 48% fill

*geek note: when both win and loss levels are reached for the same day, it is counted both as win and loss (with daily ohlc data there is no way of knowing which one was reached first) . Data for SPY Feb.2000- Feb-2011.

Code: dumbGaps.m

Sunday, February 6, 2011

Closing the gap, part 2

I felt the need to verify the calculations in the previous post, just to be sure no coding error was made, or at least not a serious one. To do this, I've rewritten the strategy using vector operations rather than 'for' loops and I'm happy to say, the result still stands. Sharpe is a little lower at 1.13, but the curves are quite similar.
To make this post a worthy follow up, I've also included the code:
dumbGaps.m , download_hist_yahoo_data.m

The following remarks are still very appropriate:

  • It is very hard to get a fill at the opening price.
  • Results are frictionless.
  • All this is just a basic concept

Your feedback is very much appreciated.

Friday, February 4, 2011

Closing the gap strategy

Fading the opening gap is a well-known strategy, promising acceptable results with minimal efforts. A great article about trading gaps is written by Scott Andrews. It sounded interesting, so I've decided to spend an hour or so to check if the numbers are right. The basic 'Dumb gap' strategy that I've simulated always trades the opening gap in the direction of the previous days close. In other words, if opening above yesterdays close, go short. Take profit level is set on yesterdays close, if it is not reached, the strategy  closes the position on the days close.
As a test set I've chosen past 10 years of the SPY, ignoring transaction costs and slippage. And the results are:

  • The gaps fully closed on about 75% of occasions
  • The strategy has a sharpe of 1.3
  • Using 'BLUD' (below low of an up day) compensation introduces only marginal improvement. These days are quite rare, ~3% of all occurences.
And as a picture speaks a thouthand words, here are the pnl curves.
Disclaimer: this is just an idea that that needs some work before a tradeable stragegy is achieved. I do not recommend anyone trading using the rules described above.

Monday, January 24, 2011

Nearest neighbour classification

I've been following trading the odds blog  for some time now. It surprised me how well  Frank was able to predict the short-term movement of the S&P500. At first I was quite sceptical about the predictability of the directional stock movement, but had to admit now that there is some sense to it.
Previously I've been playing around with some techniques from pattern recognition field, especially the 'nearest neigbours' approach. Just as one could get an impression of somebody by meeting his friends, the same can be said about stocks. By finding some 'examples' most related to an 'new' situation, a statistical picture could be made of the things to come.
To provide a simple illustration, I've taken the intraday SPY and TICK data for 2010. Some traders are using the first hour of trading to decide what kind of strategy they need to implement for that situation. But is the first hour of trading representative for the rest of the day? It turns out it is. Take a look at the chart below.
Here I've classified the intraday SPY data based on the first hour of the TICK reading. For this I've used the mean and standard deviation of the 30-sec TICK-NYSE data.  Green lines in the chart represent ten days with minimal mean and minimal std of the TICK in the first hour (closest to [0,0]). Blue lines are days with large positive mean of the tick and  low std ([max, 0]).  It turns out that days with large positive bias int the first hour of trading have a quite strong negative bias for the rest of the day, who would have thought?
Predicting the market regime for the rest of the trading day is not very profitable by itself, but can be very well used for choosing the right strategy.

Saturday, January 15, 2011

XVIX performance

In a reaction to my previous post about the VXX-VXZ combo, a kind reader has pointed me to the newly launched  UBS XVIX  etn.  From the first look it does not seem to be that liquid, so its ability to track the VXZ-0.5VXX is somewhat questionalble. 
I've decided to take a quick look at the fist month of its existance to check if it is really performing as advertised.


In the graph I've plotte the cumulative change in VIX (index), VXZ, VXX, XVIX and a synthetic VXZ-0.5VXX pair.  It seems that XVIX performing exactly as promised, providing performance very close to the underlying pair, without the cost of rebalancing. 


The following plot shows cumulative tracking error:
An interesting observation is that it does have some occasional tracking errors, of about ~0.3%, providing some arbitrage opportunities.

Monday, January 10, 2011

What if S&P 500 lost 50% on a single day?

Most of us know that there are leveraged ETFs, providing up to 3x daily exposure, in decimal percent. Usually these etfs  perform just as advertised, but imagine a situation where the index looses a very large portion (~50%). Such an event unlikely, but is not impossible if you think about the 'fat tails' and ~20% loss on black monday in 1987.
Now imagine you are unfortunate enough to own one of the 3X leveraged etfs on while S&P looses 50% of its value. will 3x exposure result in a 150% loss??? Ok, you probably will face a margin call before that, but I'm having a hard time trying to imagine what would happen to the leveraged etf price on such a day.
What do you think?

Wednesday, January 5, 2011

Yahoo quote downloader

Here is a simple function to get current yahoo data for a bunch of symbols : get_yahoo_quote.m
It downloads quotes from yahoo (take a look here at what is possible) and returns them in a struct.

example usage:
quote = get_yahoo_quote({'SPY','IWM'})     

result:
  

           symbol: 'SPY'
             desc: 'SPDR S&P 500'
        lastTrade: [127.5695]
    lastTradeTime: '3:30pm'
     dividendDate: 'Dec 17'
             open: [126.58]
        lastClose: [126.98]

           symbol: 'IWM'
             desc: 'iShares Russell 2'
        lastTrade: [79.26]
    lastTradeTime: '3:30pm'
     dividendDate: 'Dec 22'
             open: [78.4]
        lastClose: [78.422]