I wrote a computer program!
Apr. 13th, 2011 03:34 pmIt slices! It dices! It juliennes prices! And don’t you want to know how it works? No, of course not. (See obsessive details.)
I had a drawdown a few weeks back, for which the standard response is “stop trading and start analyzing”. So I wrote an analyzer program, because — unlike stock-trading — that’s something I actually know how to do. My program looks at weekly historical data for a stock and uses rules to calculate what the buy and sell prices would have been; from those it computes, for each week, how long the stock would have been held if bought then and what the profit would have been. That was the easy part.
Then I added calculations for various technical indicators such as MACD. Doing this required that I learn how those indicators really work. MACD is especially difficult because it is the difference between two exponential moving averages, and EMAs are very sensitive to round-off error. I don’t know how people managed to calculate these things back in the olden days of pencil and paper: if you make an arithmetic mistake in your EMA, it will continue to screw up your results for more than a year afterward.
To avoid having to write a graphing module for my program, I tried to make my indicators produce the same values as the indicators at StockCharts.com so I can use their free charts to display my results. This was mostly successul except that I think their values for Parabolic SAR are bogus and I refuse to match them; whenever a stock has a wide-ranging week following by several narrow-range weeks, from then until the next stop-out the pSAR values shown on their charts are just way too low in my opinion. But I got everything else to match, down to at least one decimal place and usually two.
Now the creative part: I assigned limit values for each indicator. If the indicator was on the wrong side of its limit, that meant the stock should not be bought. For each week, if none of the indicators said “do not buy”, then my program would pretend to buy the stock and see how it would have done. Then I used a technique much like simulated annealing to adjust the limit values for maximum gain. Result: only 1% of weeks were selected for purchase and those weeks yielded a 6% gain on average. Then I doubled the number of stocks being analyzed (from 50 to 100). Result: average purchase yielded a loss! So I tweaked the limit values some more and got the average gain up to 4% per purchase.
( additional boring verbiage )I slapped a GPL on my program, but I don’t plan to publish it yet until there are some realized gains to show. It is written in Emacs Lisp, which is the best language I know for writing programs where you don’t know where you’re going until you get there. It is a little slow: 0.03 seconds for a weekly scan of three years for a stock. But the speed is fine if I am just testing individual stocks that StockCharts.com has already selected for me.