19 Jun, 2018

My attempt to conquer the stock market

A few years ago, I made a rather bold and interesting dive into the stock market. I had only a layman’s knowledge of the stock market, what it was and how it worked. I had no idea what it took to actually own stock in a company, the different types of accounts available, etc.

So when I decided to attempt to make some money on the market with, of all things, a program I wrote using PHP (a coding language more commonly used for website development than numerical analysis), I was already in over my head. It may not have been the best decision to both develop a trading method while learning about the stock market, while coding my program, but at the same time I didn’t really have high expectations, I wasn’t expecting to become an overnight success.

Still, the experience proved interesting. I “paper traded” (simulated trading) for a couple months as I refined my code. It was obvious neither my code nor my trading method would have a 100% success rate but I felt it was good enough to start trading “for realsies.”

It was kind of hit and miss for the most part, but I think all things considered, I did pretty well. At the end of it all, I came out with an 8% gain after 8 months, but only after being down around 10% in month 7. Needless to say, that was a hairy time. I also had to bring it to an end because as I kept refining and updating my code, I actually managed to “break” it and because I wasn’t doing any kind of versioning, I wasn’t able to undo the mess I made. A friend of mine later told me a “good” stock broker (or portfolio manager?) gets a return of 12% per year, so obviously I was pleased with my results!

I thought it would be best to take my earnings and move onto some safer ways to make money for a while. But lately I’ve found myself wanting to take bigger risks (mostly out of desperation, I suppose), and thought with a volatile market, it would be a good time to come back (good because volatility works for day/swing trading, which is what I was doing). Now a few years wiser and more methodical, I began rewriting my code from scratch, and actually got it almost to the point I had been a few years ago, quite quickly (just under a month, I believe), but this time with cleaner and more efficient code. In short order, I actually bypassed my previous version, adding a feature I wanted before but had no idea how to do, and creating a few more. Now, a couple months later, I’m once again paper trading and while I’m slightly down (around 6%), I’m still optimistic about what I’ll be able to do with the program.

A brief summary of what my program does: using the most basic trading method I could find (and translate into code), my program takes that method and applies it multiple times to every stock in the market (I’m not kidding, all 20,000+ stocks in the AMEX, NASDAQ, NYSE and OTCBB), each time changing the length of time. I want to say the trading method is the “turtle method”? But I can’t find my original sources nor verification that that is what it’s called. Basically, you buy the stock when it reaches the lowest price in X days, and then sell it when it reaches it’s highest price in the same X days. For example, if today Pfenex Inc. (NYSE: PFNX) is trading at it’s lowest price in 5 days, then my signal to sell will be when it reaches it’s highest price in 5 days. My code takes that concept and reapplies it to every stock 6 times by changing the number of days from 4 to 10, and then returns to me which of those 6 attempts yielded the best results. By looking at a few other factors (average return, # of positive returns vs. # of negative returns, etc.), a score is assigned to the stock itself, from 0 to 100.

So every day, I import a complete list of the day’s prices for all four markets, and I run my code to analyze all the stocks, and get my list of results. It won’t tell me which stock to buy, rather it gives me a list of the stocks that were “bought or sold” on that day and ranks them from best to worst; I’ll then go through the top 20 or 30 and see which ones will give me the most return per share, and that’s the one I’ll “paper trade”. At the moment, I’m limiting myself to stocks I can buy $500 worth at a time, so if I have $800 in “paper cash”, I’ll only buy one stock. Obviously there are days where I do nothing at all, as I haven’t received a signal to sell anything.

I haven’t worked too much on my script lately as I’m contemplating an extreme shift in how the code will work. My original idea was to have my script test different trading methods by writing “modules”, basically a bunch of program that do the same thing, just in different ways. But managing the results has proven cumbersome, so now I’m stepping back and trying to figure out if there’s some way where I can write my own language, sort of, to tell my program how to trade a stock, and test that method on the market. So instead of having to rewrite my analytical code, I’m actually leaving out the analysis completely and making the analysis a variable, if that makes any sense.

This will, of course, be an ongoing thing, especially while I’m still paper trading. This is one of the things I’ll be blogging about, both the development of my stock market analysis program and its results, as well as any thoughts I have on the market and finance in general.

Tags: , ,

Leave a Reply