Posted On: Friday, 15 May 2020 by Rajiv Popat

Learning a new programming language is no different than learning a new spoken language. The biggest hurdle to both isn't picking up the language. It's having a lack of opportunity to then use the language in the real world so you become proficient at it.

I've been playing with python for a few months now and the only reason I've been able to hold on to it and become decently acceptable in the language is because there are a lot of opportunities to use python in your personal life. So if your work doesn't demand that you use python you can still put it to use in the real world to solve real problems.

For example, I have a requirement where I set fair value of stocks and buy or sell shares when the prices of a stock go outside this range. That is, if the price of a share falls beyond a certain value I want to know instantly so that I can buy the share and if the price shoots up beyond a certain value I want to be notified so that I can sell the script. The problem is I don't have time to monitor share prices all the time. So I decided to put python to some good use and see how versatile the language is at automating this task.

Building A Price Tracker For Your Portfolio Which Gives Buy/Sell Notifications Using Python:

The first and the biggest step to this automation is to get the current market price of a share / script. There are a lot of paid services that do this but I wanted to get this for free. Python gives me an excellent library called beautiful soup for parsing HTML so the task of screen scraping shouldn't be as daunting as it is in some other languages.

What I discover is that Yahoo Finance shows me the price of a share. To fetch the price of the share all I have to do is hit a URL: https://in.finance.yahoo.com/quote/ + [name of the script whose price I want to fetch]. So, for example: This URL gets me the price of a script called TVSMOTORS in the Indian National stock exchange.

yahoo

The bit highlighted in blue square above (342.35) is the exact value I need to fetch. To fetch that I select that element using google chrome developer tools, pick the specific div and just click copy selector:

yahoostockpricecopyselector

Now we tweak the selector and it basically looks like this:

#quote-header-info > div . My\(6px\) .Pos\(r\). smartphone_Mt\(6px\) > div (without the spaces).

With this I can write a simple script and fetch the price of the script using python using just five lines of code!

stockpricefunction

That's impressive! Screen scarping technologies and frameworks have come a long way and have become really elegant. Now that I can get the price of a script, I can store the high value (above which I want to sell the shares) and low value (price below which I want to buy) in a SQL Server table and then compare those with the real-time prices we fetch using screen scrapping. [Before you flame me for using SQL server with python, I'm doing this because I already have a server with SQL server for this blog :)]. I can do all of that easily in just about 20 lines of code more:

pythongetallresultsforstockprice

That's another 20 lines of code which looks up my SQL table by user, fetches all high and low prices of shares I've preconfigured, compares them with the current price of shares, does this for all shares in my portfolio and tells me if I need to buy or sell specific shares. And I run this on flask:

pythonflaskscriptrunning

And just like that I have an API that can compare of my portfolio of stocks, their low and high prices set by me with the current price in market and then tell me if I should buy or sell specific shares:

JsonResults

I can now host this on any Linux (or windows) box or container and can consume it with a mobile application or even using my automagic automations or a custom mobile application, but those are topics for a different post.

Long story short, in less than 10 lines of code in python I'm able to fetch prices of scripts. Another 40 odd lines and I can compare my entire portfolio and get insights on which stocks I should be buying and selling and turn the whole thing into a WebAPI I can call from anywhere. If I wanted to I can scale this API and open this for everyone because the concept of user is already baked in the design of the database.

In the next couple of posts we will talk about how we can deploy this on a live URL and how we automate the notifications on an android phone. Till then, If you are a .NET developer, and if you've never tried a different language, python is an excellent language to start playing around with because it's really elegant, versatile and with only a few lines of code you can get a lot done. And most of all, you can use it to solve your own real life problems even if your next work project doesn't require you to use python.


Comment Section

Comments are closed.