Stock Market and Python

Kshiteesh Bhushan
Oct 10, 2020 · 5 min read

A teenage crush, an adulthood hobby. The times were of 2013 and a cliche aspiring Engineering boy landed on to the marvelous act of DiCaprio in “The Wolf of Wall Street”. The stock market Ups and Downs have always fascinated me towards it and now in the year of lockdown 2020(hoping people will bounce back here in the future too)when we saw a sudden surge(~5 Mn) in the number of new users in the Indian market space, I thought why not to play around with the easily available market data in one of the few domains I feel comfortable(Python).

Before we start let me bring in to your notice that unlike for international market where python dedicated libraries like YahooFinancials supports easy download of data through API calls, they do not work that swiftly for Indian market space but nonetheless, we do can utilise an indigenous python library “nse” for market data handling. While keeping in mind the recent “Vocal for Local” agenda, I have done my analysis with previous 5 months market data of the automobile sector giant “TATAMOTORS”.

Data Load via NSE Library:

Here, is the snippet of my code used for downloading the data and converting into a data frame format from JSON.

Image for post
Image for post

Data Analysis and Visualisation:

The historical data for any particular company comes with15 different attributes that can be used for analysis and prediction purposes. Let’s have a look at the tabular data and a plot of stock price movement through the past 5 months on a daily basis.

Image for post
Image for post

As it’s visible that the data is at a very granular level for each date, I thought that it’ll be a better approach to study the trends, returns, sale and close price on a weekly level. As stated my analysis and predictions will move around the closing price at the EOD for the stock.

Firstly, Let’s look at the plot of movement of the mean price of the stock for the past 20 weeks.

Image for post
Image for post

Weekly Movement Analysis of the stock:

While this plot already explains a lot about the movement of the stock price, let’s have a look at the most important factor of any investment i.e. Return. As mentioned earlier, I have studied the return percentage on a weekly level. In order to calculate the return percentage, I shifted the mean price values of each week by one to another week and then calculated the difference between the original and shifted value(the mean price of last week).

Image for post
Image for post

While doing my initial reading of the data, Total Traded Quantity looked like a very promising factor to me for close price analysis. Going by the notion that as much stock is traded through the day as much as the close price at the EOD is going to be affected. Before going on with the analysis, I tried normalising total traded quantity in a week and mean price for a week to the same level so that comparison looks logical.

Image for post
Image for post

Hence, as appeared this turned out to be a promising factor to deep dive into as we can see a direct variation with the mean price of the stock. While the comparison here being shown is on a weekly level, during the later part I went on using this as a driver in my price prediction model on a daily level.

Feature Engineering for the model:

Along with the above driver total_traded_quantity_norm, I thought that in order to predict the close price of a day it’ll make more sense to have some more feature such as the percentage change in high & low value of the stock price for a day and also to keep an eye of previous day close price for the stock. Attaching the snippets with features for the model building.

Image for post
Image for post

Model building, fitting the train and testing out the model:

Now, let’s have a closer look at the model building, train test split, fitting the train data and predicting the close price on test data. However, the model I used here is a baseline linear Regression model, it can always be optimized to give more precise results on the stock close price prediction.

Image for post
Image for post

Plotting the prediction and comparison with actual pricing:

Since we have all the codes, data and learning in place now, let’s have the moment of truth. How well did our model performed and how logical were our used drivers for the model. Let’s take a look at the comparison of the actual price and predicted stock price for our test data.

Image for post
Image for post

Well, looks like the predicted close prices go in line with the actual stock pricing at the EOD for all of our test data set. Thus, we can award ourself some satisfaction for the work we did throughout the whole exercise.

This although looks a fun learning practice to perform but we should always keep in mind that the market in real is very volatile and is affected by a lot of factors. If you enjoyed reading and got yourself intriguing feeling, do appraise, clap as many times you want and feel free to reach out in comments for improvements and feedback.

NOTE: All the ideas and analysis performed are one’s personal findings and are just for learning purpose. Please reach out to your financial advisor before making any investment. Cheers! Happy Investing! Happy Coding!

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Kshiteesh Bhushan

Written by

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Kshiteesh Bhushan

Written by

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store