Python will make you rich in the stock market!

Free Python course with 25 real-time projects Start Now!!

There are so many factors involved in the prediction of stock market performance hence it becomes one of the most difficult things to do especially when high accuracy is required. Here data science & its techniques have been used to search patterns and insights that were not approachable before. Learning Python- object-oriented programming, data manipulation, data modeling, and visualization is a ton of help for the same. So, what are you waiting for? Read the complete article and know how helpful Python for stock market.

Stocker is a Python class-based tool used for stock prediction and analysis. (for complete code refer GitHub) Stocker is designed to be very easy to handle. Even the beginners in python find it that way. It is one of the examples of how we are using python for stock market and how it can be used to handle stock market-related adventures.

WAIT!! Already know the basics, jump to real-time project: Stock Price Prediction Project

python for stock market

Understanding Stock Market Analysis

Stock market analysis can be divided into two parts- Fundamental Analysis and Technical Analysis.

a. Fundamental Analysis

This includes analyzing the current business environment and finances to predict the future profitability of the company.

b. Technical Analysis

This deals with charts and statistics to identify trends in the stock market.

Predicting Stock with Python

In this blog of python for stock market, we will discuss two ways to predict stock with Python- Support Vector Regression (SVR) and Linear Regression.

Support Vector Regression (SVR)

Support Vector Regression (SVR) is a kind of Support Vector Machine (SVM). It is a supervised learning algorithm which analyzes data for regression analysis. This was invented in 1996 by Christopher Burges et al. The cost function for building a model with SVR ignores training data close to the prediction model, so the model produced depends on only a subset of the training data.

SVMs are effective in high-dimensional spaces, with clear margin of separation and where the number of samples is less than the number of dimensions. However, they don’t perform so well with large or noisy datasets.

Linear Regression

Linear Regression linearly models the relationship between a dependent variable and one or more independent variables. This is simple to implement and is used for predicting numeric values. But this is prone to overfitting and can’t be used where there’s a non-linear relationship between dependent and independent variables.

Code for Stock Prices Prediction

python for stock marketing

1. We will use the quandl package for the stock data for Amazon. Quandl indexes millions of numerical datasets across the world and extracts its most recent version for you. It cleans the dataset and lets you take it in whatever format you want.

#DataFlair - Make necessary imports
import quandl
import numpy as np 
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split

2. Get the Amazon stock data from quandl. Print the top 5 rows.

#DataFlair - Get Amazon stock data
amazon = quandl.get("WIKI/AMZN")

3. Now get only the data for the Adjusted Close column. Print the first 5 rows for this.

#DataFlair - Get only the data for the Adjusted Close column
amazon = amazon[['Adj. Close']]

4. Set the forecast length to 30 days. Create a new column ‘Predicted’- this should have the data of the Adj. Close column shifted up by 30 rows. The last 5 rows will have NaN values for this column.

#DataFlair - Predict for 30 days; Predicted has the data of Adj. Close shifted up by 30 rows
amazon['Predicted'] = amazon[['Adj. Close']].shift(-forecast_len)

5. Now, drop the predicted column and create a NumPy array from it, call it ‘x’. This is the independent dataset. Remove the last 30 rows and print x.

#DataFlair - Drop the Predicted column, turn it into a NumPy array to create dataset
#DataFlair - Remove last 30 rows

6. Create a dependent dataset y and remove the last 30 rows. Print it then.

#DataFlair - Create dependent dataset for predicted values, remove the last 30 rows

7. Split the datasets into training and testing sets. Keep 80% for training.

#DataFlair - Split datasets into training and test sets (80% and 20%)

8. Create an SVR model now and train it.

#DataFlair - Create SVR model and train it

9. Get the score of this model and print it in percentage.

#DataFlair - Get score
print(f"SVR Confidence: {round(svr_rbf_confidence*100,2)}%")

10. Now, create a model for Linear Regression and train it.

#DataFlair - Create Linear Regression model and train it

11. Get the score for this model and print it in percentage.

#DataFlair - Get score for Linear Regression
print(f"Linear Regression Confidence: {round(lr_confidence*100,2)}%")

Python for Stock market

Let’s look at the analytical capabilities of Stocker in parts.

Starting with Stocker

The first thing that should be done is importing the Stocker class into the current python session after installing the required libraries. You can use it to create an object. The constructed object will contain all the properties of the Stocker class. As the stocker is built on quandl WIKI database hence it allows access to 3000 and more US stocks.

Python classes are comprised of – attributes and methods. Amongst all the attributes of the class, one of it is stock data for a specific company.

The benefits of using the Python class include – the functions and the data it acts on are associated with the same object. The entire history of the stock can be plotted by using the method of the Stocker object. The ‘plot_stock’ function has a number of arguments that are optional and by default, it plots the adjusted closing price for the entire date range that can also be customized according to our needs (range, stats to be plotted, type of plot). Using ‘plot)stock’ we can investigate any number of quantities in the data present in any data range and also suggest real-world correlations.

Additive tools

These are very powerful for analyzing and predicting time series. We know that the long term trend of any established multinational company seems to be increasing in nature but there is a possibility of identifying yearly or daily basis patterns. Such help of time series with daily observations can be provided by Prophet, developed by Facebook. Stocker can do all the work that be done by Prophet behind the scenes using simple method call to create and inspect the model.

These types of models remove disturbance present in data and smoothen it. Prophet models also look into fluctuations of data in real-life processes and make predictions for the future. Though there is concern related to past data but future data analysis is what companies strive for. This method call returns two objects (data and model) which are then assigned to variables that are later on used to plot time series components.

Explore top Python Applications to know more about the use of Python


It occurs when the time-series go from increasing to decreasing or vice-versa. These patterns are also very important as one needs to know when the stock rate is at its peak or there are significant economic benefits. Identifying these points and their cause of change helps in predicting the future. The stocker object can automatically predict the 10 largest changepoints which tend to line up near the peaks and valleys of the stock price graph (generally). On the other hand, the prophet can only find changepoints in the first 80% data only. Google search tools allow us to see the popularity of any search word over time in Google searches. Stocker can automatically retrieve this data for any specific term.

These are only the first half capabilities of the stocker where Python for stock market is used. The second half –


They are designed for forecasting, or predicting future prices. This is a tiresome exercise and hence needs plenty of learning to get into the actual process. The capabilities are publically available, even creating the tool itself.


We live in an age where anyone can learn programming or arts like data science or machine learning without that much of formal instructions. The idea can be anything, even stock prediction, python can be used in any sort of application base. All you need is hands-on knowledge of it!

Thinking how to start learning Python????

Ask for DataFlair Self-paced online Python training which will make you ready with Python 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google | Facebook

5 Responses

  1. ali says:

    is this possible to install stocker via anaconda?

  2. Sir Mixa Lot says:

    Your anaconda don’t want none unless you got buns, hun.

  3. MWJ says:

    Yes, it is. Go to Anaconda prompt and first install tensorflow (with pip since conda will likely not know where to find tensorflow):
    pip install tensorflow==2.2.0 # you can of course install newer version of tensorflow, but not older

    and then simply

    pip install stocker
    If you’ve been running Jupyter, close it, and run again. It’s worked in my case:

    import tensorflow as tf
    import stocker as st
    [133.27, 1.332, ‘2021-05-03’]

    Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.