Getting Started with the Twitter API article described how Twitter's founders see it as more than simply a web site where people can send short messages to those who choose to follow their message stream. Twitter is a new kind of publishing platform, and the simple, straightforward API for this platform is easy to use with a variety of tools and programming languages.
Tweeting Big Jumps in the Dow Jones Industrial Average
On a normal day last September, I would never check the Dow Jones Industrial Average. With the financial turmoil of
recent months, though, I wondered more often whether the economy was getting us in deeper or if things had bounced
back, so I checked too often, usually at
CNBC's home page. When I did this, I often wondered if there was a simple way to receive alerts when the Dow made newsworthy jumps.
Checking stock and financial index quotes is pretty simple with any scripting language, so I used the
python-twitter API to write a script that checks the Dow and posts a Twitter message (or "tweet") if the figure moves at least 50 points since the last check. The script is about 60 lines of code, which, in addition to basic file I/O, determines whether the DOW went up or down since the last check, and assembles a sentence describing what happened.
The script is not hard coded to only check the Dow Jones Industrial Average and tweet after a 50 point swing, which is
why I called it tweetstockjump.cgi. By resetting variable values near the top of the script, you can change both the ticker symbol for the stock or index you want to check and the increment that triggers a tweet.
The web offers a wide choice of free stock quote services. Instead of going with a SOAP or other XML-based web
service, I went with a simpler option: the one-line comma-separated lists of stock information available from
Yahoo! Finance. For example, on the evening of November 19th, adding the ticker symbol IBM at
the end of the URL
http://download.finance.yahoo.com/d/quotes.csv?f=sl1d1t1c1ohgv&e=.csv&s= and pasting
it into my browser (or using the wget or cURL utilities mentioned in Part 1 of this article) retrieved the following line of text:
The commas separate these nine pieces of information:
- Ticker symbol
- Last price (after a 20-minute delay)
- Date of that price
- Time of that price
- Change since the day's opening
- Opening price
- Day's high price
- Day's low price
- Trade volume
Python, like Perl, has a split()
function that separates a comma-delimited list of values into the individual slots of an array with one short line of code.
With all these tools in place, the tweetstockjjmp.cgi script checks the Dow Jones Industrial Average figure, evaluates the difference since the last check, and tweets if this difference is greater than 50 by performing the following steps:
- Retrieve the latest information from Yahoo! Finance, split it up into an array, and assign the needed figures to variable names.
- Grab the same information about the most recent polling of the same data from a one-line text file that was saved at the time. If there is no such file, calculate the figure's jump from the day's opening figure, which is the sixth field in the comma-separated list returned from Yahoo! Finance.
- Compare the new figure with the old figure, and if the difference exceeds the value stored in the
a. Construct a sentence in the variable tweetMsg following this model: "Dow Jones Industrial Average down 130.82 from 8128.1 at 3:51pm to 7997.28 at 4:06pm NY time, down 427.47 since today's opening." As with the increment that triggers a tweet and the symbol of the stock or index figure being checked, the full name of the stock or index is stored in a variable set near the top of the script so that it can be easily modified to check for other financial information.
b. Send the message (or, as Twitter calls it, an "update") to the Twitter server.
c. Save the newly retrieved CSV file so that the next polling of the latest quote has something to compare it with.
d. Otherwise, if the figure moved less than increment points, the script is finished.
My first draft of the script was called checkDowJones.cgi
(cgi because it's a script being run under my account
on my host provider's system), but after storing the quote symbol, the referenced index's full name, and the increment
to check for in variables to make the script more flexible, I changed the name to tweetstockjump.cgi
to reflect its potentially wider usefulness.