Monday 15 June 2015

Robs Stock Tracker!

Background
Ever since playing Eve Online I've liked playing the market in video games. So much so I built a little trading game prototype at some point. One that relied on a barter system. 


I really should go back and finish this at some point! :D

I don't have the time to play MMO's anymore. But while watching my girlfriend play Eve I started jonesing to trade again. So I thought to myself, why do something in a game? If I was 'ok' at it on Eve then why not try the real thing. I mean, it's not technically a game but the term 'playing the market' must means it's kind of a game, right? :D

I started reading up on stocks and shares and came to the conclusion that this was a lot more in-depth than any game I've traded on. Reading candle graphs and trying out technical analysis was fun and all but I'd need to do a lot more research before being able to invest cash on this. I needed to find out what time scale I wanted to be using (day trading, swing trading etc). I'd need to settle on collateral to start off with.

I started trading on a fake/virtual stock exchange. Just to practise. I had a lot of fun and managed to make a profit of 3K fake Internet money in a week (albeit with a collateral investment of 100K).

During my dabbling I found that I would often have to do the same calculations over and over again. This got on my nerves really quickly. Especially when trying day trading where 15 minutes can mean profit or hitting your stop loss. It was at this time that I started dabbling with Perl and saw an opportunity to make something cool!

Objective
The objective of this Stock tracker wasn't just to track the value of my stocks and list them out as increases or decreases per stock. This information can already be found on the stock market and various other sites / newspapers. This information wasn't all that helpful to me as an increase on the day wouldn't necessarily mean a profit for me as this would be based on when I bought into the stock.

I wanted to be able to at a glance see what my total profits would be at any point during the trade day per stock. This included any commission fees and taxes. So I set out on doing this, making use of Perls text matching functionality. The tracker works really well albeit pretty basic.

I am able to retrieve a webpages source directly by making use of the LWS module, after this I parse the HTML extracting only the information I need. After I have this information, I simply use it with my own information (amount of stock I have, how much I paid for it, my stamp tax, commission per sale etc.) I read this in from another file and nothing is hard coded.

In fact I am able to specify multiple stocks by specifying it in a file (at the moment its a simple .txt). I specify the company, URL and the rest of my details. After this, my tracker uses the URL to grab the page. After it does its parsing and calculations it returns the values I want. At the moment only the total profit per stock.

The next step I wanted was to automate the updating as constantly re-running the script manually is pure madness. My script will start up when run and continue to update itself every minute (updating my profits). It will run up until the market closes (for debugging purposes it will run till 18:30 GMT at the moment). Once it hits this time it will shut itself down. You can also quit out using ctrl+C which will then run a subroutine to clean up a bunch of information and display a user friendly message.

After this I used the Term::ANSIColor module (a built in module) to colourise my output and make things a bit more user friendly.

The final feature I added was stop loss tracking. This allows me to set a stop loss values (in a similar fashion as the rest of the values input). This will the be used to either inform me that my current total stock value is still higher than my stop loss or if it drops lower it'll output a warning. This stop loss can be set in an easy way for each stock I wish to track. The number of stocks I want to track is (in theory anyhow) infinite. There is no limit but obviously tracking more will impact performance as I have to parse a lot of information.

The final output can be seen below! A stock tracker that can track any amount of stocks on the London Stock exchange and update it in real time. Only total profit numbers will be output and stop losses will be monitored. Pretty much automating a whole bunch of minor inconveniences for me. And best of all is it runs on most platforms (Only tested  on MacOS and Windows however).


Example of the script in action!

TODO - Future Work
There's still some things I'd like to be able to do with this script, but they aren't super important. These are:

User specified output - I already have this partially implemented in my script. It would in theory allow the user to specify what they wanted to see. For example perl Scriptname - StopLoss would only show stop losses. Scriptname - showall would output everything. Scriptname - help would list the help output. I'd need to use the getops module for this. At the moment I use ARG[] for this so I don't have to rely on cpan or third party mods.

This little project would also be really neat to use to combine with some AI. As I specialised in AI during my undergrad I am pretty interested in this. Mostly in terms of using some form of intelligence to calculate ideal stop losses. Seeing as how I can track the days trend / months trend and even years trend. This is something that seems feasible to me. If I had a week or so free I'd love to look into this. the AI field seems pretty dead in terms of Perl so I might have to rewrite things into Python or Ruby for this however.

In conclusion I'm pretty stoked with what I managed to get done. I got much better at Perls data structures and the flow control (which is pretty much the exact opposite of C++'s flow). Yea I get ones a scripting language and ones a proper programming language! Chill! Sheeesh!

Most importantly I got competent at using regular expressions, which was a big gap in my Perl knowledge until this point. For example, do you understand the code below? Cause I finally do! :D


Regularrrrrr Expressionnnnssss


Good times, good times!

No comments:

Post a Comment