Premier of Perl and Stocks

I’m starting a new job soon, and with any new programming job one should expect to learn new languages and technologies. What are these? Perl and Sybase. Out of these two, Perl has a much more vibrant community, so I have decided to learn Perl. How does a programmer learn a new language? Pick up a book. What book? Modern Perl. Reading about Perl can only do some much, so the best way to learn is to put knowledge gleaned from the book into good use. The best use I came up with is to create a stock scraper that spits out what stocks to buy and what ones to sell.

The strategy for stocks is remarkably simple. Buy stocks that have set three 52-week highs within a period of 24 days. Sell stocks that have set three 52-week lows within a period of 24 days. How did I come up with this strategy? I copied it. From whom? I forget, some author. The author came up with the magic numbers of three and 24. Whether or not this strategy is effective remains to be seen.

The next problem is finding the right information. Spending hours scouring the web revealed that stock pricing and history is closely guarded and often hidden behind some sort of paywall. The closest API that I found was Yahoo’s YQL, but it doesn’t keep historical information, so I couldn’t use it. Then I stumbled across Wall Street Journal’s new 52-week highs and lows webpage. I found I could manipulate the URL to get the 52-week highs and lows for the day that I wanted. It wasn’t the perfect solution as querying the Wall Street Journal involved downloading an entire webpage, which meant I would have to parse all the HTML elements for the information that I wanted. Luckily the community behind Perl had plenty of resources that facilitated solutions.

The resulting Perl - 75 lines of simple code. This was my first experience with Perl and I was quite surprised at how similar to C it was. There’s a notion of referencing, dereferencing, lexical scoping. In fact, I was able to write Perl code just like I would write C code, albeit a bit more tersely. The most awkward part of Perl is its syntax and default variables (yes, I’m looking at you $_ and @_). I just don’t find it intuitive to call manipulative functions without an explicit variable. It is not like currying, which is logical and mathematically thought out. Default variables just seems like a way to save a few keystrokes. I know that they are suppose to stand for “it” but “it” can be quite ambiguous. I don’t have as big of a complaint against syntax, I just have to remember that getting the reference of a variable is / and not &, and that I have to keep void, scalar, and list context in mind at all times. Perl shouldn’t be too hard to learn (famous last words?)

In the end I decided to put my money where my programming skills were and invested in five stocks, approximately a thousand in each. After a couple weeks of this algorithm I am still about even. The main goal here to do better than the market average. Whether or not I succeed in my goal, hopefully I can say that learning Perl was worth it.

Comments

If you'd like to leave a comment, please email [email protected]