Stock Watcher
Overview
Many stock brokers offer an option called a limit order. The idea is that your account will automatically sell once a stock reaches a certain price. For example, consider a trader who owns 500 shares of XYZ's stock. If this trader wants to sell immediately when the price is over $30/share, a limit order to sell is placed at $30. However, brokers frequently charge an extra fee for this feature. I wrote a piece of Java software that could place the limit order without the additional fee. It would use TCP sockets to frequently check the price of the stock. Once the stock was at a pre-determined level, it would send an email to the trader.
Getting the Price
Yahoo.com shows all the prices for stocks. Finding a quote for Microsoft is as easy as getting the page http://finance.yahoo.com/q?s=MSFT. By simply replacing the MSFT with a different ticker symbol, any stock price can be quoted. Java has a few web classes that could parse a webpage, but I chose the Socket class. Here's how the program read the page:- Open a socket to port 80 (the HTTP port) of the yahoo.com page.
- Read the data from the webpage until it found the Last Trade price.
- Convert the Last Trade price from a string to a float.
- Close the socket.
Verifying
Once the price of the last trade was discovered, the program would compare it to the limit. The limit could either be an upper limit or a lower limit. The user would set the limit on the program before it is run. If the limit is not met, the program sleeps for a time and gets the price again. If the limit is met, it sends an email.Sending Email
This is the really cool part. Practically everything on the Internet can be accomplished with the Socket class in Java. I used the Socket class again, but this time used to send an email. Here's a simple algorithm of how it worked:- Open a socket to port 25 (the SMTP port) on my mail server.
- Write the headers of the email (Date, subject, to address, from address, etc).
- Write the body of the email.
- Close the socket.
Sure enough when the limit was met, the email was sent. This is a safe way to run the program. If a person was very daring, they could do the following:
- Open a socket to port 80 (the HTTP port) of their stock broker.
- Submit a URL that logs them into the website.
- Capture the cookie sent from the server after login is complete.
- Use the cookie to submit a sell order to the website.
- Close the socket.
If done properly, this would allow the user to automatically sell the stock. This is an especially nice feature for those who do not have Internet access all the time like travelling businessmen. However, newer sites may require an SSL connection to actually sell stocks. I noticed an SSLSocket class that would probably do the trick, but haven't had an opportunity to try it.
Conclusion
Using Java technology, it is possible to overcome the limit order fees from brokers. However, it might be best to simply send an email when the stock reaches a certain limit and do a reality check before selling stock.