One can mostly classify traders as a mean reversion trader (i.e. who bets on the reversal of the trend) and as a breakout trader (i.e who bets on the direction of the trend).
So, let’s take our journey of data visualization in Python forward. The first attempt to make the chart was over dynamic Treemap offered by Amcharts.
Although the heatmap looks beautiful, there are dozens of problems –
A heat map is a graphical representation of data where the individual values contained in a matrix are represented as colours. “Heat map” is a newer term but shading matrices have existed for over a century. Heatmaps can also be of many forms. It is all about visual representation.
Now to generate heatmap, all we needed is to create a table and the colour to quantify its price movements! The animation is useless in real-time data because it will be chaos due to so much data.
So, we will update it in each 5 minutes interval!
The primary goal here is to create a heatmap for all the stocks which are in the FNO segment listed on the National Stock Exchange of India Ltd (NSE). Our HTML heatmap will the stock symbols and its respective single-day percentage price change and it will be coloured accordingly.
We collate the required market data from NSE and construct an HTML table. Although the number of companies in the list is fixed as of now i.e. 200, we are not sure if it will stay the same in the future. We are dividing the rows after each 13th item. Further, we want our seaborn heatmap to display the percentage price change for the stocks in a descending order.
The first task is completed.
There are two ways of representations in HTML.
The first problem is to sync it to the web server. Currently, I am using Digital Ocean for Python works as I get tons of free credit from them. They are very easy to use and most importantly, the server is glitch free! You can use my link to get 50$ free credit to test out.
So, coming back to the point. Use ftplib, you can write it like this:
import ftplib session = ftplib.FTP('server.address.com','USERNAME','PASSWORD') file = open('kitten.jpg','rb') # file to send session.storbinary('STOR kitten.jpg', file) # send the file file.close() # close file and FTP session.quit()
Now the second task is to dynamically generate the colour. Now we have two sets of gradients –
So, we need to calculate the number of positive stocks in total. Then make a gradient of type A mentioned above into them. Similarly, goes for the negative stock
positions = requests.get('https://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/foSecStockWatch.json').json() endp = len(positions['data']) positive_stocks=0 i=0 for x in range(i, endp): value = float(positions['data'][x]['per']) if value &amp;gt;= 0 : positive_stocks = positive_stocks+1 negative_stocks = endp-positive_stocks
Then, we shall just use the colour package. Here goes the basic usage –
from colour import Color red = Color("red") colors = list(red.range_to(Color("green"),10)) # colors is now a list of length 10 # Containing: # [&amp;lt;Color red&amp;gt;, &amp;lt;Color #f13600&amp;gt;, &amp;lt;Color #e36500&amp;gt;, &amp;lt;Color #d58e00&amp;gt;, &amp;lt;Color #c7b000&amp;gt;, &amp;lt;Color #a4b800&amp;gt;, &amp;lt;Color #72aa00&amp;gt;, &amp;lt;Color #459c00&amp;gt;, &amp;lt;Color #208e00&amp;gt;, &amp;lt;Color green&amp;gt;]
So, We modify it to our needs –
from colour import Color positive_colors = list(Color("green").range_to(Color("#BDD051"),positive_stocks)) negative_colors = list(Color("#DFA4A4").range_to(Color("red"),negative_stocks))
The third and final step is to generate the table dynamically! We are getting the data from NSE as JSON format. Let’s write down our complexity on the algorithm –
Now, to deploy colours, We need another loop –
So, We need to a setup cron job. Cron is a scheduling daemon that executes tasks at specified intervals. These tasks are called cron jobs and are mostly used to automate system maintenance or administration.
You can schedule cron jobs to run by the minute, hour, a day of the month, month, a day of the week or any combination of these. Crontab (cron table) is a text file that specifies the schedule of cron jobs. There are two types of crontab files. The system-wide crontab files and individual user crontab files. Users crontab files are stored by the user’s name and their location varies by operating systems.
You can read more here. You need to know two things –
/usr/bin/python for Digital Ocean droplet we are using.
/root/heatmap/ for Digital Ocean droplet we are using.
chmod u+x /root/heatmap/main.py
*/10 * * * * /usr/bin/python /root/heatmap/main.py
That’s it! It will run every 10 minutes. Now let’s define all the code inside the function
heatmap(). We will run another function to check if it is market time i.e. 9:15 to 15:30. It will skip running the program unless it is market time.
import datetime current_time = datetime.datetime.now().time() start_time = "09:10" #UTC Times end_time = "15:30" start_hour = int(str(start_time)[:2]) start_min = int(str(start_time)[:5][-2:]) end_hour = int(str(end_time)[:2]) end_min = int(str(end_time)[:5][-2:]) def heatmap(): #Rest of the code goes here def job (): if (current_time.hour&amp;gt;start_hour and current_time.hour&amp;lt;end_hour) : heatmap() elif (current_time.hour==start_hour and current_time.minute&amp;gt;=start_min) : heatmap() elif (current_time.hour==end_hour and current_time.minute&amp;lt;=end_min) : heatmap() else : pass
The data is coming directly from the JSON file. So, We are basically waiting till they block us. It auto-refreshes itself every 5 minutes. The resource will be low as it is within an iframe.
is it possible to run this locally in a computer
You can code it using Node.JS.