Monday, March 25, 2013

Scala and Functional Programming Class

In a previous job I self taught myself Scala and Actors model using Martin Odersky's book.  I haven't used it in a while, but I have been itching to learn proper idiomatic functional programming.  Scala lets you use both OO and FP principles, so it easy to revert back to Java-style OO when not pushed.  I feel like Scala is a good language to learn because it straddles that line between being a "cool" and productive language but also adult enough to scale and taken seriously by large enterprises (i.e. you can find a good job).  Moreover, its sweet spot is concurrency with actors and STM, which allows horizontal scaling over many cores and machines with minimal effort.  From what I can tell developing concurrency correctly is one of the strongest skills a programmer can have over next five years.

That is why I jumped at the opportunity to jump on a free online class taught by Odersky himself at Coursera.  Today is the first day, so sign up now if you interested in getting "credit".

Installing OS X Mountain Lion on VMWare Workstation 9

Over the weekend, I was doing some iOS programming on my Macbook Air and was getting annoyed by the small screen size and constant switching between my laptop and my desktop screens.  The obvious solution would be to buy a monitor cable to hookup my laptop to one of my monitors, but since I have been using virtualization more, I became curious if there was a way to run OS X as a guest on my more powerful Windows 7 machine.  I already have moved most of my Linux-based web programming to a headless Debian virtual machine (which works splendidly with puTTY and samba).

OS X guests are not officially supported on PC hosts such as Windows, however since Apple has since migrated to an Intel architecture, it should be theoretically possible.  A quick google search revealed that a benevolent group of hackers at the the SoulDev Team have already put together a painless solution.  Here are the steps I had to take to get this work sufficiently:

Before you try this, you need to make sure your machine is powerful enough to do virtualization.  If you bought or built the machine in the last couple of years mostly likely you are OK (my previous machine which was 8 years old could run Debian and barely Windows XP guests on VMWare Player).  You just want to make sure you have hardware-assisted vitrualization support in your motherboard.

My machine is an Intel i5-3570K (ivybridge) with 16 gigs of RAM running Windows 7 64-bit.  The souldev team posted instructions on how to get this running on an AMD architecture if that is what you have, but I believe the result is less reliable than using an intel machine.
  1. First you need to get the OS X image from thus torrent link posted on their download page (note that you should have a valid license for OS X before you do this legally speaking)
  2. When you expand the z7 file, you will see three directories.  The folder named "OS X Mountain Lion" contains the vmware image files, which we are going to use.  So, move these files to the directory where you want to store the virtual machine.  The instructions recommend that you run an "unlocker" first from the third directory which unlocks the "Mac OS X" operating system option in the VM settings, but I ended up not running it due to security concerns.  Note that I am unsure if bypassing this step will work for for every machine or I was just lucky.  It also might degrade performance slightly, but I am also unsure about this.
  3. Start up workstation and open up the vmx file.  Adjust the amount of RAM available to the guest to at least 2 GB for decent performance.  I toyed around with increasing the number of processors but didn't see much of an improvement, so I left it at 1.   After starting the VM, tell workstation that you "copied" the virtual machine from another location.  During the first bootup, it is pretty slow.  You will probably see a grey screen for a few minutes followed by the OS X "computer setup" screens.  Here, you probably want to skip over the part about setting up iCloud and registering the copy.
  4. Once it fully boots up, you probably want to install VMWare tools.  The freshest copy of the tools are at this location.   Inside the guest, download "Install VMWare" and run it.  This will install VMWare tools for your OS X guest.  After a restart, I also installed the file VMsvga2 from the same location.  This dramatically improved my graphical performance on the guest and allowed my to choose display settings that were native to my monitor.  I am sure the success of this is dependent on the machine specs you have, but I found it to be the determining factor between a neat experiment and actually usable.
At this point your guest is almost fully functional.  I was even able to upgrade to 10.8.3 using the app store (you should make a snaphot before you do this) and download XCode.  The only thing that I believe there are issues with is getting iMessage to work properly.  You can read up on it this post if that matters to you.

I hope that was helpful even though it is pretty straightforward.

Sunday, April 29, 2012

Programming Shortage

I ran across this article from TechCrunch, which states that there is a software engineer shortage despite favorable characteristics for programmers salaries (low unemployment and potential for huge gains).

As a programmer by trade, I find this articles really annoying. Ignoring for the fact that start-ups are probably in a valuation bubble about to burst (e.g. Instagram is a huge outlier); what they are really saying is that "there is a shortage of talent ... for what I am willing to pay".  According to econ 101, if you need more supply just increase the price.  What these (start-ups I am guessing) really want it some rock star to implement their ideas for peanuts.  However, reality bites them in the butt once they realize they are competing with Google and Facebook salaries, so they complain they "can't find anyone" and should increase "H1-B" visas (i.e. cheap labor).

The real story is that most programmers won't be working at a hot start up or see a big pay-off and the lessons of the early 2000's are still fresh in people's minds.  Moreover, most Americans don't find programming sexy - it is too socially isolating and logically complex despite the cool factor of web/mobile start-ups.  Therefore, the only real way to attract more talent is better salaries and treating software engineers more like lawyers and less like construction workers.

Sunday, April 22, 2012

Fade Opening Gap SPY Trading Strategy

I mentioned in a previous post that fading the opening gap can be very profitable on certain indices and securities.  Here is one of the best examples - the SPY ETF.  Using a very basic strategy of buying or selling in the opposite direction of the opening gap and taking profit at previous close (or closing out at the end of day if the target has not been reached) shows this equity graph (using Yahoo! finance data and compounded returns)

As you can see, this is a very profitable almost every year especially post-2008.  Note there are many ways to improve upon this strategy (to reduce risk mostly) but the basic one works pretty well.  Note that this does not include any trading costs nor slippage which can occur at the open despite SPY being very liquid.  However, let's look at the risk of the strategy.  Here is a graph of the draw-downs from the equity high water mark.

The max draw-down was only 20% and that was in the depths of the 2008 crises which is not bad considering that the SPY dropped +50% during that period.  Post 2008, the draw-downs have barely made it pass 5%.  The Sharpe ratio of this strategy over this thirteen-year time period is about 1.19.

Edit (03/25/2013):  After looking at this further these numbers are mostly bunk based off bad high-lows from historical Yahoo! Finance data.  This strategy is still somewhat viable (you need to be more selective) but not nearly as profitable.

Monday, April 16, 2012

GAZ and ETNs

There has been a lot coverage of the big fall of TVIX an ETN that "supposedly" tracks VIX futures.  GAZ is another ETN one should be wary of.  It is supposed to track natural gas futures, but currently trades at 4.00, but its NAV is 1.89, a premium of 112%!  Here is a 3 month chart of GAZ vs UNG, notice something unusual?

So when dealing with these exotic ETNs be careful.  I personally believe there are opportunities to make money in these for trading purposes but not so much for investing.  Look how someone would have fared if they bought VXX for long-term "hedging purposes", yikes!  Although to be fair, one could reasonably compare this to the premium on an LEAP option that expired worthless.

Monday, April 9, 2012


One thing that isn't intuitive to novices is that shorting is not completely symmetric with being long.  Other than the restrictions and additional costs that occur with shorting a stock, you also suffer from the fact that you do not compound returns when you go short.  What I mean by that is that your position gets shorter when you are winning and longer when you are losing.  As an example, if you shorted a stock at 100 and it went to 10, you would make 90% on your position, however if you bought a stock at 10 and it went to 100 you would make 1000% on your position - not a fair deal.  What's worse is that if the position starts moving against you, you become more at risk.  Ways to overcome this characteristic of shorting is to rebalance your position or use an inverse ETF which implicitly rebalances for you (usually daily) but is usually cheaper due to less transaction costs.

Saturday, April 7, 2012

SPY RSI Trading System

Here is a backtest on a daily mean-reversion trading system inspired by this post.  The rules use a simple countertrend-based system using the RSI and SMA.  The goal is essentially is to buy the dips in an up market and sell the spikes in a bear market.  First, we have a 200-day SMA average as a filter to determine if we are in a bull or bear market.  If the price is above the 200-day SMA we use "bull market rules" and vise-versa.

The bull-market rules are buy at the close if the 4-day RSI is below 35 and close the position if it is above 55.  The bear-market rules are that you go short if the 4-day RSI is above 80 and close the position if it is below 50.  I included transaction costs of 0.01% per trade to make it slightly more realistic.  The data set is from 01/02/2001 to 04/05/2012.

It is interesting to note that most of the over-performance is post-2008.  This is likely because the system managed to avoid a big losses during the crises.  Some additional stats: Sharpe ratio of 1.036 and max drawdown of 9.89%, so it would not be unreasonable to lever this up a bit if one wanted to be slightly more aggressive.  The downside of this strategy is that I picked some slightly optimized in-sample parameters which probably makes the performance over-optimistic.  Second, this strategy seems to do well in choppy markets, which could be abating in the near-term.