Monday, 15 June 2015

Robs Stock Tracker!

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!

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!

Still transmitting! - What I've been up too

Hello world,

It has been a while since I last updated my blawg! :D

So what's been going on with me lately can be summarised in three words - University, Work and Perl.

As can be seen on my site, in the portfolio section I finished my second module on my part time masters course. My preliminary grade is 65% which is kinda low for what I wanted. But considering I was trying to learn C++, OpenGL and the Oculus Rift SDK all at once, I think I did ok :D I'll just have to focus on my next two modules to make up the mark. This can still work! Just means I need to write more (read ALOT more) c++. I also need to pick up a book and Datastructs and Algo's in C++. I have only recently come to understand the utter importance of data structures and algorithms. Something I wish I picked up on sooner. I always knew they were important in theory, but I always shied away from them and Big O notation. This will need to change soon. Additionally I need to pick up a good book on OpenGL, one that does not use deprecated stuff! Out of date GL stuff screwed me over a few times!

As you can probably tell, our Grimm Journey game project has been on hold for a little while, as most of the team are currently pretty swamped with various other projects that has been sucking all our time away. Hey we gotta eat and pay rent, right? But fear not for the project will continue soon! Personally I am aiming to implement the animations from the demo scene into the game and hook up the narration (yup still working on this) properly at the end of next month when I have the time. After all that C++ and perl, writing some C# should be a treat lol.

For now my main focus has been on refreshing myself with / learning Perl to a proper standard, due to work requirements. And because I learn better by doing than reading and watching I set out to make a little application/script. Making a game in perl would have been ... interesting. However I wanted to use it more in line with what it was designed for really. Covering scalars/arrays and hashes as well as regex and matching/substitution.

As such I made a basic Stock/Share tracker that pulls info from the stock exchange and does some math on it. The tracker doesn't track the value of stocks ion their own, mostly because this info is available all over the Internets. What it does instead is track your specified stocks, based on the mount you paid for them and the quantity you have. Then using that calculates you profits (after stamp tax and commission and things like that). The results update in real time (at the moment every minute).

What this means is I have an easy way to at a glance see where my stocks are at, if I had any. I'll make a follow up posts going into more detail for those interested. I am currently using it with a fake/virtual stock exchange to test. The goal here was to see the accuracy of my script and not make cash...although that would be neat.

Work-wise everything is going neat, should be moving departments at the end of this month and be able to focus on more programming and less testing. I'm looking forward to this. Exciting times.

That's all from me for now, I'll be updating my portfolio with some screenshots of my stock tracker sometime soon so go check it out :)

Seeeeeeeeeee ya!