# 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. ## 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 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

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
forecast_len=30
print(amazon.tail())```

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
x=np.array(amazon.drop(['Predicted'],1))
#DataFlair - Remove last 30 rows
x=x[:-forecast_len]
print(x)```

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
y=np.array(amazon['Predicted'])
y=y[:-forecast_len]
print(y)```

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

```#DataFlair - Split datasets into training and test sets (80% and 20%)
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)```

8. Create an SVR model now and train it.

```#DataFlair - Create SVR model and train it
svr_rbf=SVR(kernel='rbf',C=1e3,gamma=0.1)
svr_rbf.fit(x_train,y_train)```

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

```#DataFlair - Get score
svr_rbf_confidence=svr_rbf.score(x_test,y_test)
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
lr=LinearRegression()
lr.fit(x_train,y_train)```

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

```#DataFlair - Get score for Linear Regression
lr_confidence=lr.score(x_test,y_test)
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.

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

### Changepoints

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 –

### Predictions

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.

## Summary

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

### 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.

• Kevin says:

I have to agree

• James says:

I will have to agree to agreeing

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
tf.__version__
‘2.2.0’
import stocker as st
st.predict.tomorrow(‘AAPL’)
[133.27, 1.332, ‘2021-05-03’]

Good luck!
MWJ