Wednesday, June 29, 2005

The Gamma-Trading experiment.

I have no big positions for July expiration. I'm just sitting here mostly in cash, waiting for the next buying opportunity. It sucks that I have few positions for the July expiration coming up, but I want to be patient and only sell puts when conditions are favorable for that type of trade. I feel like better buying (naked put selling) opportunities are ahead. I closed out my RIMM puts at $0.20 today ahead of the earnings announcement. This is a $0.63 profit per put. I would have liked to have heald out for the remaining $0.20, since I probably won't put that capital back to work anytime soon. Too many times I have waited to make an extra 10 or 20 cents and ended up losing money. I'd feel pretty stupid If I lost a buck tomorrow to make 20 cents. PALM also reports after the bell today (or is that tomorrow). Anyway, I'm not covering my PALM puts, as they are farther out of the money and still retain about half their premium (these are NOV puts anyway.)

I bought a SPY 119 straddle on Monday I think (maybe it was Friday). I rarely buy straddles. This is mainly an expiriment, so I only bought one straddle. The market seems complacent. Regular readers know I usually like to sell premium...particularily naked puts. I haven't seen any options I've really wanted to sell lately, so this seemed like a good time to experiment with a different type of position while I wait for the time to put on big positions to come around. According to McMillan on Options, volatility often reaches a seasonal low in June or July. Furthermore, the market seems especially complacent right now. Complacency often precedes big moves. The complacency can be measured with the VIX index. This is a measure of the implied volatility of the S&P 500. It has been fairly low for some time, and even though the market sold off fairly hard last week, the index hardly moved at all.

I originally bought the straddle for the reasons explained above, but I am also thinking of using the position for expirimenting with" gamma scalping". To explain what that means, I'll have to get into the Black-Scholes model. The Black-Scholes model is a differential equation that gives the price of an option as a function of the stock price (or the price of any underlying instrument), time to expiration, the strike price, future volatility, and the risk-free interest rate. If you take the partial derivitives of the equation with respect to each of the individual parameters you get what is called "the greeks". Each greek tells you how the option price will move in response to a unit movement of the respective input parameter.

Probably the most important greek is the derivitive with respect to the underlying stock price. This is called Delta. Delta is the amount the price of the option will change when the stock price changes. If a position has zero delta then it is said to be "delta-neutral". For example, an exactly at the money straddle would have a delta of 0 since the put's negative delta would exactly cancel out the call's positive delta. Gamma is the second derivitive of the option price with respect to the underlying price. This tells us how much delta will change as the underlying stock price changes. Gamma can be thought of as the profit potential to be gained from hedging the delta of an option position. Theta is the derivitive of the option's price with respect to time.

For example, when I sell naked puts I have a negative gamma and a Positive theta. This means that the passage of time tends to be profitible, but if I want to hedge my delta as the underlying moves (to protect myself from large losses), I will tend to lose money doing so. A Straddle has negative theta, and positive gamma. So here is how scalping the stradde's gamma works:

Assuming the straddle was exactly at the money when purchased, it would have a delta of zero. Then if the stock moves in one direction, one could compute the delta at that time, and then buy or sell a quantity of stock to bring the delta back to zero. An option will always have a delta between -1 and 1. A share of stock always has a delta of 1. As one buys and sells stock in this way to bring the delta back to zero in response to the movements of the underlying stock, then this will tend to result in buying high and selling low, producing small profits over time. It's important to remember that even as one may be profitably scalping gamma, those profits may not be enough to overcome the time-decay (negative theta) of the position. It's kind of like a race between the gamma trader and theta. If the stock isn't volatile enough to profitibly trade the gamma enough times before expiration, then the trader will lose the race.

Yesterday the SPY's moved up about a buck. This morning as a result of that move my put contract was down about 40 bucks, and my call contract was up about 40 bucks. According to Interactive Broker's trading software (TWS), The delta of my position was around 30, so I sold 30 SPY shares short to bring my delta to zero. Now, if the S&P moves down again, I will have to buy back some of those shares I sold short to bring the delta back to zero which would result in a profit. I plan on re-adjusting the hedge every time SPY moves about a buck. With such-infrequent adjustments, I doubt I will make enough money to overcome time-decay, but I am curious how much can be made. The danger of hedging the delta like this as the stock moves, is that I may give up substantial profits if the stock makes a big move against the hedge.

Thursday, June 23, 2005

Private property rights died today.

Normally I don't write about politics but the supreme court ruling on eminent domain today, really pisses me off. Briefly, the court ruled that cities have the right to take your land, and sell it to someone else if they think the "someone else" will pay more property taxes than youd do. In this particular case the city of New London, Conn. is trying to take these people's old victorian homes which are located on riverfront property and sell it to a big developer to build luxury condos, an Office Complex, and a Health Club on it. Their justification is that the new property owners will pay more property taxes than the current owners of the property do.

It almost makes me glad that I'm not a homeowner. All of my property is paper assets and intellectual property, rather than land. No city-council is ever going to want to take my QQQQ LEAPS and build a Costco on top of them. Nor do I have to worry about my software copyrights being confiscated so that Donal Trump's can build a limo parking lot on top of them, but all of you people who own actual physical property should be worried and pissed off!

UPDATE: I found this nice post on slashdot. (For the record I thought of this possibility too.):

And that creates a new way around California's Proposition 13 (which keeps them from raising property taxes on your house and land until it sells). Watch for this:

1) Emminent domain the tax-capped house.
2) Sell it to another buyer. (Taxes now at new rate.)
3) Previous owner has to buy a different house. (Taxes now at new rate.)

UPDATE: Here's a post I made on EliteTrade. I definately think that the poor and the middle class get hurt the most by eminent domain, but here's a plausible scenario for how the rich could be hurt as well. Unless one has Gates/Buffet/Soros quantities of wealth, most "rich" people are still "poor" compared to the resources and power of a publicly traded corporation.

... there are lots of beautiful beach-front properties in California with mansions on them that would be far more valuable if it they were developed into condos instead. A developer could easily turn a 29 million dollar beach-front La Jolla estate into a two-hundred million dollar condo complex, selling 2 bedroom condos for a million and a half each. The only thing stopping it is zoning and the willingness of the rich guy to sell his estate.

I could imagine scenarios where a big corporate developers worth 100's of millions of dollars might lobby government to kick rich guys off their land, tear down those mansions, bulldoze their sprawling gardens, and build high-density beach-front luxury condos for the "good of the public". The political power and resources of a rich guy worth tens of millions of dollars, though impressive are still no match for the money and power that a publicly traded corporation like KB homes could muster. When government has this kind of power, no one is safe.

George Costanza Investing

Remember that episode of Sienfield where George Costanza realizes that every instinct he has ever had has been wrong...that ever decision he makes is doomed to fail? So he decides to begin doing the exact opposite of everything that seems right to him, and immediately his life turns around. He gets a girlfirend, a great job, and starts to become successful. Sometimes, this is what I think trading as a contrarian is like. So often when a trade feels like a great idea it's absolutely wrong. Often my best trades feel so stupid, risky, and wrong as I'm making them. I need to get better at embracing my inner Goerge Costanza when I trade. It seems like so often every instinct I have is absolutely wrong and I need to do the exact opposite of my impulses!

Case in Point: The Market finally declined today. You'd think with those in-the-money QQQQ Puts I'd be pretty happy about this? Today, when I logged in all of my stocks were up, my account near at all-time highs and I'm thinking to myself "What the hell am I doing shorting this market? I must be insane. I hope I can get out of these puts without losing too much money". So, I put a limit order in to sell them for $0.18 below what I bought them at after commissions. A while later after taking a shower I look at my screen and see that I no longer have a position. For a brief moment a wave of relief washes over me that I was able to get out of these things for such a small loss. Then, I noticed the Q's were down a fair amount. I look over at CNBC and I see the Dow is down big, and the Nasdaq is holding in there, but has still had a considerable drop while I was in the shower. It looks like I sold into the very beginning of today's downdrop. This has to be the dumbest trade I've made all year. Those puts have made me nervous ever since I put the position on...in retrospect I should have recognized my nervousness as a good sign.

Remembering back to April when we had that huge sell-off. I remember the one day the market was up, and how relieved I was to be able to establish bullish positions into TWX and UNH before the market "rallied without me"...only to see both selling for lower every day that week. I also remember how I bought(well sold puts short) on almost every down day there-after and how it almost physically hurt to make some of those purchases, most of which turned out to be great trades. (The bad one GS...was bad because I panicked later and closed too soon, not because I didn't buy at a good price that week. ) I also remember how later in May how relieved I was to cover May GS put shorts...which it turns out was at the absolute worst price I possibly could have covered at prior to expiration. To make matters worse they even recovered their strike in time for expiration, days after I covered.

It's so hard to ignore my instincts. Every time I feel relieved at selling a position at the bottom...I usually realize at the time that I'm probably selling at the bottom and yet I still feel great about how I'm "limiting my risks by using stops". When I chase a stock up because I'm afraid I'll miss my chance to make money...I almost always realize at the time that I'm over-paying yet I do it anyway. Somewhere there is George Costanza in me and I need to find him if I'm going to continue to trade on a purely discretionary basis.

This further emphasizes the need for developing more quantitative methods for picking my positions so that I can remove my instincts from the process entirely. Even the basic techniques I use for picking puts right now help me immensely. No matter how much I like a stock, I only sell OTM puts with high theoretical odds and a 10% anual yield. This weeds out many of the non-contrarian plays. Puts get a high yield and high theoretical odds for a simple reason: Most people don't want to sell them so the price rises. When I find puts with those characteristics and short them, at least I know I'm not trading with the majority. This alone isn't the holy grail. Often the majority is avoiding the position for good reason, but at least it's a start, and has prevented me from getting slaughtered with the herd a few times now.

Tuesday, June 21, 2005

Low Implied Volatility in CSCO

I was tempted to close my QQQQ put trade several times today, for no good reason other than that I just feel bullish. Today I noticed the guy at Hedging Options wrote " Unbelievably, CSCO atm July 20 calls are at a 15 IV", so I loaded up CSCO in my new program for ranking a stock's past historical volatilities. I set it to load the past 1000 trading days:

Current Historical Volatility (as of June 17th)
20 day: 19.64%
50 day: 20.22%
100 day: 20.5%

Past Historical Volatility:
Rank % 20day 50day 100day
0th 13.01 18.8 20.5
10th 21.65 23.59 26.86
20th 26.85 28.81 31.01
30th 29.64 30.65 33.28
40th 33.1 33.75 34.25
50th 36.69 37.37 35.03 <--- this is the median
60th 43.24 40.53 38.13
70th 49.78 54.85 57.83
80th 57.82 65.41 66.19
90th 69.26 72.26 68.73
100th 107.76 83.09 72.44


The 50th percentile ranking is the median volatility. The program confirms what hedging options said...those CSCO calls are trading well below typical volatilities for CSCO. The guy at hedging options uses more complicated strategies than I usually do. I think he generally enters delta neutral positions with positive gamma, and then scalps against the gamma. I don't have any experience with that and am not interested in giving it a try at this point. Just as an experiment though, I bought 1 July 20 Call contract for 30 bucks just for the hell of it. Let's see if it goes up now! (This isn't exactly a high probability trade.)

If I ever get the option data imported into my database I'll do some real expiriments. I'm really interested in backtesting some long gamma, long volatility strategies as well as building a useful screen for finding puts to sell. My copy of McMillan on Options just arrived from Amazon.com. McMillan's other book Options as a Strategic Investment is hands-down my favorite options book. It's almost always on my desk, so I'm really looking forward to reading this book. I'll post a review when I'm done.

High Oil is Bullish!

High Oil prices are a sign that the economy is healthy. All of the oil in the world that can be pumped is getting pumped right now. Eventually the high prices will create incentives to conserve and produce more capacity, which could reduce prices...but that is a long-term thing. There's only one thing that will bring Oil back into the low 40's or even lower in the near future (the next year or so). A recession. As long as there is economic growth in oil consuming countries (like ours), Oil's going to stay expensive. So when you see Oil futures surging that is a sign that the markets forsee a healthy economy. Be happy that it costs you 50 bucks to fill your tank. It's a good omen for the future!

Monday, June 20, 2005

If I feel great about the market, does that mean I should get short now?

I bought some in-the-money QQQQ July 42.5 puts today. I feel really good about my portfolio. It's at all time high's. Just about every stock I own in my long-term portfolio today was up quite a bit...SHLD, CMCSA, even LU! When I feel this good about my long-term stock holdings often this means it's time to short the Q's. I hope I'm not shorting too early here. Most of the time I've been actively trading has been during markets where this type of strategy has worked well. (range-bound and bear markets).

I hope past success hasn't conditioned me to get hurt by shorting an extended rally. I do feel bullish long-term, but my gut tells me to get short for a counter-trend. This is a very small position. However, one of the happy consequences of writing this blog is that I now pay more attention to my small side-trades. One thing that has become apparent under this increased scrutiny is the small really losses add up. Since my main strategy (selling option premium) is to take lots of small gains...I need to be careful about allowing myself to take too many small losses as well while trading these random "gut feelings" I get.

I got all of my stricknet.com options data imported into my database by Friday night. The next step here is to compute implied volatilities for every price-record. Saturday, I wrote some routines to work with the Black-Scholes equation in C#. The appendix in Option Volatility & Pricing by Natenberg was especially invaluable for this. This was my main source for this undertaking. The information in this book's appendix was refreshingly complete and concise. I also cross-referenced with Options as a Strategic Investment by McMillan. One of the things I love about McMillan's book is he throws in so much practical stuff for programmers...not just formulas and theories, but actual sample data and sample step-by-step calculations so you can check your work. This was key for me while debugging my code.

Next, I needed a source of historical interest rates. (The "risk-free" interest rate is one of the variables in the Black-Scholes equation.) The daily Fed-Funds rates are available at the federal reserve's website. I wrote a parser to pull this data into my database. Now I was armed with everything I needed to compute IV's for each of the 83 million individual option price records.
At this point I started to get excited. Could it be that in another few hours I would have 3 years of historical options data for backtesting at my virtual finger-tips? I wrote an sql query to query all of the variables to compute IV, and executed it to make sure the syntax was right. I waited about an hour and it still wasn't done running, so I decided to check this out in the MySQL query analyzer. Well without getting into too many more boring details, it turns out the query was very bad. I bet the universe would end before it was done. So I decided to add 2 indexes that I thought would help matters considerably. I added one to the daily equity prices table, and one to the daily option prices table, and then I waited, and waited and waited and waited. Then finally, Sunday night after I had gone to bed, I walked past my computer on the way to the kitchen to get a glass of water, when I heard a little "beep". Just at that moment, it had finally finished computing the index on the equities price table. This table is considerably smaller than the option price table and it took what? over 48 hours maybe?

Well, it doesn't even take that long to import the options data in the first place. So I decided what I'll do is delete the options data, add my index to the empty table, then re-import all of the data. I am going to write code to compute IV's and put this in the database along with the data to save myself having to re-read all of the data later. I think this will save me a lot of time in the longer run. I've also decided to bite the bullet and set up a separate database server, so that in the future I can run long operations without slowing down my main working computer.

Holy crap! What a pain in the ass this has been! I thought I was used to working with fairly large and complicated databases with my main business. These were nothing compared to this option price data. One pleasant result is that while waiting for the re-indexing to complete I wrote a program I have been meaning to write for some time. On page 800 of Options as a Strategic Investment by McMillan, he writes about a technique for ranking the historical volatilities of a stock, to get a picture of where current volatilities are in relation to the stock's past volatility behavior. This can be easily implemented using the AdjustedClose field from the historical stock market data at finance.yahoo.com. I wrote a neat little windows-forms app to do this analysis using the historical yahoo data I allready have. I will write more about this program, the technique, and what the next steps I hope to undertake to enhance it, in a later post after I find a place to host some screen-shots.

Friday, June 17, 2005

June Performance

I had a good day today. Most of my stuff was up. My last position expiring in June was DY and it was even up a little. The options were illiquid, so I decided to try something new for closing this position. I shorted DY at 19.23. When I get assigned shares at 20, they will cover my short and lock in a loss of $0.77 per share. I collected $0.39 of premium per share so I believe I have locked in a loss of only $38 per contract. This was my only loss this month. I'm very happy that it was only 38 bucks. At times this contract was selling for 150 or so.

Remember those "short-squeeze" plays I entered into because of a Cramer article? Well I sold my TXCC common a while back for a push (0.02 per share profit). I probably would have sold my in-the-money MLNM options too if the contract had been more liquid, but the bid-ask spread was huge and the stock wasn't doing much so I opted to just wait until closer to expiration when hopefully the contract might become more liquid. Well I woke up to a nice surprise today. MLNM was up over a buck. Maybe the short-squeeze I've been waiting so long for is finally starting...

Here's a quick-and-dirty run-down of the June short put Portfolio (commissions included):

MOVI: +$33
NVDA:+$43
MO:+$74
GS:+$84
CBH:+$49
ONXX:+$59
DY:-$38

With hedging and transaction costs I earned aproximately 2.05% on margined capital. This would be a 19.9% annualized (non-compounded) return. Had DY expired worthless, I would have made around 2.63% ROI instead. This would have been an annualized return of 25.6%. The maximum return I could have achieved from this portfolio would have been around 2.85% , assuming all options expired worthless without any early position closures (and associated transaction costs). I am pleased with these results. I would be happy if I could duplicate this every month. We'll see if I can.

On Monday I will be seriously underinvested. Expiring in July, currently I only have RIMM 60 puts short @ $0.84 and DNA 75 puts short @ $1.04. Both positions are presently showing profits. I also have NOV 20 PLMO puts short @ $0.84. That's it. Interestingly enough, even though PLMO was up over a buck today, the NOV puts were at break-even. Implied Volatility must be expanding quickly in these options.

Even though I'm underinvested I am reluctant to open new positions. I just don't see anything I'm terribly excited about out there. Allso, I am reluctant to open positions until I have some better option analysis tools. Currently I use these free web based tools to examine and screen options I'm interested in: finance.yahoo.com, IVolatility.com, and OptionsClub.com. These sites are pretty useful, but there are some more specific types of screens and analysis I would like to be able to do which I just cannot do using those publicly available tools.

One tool that I really need is a program that can compute the probability of profit and theoretical odds (according to Black-Scholes) of an arbitrary option contract. The above tools can do this to some capacity, but they are generally screening tools so you have to figure out a screen that will bring up the particular option your interested in. Often I just can't get the info for a particular option to display. Furthermore, in addition to probability of profit, I'd like to compute the probability of the option ever being in the money before arbitrary time intervals. I hope to write a program in the near future to do all of this using Monte-Carlo analysis. The program would ultimately integrate my stricknet.com data, and perhaps realtime quotes through interactive brokers TWS API. I think this could be done in a good solid weekend. I'm so busy though and the San Diego Summers are so warm. The beach beckons.

Thursday, June 16, 2005

A Strategy Adjustment

ONXX was up today. I covered my puts at $0.10 for a profit of $0.59. They'll probably expire worthless tomorrow but this stock is volatile and I don't want to risk a major portion of my profits for the month to make another 10 cents. DY was up nicely today and yesterday too. DY seemed to make an intra-day reversal yesterday. I was tempted then at the low of the day to close DY and ONXX both, so I could be done watching the markets for the week and lock in a profitable month. I'm glad I was patient in this case. DY has recovered a little and I think it may continue to do so. This stock could still cost me significant profits this month, but I decided to hang onto my DY position for another day since the premium was still around ~20+ cents above parity, and because of my hope that DY will continue to recover some of its losses. It's price is close to my break-even price at expiration. I'll probably end up letting myself be assigned DY shares tomorrow, and unload them on Monday if the options don't trade to within 5 cents of parity on the offer tomorrow.

I am in good spirits. Now that I am out of ONXX it looks like most likely I will have a decent ROI for June. I've felt confident that I would come out with a gain the entire month after taking significant profits in MOVI and NVDA early. For the past few weeks though, it has looked like ONXX and DY would cost me significant profits, yielding a low return for the month which would have left me in low spirits after last month's negative return.

I sold short PLMO Nov 20 puts @ $0.85 today. PLMO was up 90 cents selling around 28 bucks. PLMO's implied volatility has been ramping up ahead of earnings which come out at the end of June according to finance.yahoo.com. I'm apprehensive about this play. It seems like the market expects a tumultuous earnings report so I wanted to sell options very far out of the money to give myself a wide margin of error on this trade.

This position represents a small change in strategy. I am trying to move into selling options with longer expiration dates instead of primarily front and second month option contracts as I have mostly done in the past.

One of the surest ways to increase your profits in the markets is to reduce your transaction cots. Interactive brokers commissions are low and these are not the transaction costs I'm primarily talking about. I'm talking about the bid/ask spread which is a minimum of 5 bucks a contract each way. By trading options with longer expirations I will be reducing my transaction costs in two ways. First of all Options with longer times to expiration have larger premiums. Thus that 5 cents will be a smaller percentage of the premium collected. Secondly, I'll probably end up trading less frequently since my capital will be tied up for longer periods of time which will reduce my opportunities to pay transaction costs.

Another advantage of trading options with expirations further out is that my break-even level will be lowered, giving me a larger absolute margin of safety. However, there is a price to pay for selling longer dated options. You just can't get as high a return, assuming the positions are heald to expiratoin. I like to sell options that yield a minimum of a 10 percent annualized yield on unmargined capital. Selling this far out, it's often hard to find that.

I believe though that my yield may actually end up being higher than initially expected selling options farther out in time. If the stock moves in my favor sometimes I can exit the position very early and take most of the profits over a shorter period of time. For an example of this I have my recent NVDA trade. Earnings came out shortly after entering the position and the puts became next to worhtless very quickly. This was not only do to the favorable price-movement. The position also benefitted from the volatility collapse after the news event. I think I only heald that position for like 4 days instead of the 30+ that were left on the expiration. This gave me a much higher yield on the capital risked in that position than was originally expected. (Yield is profit divided by time)

For PLMO the only July puts today worth selling seemed to be the 22.5's. They could be sold for $0.30, and represent really high theoretical odds as well aa very nice yield on an annualized basis. Selling those short would be a break-even level of $22.2. With the stock trading north of $28.00, that's pretty far out of the money, but these PLMO options remind me of last months NVDA, MOVI, ONXX, and DY trades. There's a reason they look so attractive. They are probably riskier than they seem even though they are almost 6 bucks out of the money.

By selling the Nov. 20's@ 0.85 insted of the July 22.5's @ 0.30, I'm giving myself room for the stock to move almost 9 points against me instead of just 6. If the PLMO earnings are bad it will probably drop quite a bit and just sit until the next big bad event. The unfavorable price movement will increase the value of my options which will be bad, but hopefully they won't be in the money and that will allow me to sit tight and wait a few months for time-decay to work in my favor since there probably won't be as much movement after the earnings related movement settles down. Hopefully I'll get my expected 10% return assuming I've given my self enough of a safety margin. Furthermore I'll get some help from the post-event volatility drop. If PLMO rallies or stays the same, the options will lose a lot of their value right away due to this drop in volatility, and I will probably be able to cover them sooner than November, and earn a return better than 10%, assuming I am able to redeploy the capital elsewhere between now and then.

Wednesday, June 15, 2005

Better Living Through Code Optimization

Well, I think I optimized my option data importer enough to be practical this last weekend. This turned out to be quite a chore. Some of you may wonder why speed was even an issue at all. I mean how long can it take to copy 6 megs of data from one place to antoher? The process of imporation was not mearly just one of moving data around. I use a more complex structure to store my data than the form which it arrives in from stricknet. When designing a database it's important to use a good "structure". When you have a well-structured database you find that it is easy to extract information from it. It will also be easy to intigrate new types data into the database at later times. Extracting information from a poorly-structured database is often such a chore that useful information never gets used or even discovered.

Among other things, a well-structured database, stores each fact only once. One reason for this, is to reduce over-all storage requirements. An even better reason is that it makes it easier to maintain consistancy across a database. As information changes and gets updated, you only have to update it in one place. If your storing the same information in multiple places, great care has to be taken to update it in all of those different tables and records. Furthermore there is a tendacy for information get lost due to uncertainty introduced to the database from inconsistencies resulting from incomplete updates.

Here's a practical example of what I mean. The data from stricknet arrives as two tables. In the equity table, you have rows that look like this for each trading day:

Symbol, Company Name, , Open, High, Low, Last, Volume

AA,Alcoa Inc,,29.99,30.0,28.64,28.7,6591100

It would be very easy to import that "raw" as is. One obvious problem with that though is that every day I will be writing the symbol and the company name into my database. Why do this for every date record? The symbol and company name don't change on a daily basis. Instead I have a table called "symbols", which looks kind of like this:

ID, Symbol, CompanyName

Then I have another table called "dailyequityprices" that looks something like this:

SymbolID, Open, High, Low, Last, Volume

So, when I import the data I first check if there is a symbol record for "AA", and if there isn't I write the record to symbols just once. Then for each date record I store just the id of that symbol record instead of storing the text for the symbol name and the company name. Not only is this more efficient, but now the equity data is cross-referenced to all of the data I allready have on alcoa in my database, because all of my existing data also references the same symbol record. For example, I allready have earnings data, split data, dividend data, etc.

This cross-referencing tends to multiply the useful information in a database. When I import the daily price facts I haven't just imported the price of a particular stock on a particular day. I've created new informaion as well, through the cross-referencing to other related facts which allready exist in my database. As new facts are imported into a relational database, the total information tends to grow faster than a linear rate if the database is well structured.

Another practical benefit that I mentioned earlier is that if I have to change some information I only have to change it in one place. For example when the symbol for the "QQQ" changed to "QQQQ", I didn't have to change every price record, every dividend record, and every split record in my database. I just changed one symbol record.

I used to regularily solve optimization problems years ago back during my short career as a game-developer, but it has been awhile since I've needed to undertake this type of chore. I guess it is like riding a bycle though. You never forget. Here are the general steps I use for optimizing code:

Step 1: Write bug-free, easy to understand, intuitive code. Ignore optimization issues.
The first step in writing optimized code is to not even try. You need a solid foundation on which to build the optimized code. If you try to optimize from the very beginning of a project, more than likely you'll only add complexity and bugs, without much of a speed increase. This is what I did. I wrote a very simple importer which imported the data one row at a time, while reading from the database before writing every single record to make sure that duplicates were not introduced and that the consistancy of relationships was maintained. Not all of these checks needed to be done for every record, but this was the easiest and most intuitive way to do it. I err on the side of caution, by not eliminating checks that might be redundant.

So after writing my simple implementation of the importer, I found that it took well over an hour to run. I never actually ran it to completion for even one day of data (From my retrospective measurements, I now know it would have taken the better part of a day.) It simply took too long.

Step 2: Measure everything.
Often ineficiencies in a relatively few lines of code will account for the vast majority of the slowness. If you don't measure, you waste a lot of effort optimizing lots of code, with little effect on execution time.

The data comes in a zip file, which I decompress on the fly as I read the data in. Decompression is a slow process. One of my first ideas for optimization was maybe I should decompress the zip files in batch ahead of time. So the first thing I did was time how long it takes to just decompress the zip file the way I was doing it. To do this I disabled all of the parsing code and database access and ran one file through. It took less than 0.8 seconds. Hmm..maybe the parsing is slow. So I enabled that as well. It still took around 0.8 seconds to execute. So then I knew there were no easy optimizations. To get a baseline, I disabled all but the simplest database operation...symply checking if an equity symbol allready exists for each line of text in the zip file. So I executed this, and it took over 4 hours and was still not done. Wow! what an eye opener.

Well, one way to speed up database record retrievals is to add strategic indicies to your database, so I added indicies throughout my database. Then I enabled everything and ran the importation again. It was still taking forever.

So, next I did an experiment. I ran a query on an empty table for every record in my options zip file. I would expect the actual sql query should run in a trivial amount of time. This took like 10 minutes to run. The lesson here was clear. Merely opening a connection to the database and transmitting query code is a significant process. I now knew what I would have to do. Batch all of my database operations. For example to query the database and return 1 option symbol record, takes about the same amount of time as to query all 140000+ option symbol records. So instead of doing all of those one at a time for each record in the zip file, I needed to read all of the information ahead of time.

In order to batch all of my database operations, and still ensure consistancy in my database required importing the data in batches, building hash tables in memory, etc...In short it greatly increased the complexity of my code, introduced many bugs that had to be found, and my code is not as robust as my original implementation. However, now importing a striknet zip file only takes 20 seconds for the first zip file. The time then slowly increases as the data grows, so that now after running the importation over night, the time to import one zip file is up to 5 minutes after importing the vast majority of the data. 20 seconds to 5 minutes is much better than over 4 hours, so the trade-off in development time, complexity, and robustness in exchange for speed seems well worth it in this case. My original code probably would have taken months, if not years to import all of the data.

Tuesday, June 14, 2005

An example long-only option portfolio.

There were some great posts on Niederhoffer's site today. By the time you read this, they will probably no longer be at the top of the site. For the first article search for "Roger Arnold writes of the Sage". It's a great article about Warren Buffet.

The other great article is a basically a backtest of buying QQQQ LEAP calls. Search the site for "Nasdaq Options, by Charles Pennington". If I understand this article correctly, ATM QQQQ Calls 18 months out usually return about 3:1.

I've been asked lately about how a long option premium portfolio would work. (Remember I usually short option premium.) So, I'll use these QQQQ leaps as an example of how a very conservative long-option portfolio could be constructed. Say you have $10000. You could use that money to buy a 10 year Treasury bond yielding almost 4.60%. Today the options in the article mentioned in the paragraph above could be purchased for $460 per contract. (I purchased at that price today.)

So here is how the portfolio could work. You could purchase a 10-year Treasury Bond, and use the interest each year to buy one of these option contracts. If the options on average return 3:1 over 18 months, then that is around 200% return over 12 months, right? So now instead of earning a riskless 4.6% interest you are getting 4.6%*200% = 9.2% return, and the return is still basically riskless since none of your principal is at risk. That's a pretty nice rate of return for almost no risk isn't it? If I was allready wealthy I'd be seriously tempted to put most of my money in a strategy like that. (not exactly like that though.)

Here are some potential problems with this quick and dirty analysis. First of all I'm doing this all in my head, and may have made some very simple math errors that could ruin the whole thing. Secondly I know almost nothing about bonds...I may be missing something important here, especially with regard to the whole mechanics of how bonds work. For example, I'm not sure if treasury bonds actually pay an annual coupon or not, also you would be buying the first option contract a year before you would actually have accrued any interest.

Probably the most important problem with a strategy based on buying exactly those options though, is that there are only 13 independant data points in the sample set. I think my Dad (who happens to be a professional statastition) once told me that as a general rule of thumb you want at least 29 data points if I recall correctly. Based just on the information in that article, there isn't enough information to know if buying those options is a good idea or not. But anyway, this illustrates the general idea of when I talk about building a long-option portfolio. I'd put the bulk of the capital in a safe place (preferably earning decent interest) and use a small percentage..like 10% a year or less, buying options that are well-leveraged to their underlying issue. One of my favorite options books Options as a Strategic Investment by McMillan has a chapter about this type of strategy. Once I get my option database set up I hope to investigate these types of strategies more thouroughly.

Yesterday I bought some more SHLD common for my long term portfolio. I have been watching SHLD closely during this sell-off in the stock. I was hoping to increase my position in this sell-off, by at least 50%. Yesterday I bought half of that (I've held SHLD for a little over a year now.) I wanted to buy at around 130, but I was concerned the sell-off might end before the stock hit that price so I bought some yesterday at around 135 I think, with the plan to buy the rest at around 130. The stock was up 3 bucks today...I wonder if I will ever get a chance to buy more. My gut tells me I won't. One thing that is kind of cool, is the money I spent yesterday buying the stock was basically the house's money, since my profits on the position were more than it cost me to buy what I bought yesterday, even when the stock was down so much.

As I mentioned before, I also bought those options mentioned on the daily-spec list today at $4.60 as another addition to my long-term portfolio. They expire in 18 months. I don't think I've ever actually heald a position for 18 months in my life. Maybe my CMCSA position is getting close to that length of time, but that would be about it.

Other than those 2 trades, I'm just patiently waiting for option expiration. I expect to make a profit this month. I have 2 positions which are problematic. ONXX is trading close to break even, and DY is at a considerable loss. Delta's on both of these are pretty high now. Either one could wipe out my profits for this month in the next 3 days. The rest of the positions I am still carrying, will almost certinately expire worthless, and are all presently trading with no bids. My PnL at this point will largely depend on the fortunes of those 2 stocks. This is the first expiration in a long time where I don't have to be concerned about getting assigned stock, as I am currently only using 65% of my margin capital. This makes it much easier to just relax, hang back and see how expiration goes.

I'm looking at several potential option short sales in the near term. I'm looking at establishing a new position in MO options, off of recent weakness...perhaps sep 65 or 60 puts. I want to wait to see if MO comes in a little more during this expiration week. I'm also looking at EBAY puts. Maybe Oct 32.5's or if ebay drops a little something in the 30 strike.

Tuesday, June 07, 2005

So much for backtesting?

Well, I wrote some code this weekend to import my stricknet.com options data into my MySQL database. I had a few hours to burn tonight so I debugged it, and started importing one day of data as a test. The data from stricknet comes as a zip archivefor each day which contains 2 comma dileminated text files. The first one is about a 136KB uncompressed and contains end of day stock price data. There are around 2600 optionable stocks. The other one is about 6.1megs uncompressed and holds the options data. My program reads the files directly from the compressed zip archive and imports them into my MySQL database.

I need the data in a relational database. This gives me more flexibility, and makes it easier to do the complicated queries I will want to do as I develop quantitative options trading strategies. I also structure the data more efficiently than the simple comma deliminated list that stricknet provides. The comma deliminated lists contain much redundant information which I eliminate by using good data structure.

So anyway, I started importing the first zip file a long time ago and it's still not done. I don't know how long ago exactly. It wouldn't surprise me if this is taking over an hour for just one day's data. The stricknet data comes as 2 CD's and contains just a little over 2 years of data. There are what 240 trading days a year? 240 days * 2 years * 1 hour per day / 24 hours a day = 20 days to import the data...if it takes an hour to import each day's data. That estimate of 1 hour per day may be too low. This may be too long. If it takes that long I may never get all of the data imported and may never be able to do much backtesting.

I need to think of some optimizations. Maybe unzipping all of the data first would speed it up? I'll have to try that. I do a lot of checks to ensure the data-integrity as it gets imported. I suspect some are unnecessary, maybe eliminating some of these will speed it up a little. I could add some indicies to speed up the checks...but that would also slow down the inserting of new records.

I have another computer that I don't use for anything at all. It's a fairly fast computer. One of the first things I probably need to do is to set that up as a dedicated database server. Maybe that is what I do...Just start running the import program on that other computer 24 hours a day for a month or two.

This is a very discouraging development.

Hooey?

Well, maybe all of that stuff about how you have to chase stocks sometimes to be a contrarian to justify my DNA entry was all bullshit? I could have gotten into DNA at a much better price today.

Thursday, June 02, 2005

What it means to be a "contrarian" changes as the markets change.

Last Friday I purchased some in the money QQQQ puts right before the close, because the market is overbought and sentiment had been very bullish for sometime. All weekend I felt like the trade was a mistake. The market felt (and still feels) like one of those markets where the market goes overbought and stays that way for a long time. When there is strong momentum, I don't think selling strong bullish sentiment works anymore. You just don't run out of buyers like you do in a down or range-bound market. The rising stock prices begin to attract new money, and stocks just keep going up. Shorting these markets too early can be suicidal. This is why I felt uncomfortable with the trade over the weekend. However on Tuesday, the market was down a little when I woke up. I resolved to stay in the trade. Then I read some article from Cramer on realmoney.com where he said he expected strength into the close. That was enough for me, and I exited the position at a small profit of $3.00/contract. By the end of the day as the market sold off into the close I felt stupid for listening to Cramer. I didn't feel dumb when I woke up on Wednesday to a really strong up market. I would have easily lost a minimum of 40 to 50 bucks a contract if I had still been in those puts.

Remember a while back when I said I was tempted to buy DNA LEAP CALLS? Remember how I congratulated myself for having the strength to resist my temptation to buy those calls? I would have made out like a bandit on those calls. They have almost doubled since then. I'm noting this, so that maybe I'll remember this next time I feel trepedation about buying premium. That pick would have made my long premium portfolio. PBG and NITE have been losers so far, but with the profits I could have made from DNA, would have more than made up for that. Remember the idea for being long out of the money puts and calls is to expect to lose money on most of your positions, and have one or two winners that are up so hugely that they make up for many small losses.

Part of the problem with a stock like DNA is the options I wanted to buy were so expensive on an absolute basis (they were cheap according to the Black Scholes Model however). At the time the call contract I wanted was around $280. When buying far out of the money options, I expect that more than likely I will have a complete loss on any individual position. Now losing 280 would not be a huge drawdown in capital for me. Over the timeframe of 6 to 9 months, I would barely even notice a loss that size. The problem is that a drawdown that size would be noticeable in terms of monthly profit/loss for my short premium portfolio in whatever month I took it. Right now I am focusing on learning to sell premium and being consistantly profitable every month. Psychologically the prospect of hurting my results in whatever month I potentially took a 280 loss was too much for me. I have found this year that even losing as little as 100 dollars on a trade here and there really adds up to hurt my performance over the long term. As I become more capitalized, I will be able to experiment more with trades like the DNA trade, because the loss from trading just 1 contract will become insignificant, since I will still be trading in the same size when I experiment.

The bottom line right now though is that I don't have enough capital to have a good expiremental long premium portfolio, as well as my primary short-premium portfolio. For a portfolio consisting of long puts and calls positions, I would want/need to be even more diversified than I am in my short-premium portfolio. I don't even have enough money right now to have a sufficiently diversified short premium portfolio. (I can be less diversified when short premium for a period of time because more than likely most of my trades will be profitable, so over any period of time my likelyhood of one position hurting me is small.) I should have 30 positions, all equal sized in my short premium portfolio. Right now I have like 9, and the position sizes vary more than is ideal. I would want at least 30 in a long premium portfolio to make sure my net was cast wide, since all of my profits would be coming from maybe as few as a couple of my positions in a year.

I sold short DNA JULY 75 puts today, with DNA around 82.85. Yes I'm chasing this stock up. Sometimes to be a contrarian I believe you have to chase stocks. Sometimes in markets "the crowd" is waiting for a pull-back to jump in. In such markets to be a contrarian you need to jump in early, since the crowd won't get their pull-back, and money will be made by the few who did jump in. Eventually the crowds will capitulate and buy, because their greed over the lost profits will be to much to bear, and the stock stops going up. Of course the chalenge is to know when we are in that accumulation rather than that capitulation stage. I guess we'll see.

I hope I am not jumping into this stock just because I feel bad for not pulling the trigger on that DNA LEAP call purchase last month. I don't think I am. I like the stock's prospects near term, and selling these options short has good theoretical odds and a high probability of profit according to the BS model. But really it's hard to know when bad emotions slip into trading decisions.

The June portfolio remains in good shape despite serius sell-offs in DY, GS, and ONXX. GS is still far above my break-even (around 10%). ONXX has sold off but seems like it wants to stay above the strike. DY is now in the money and below my break-even, but not hurting me badly yet. It's going to be awhile before I have to worry about being assigned. Since it is a small position and I have allready taken profits on some June positions, I will probably be ok if I'm assigned. Usually where I have to worry about assignment is when I'm transitioning into later month options, when at the same time I still have lots of front month options which I am waiting to expire. I often have to use most of my margin in such cases.

It's usually only for a few days that I am so highly margined...but unfortunately this is usually the few days when my danger of being assigned is highest. In such cases I get very nervous as an assignment would surely result in a margin call. I've never gotten a margin call so I'm not really sure how bad that would be. If my broker liquidated me out of really illiquid option positions that would really suck. I would lose significantly on the bid-ask spread. Interactive brokers doesn't actually give traditional margin calls...their computers just immediately liquidate your positions...I'm not exactly sure how that works since it has never happened to me. I think there is some way to set priorities for what gets liquidated first. I probably should do that...but I know I won't ever really worry about it until it hurts me. Sometimes lessons have to be learned the hard way. Maybe I'll get the opportunity to learn this one some day.