![]() |
VOOZH | about |
Predicting stock price direction is a key goal for traders and analysts. Support Vector Machines (SVM) is a machine learning algorithm that can help classify whether a stock's price will rise or fall. In this article, we'll demonstrate how to apply SVM to predict stock price movements using historical data, covering data preparation, model training, and evaluation.
Here we will import pandas, scikit learn and matplotlib.
We will Read the Stock Data Downloaded From Yahoo Finance Website. You can download dataset from here.
Output:
The data needed to be processed before use such that the date column should act as an index to do that and will drop date column.
Output:
Explanatory or independent variables are used to predict the value response variable. The X is a dataset that holds the variables which are used for prediction. The X consists of variables such as 'Open - Close' and 'High - Low'. These can be understood as indicators based on which the algorithm will predict tomorrow's trend. Feel free to add more indicators and see the performance.
Output:
The target variable is the outcome which the machine learning model will predict based on the explanatory variables. If tomorrow's price is greater than today's price then we will buy the particular Stock else we will have no position in the. We will store +1 for a buy signal and 0 for a no position in y. We will use where() function from NumPy to do this.
Output:
array([1, 1, 0, ..., 1, 0, 0])
We will split data into training and test data sets. This is done so that we can evaluate the effectiveness of the model in the test dataset. We will split 80% data for training and 20% for testing.
We will use Support Vector Machines by SVC() function from sklearn.svm.SVC library to create our classifier model using the fit() method on the training data set.
This code calculates and prints the accuracy of your model on both the training and testing data which were split 80/20 to check for overfitting.
Output:
This code trains and tests SVC models with different kernels like linear, polynomial, RBF and sigmoid to see how the kernel affects prediction accuracy on the test data. It then prints the accuracy for each kernel.
Output:
We will predict the signal (buy or sell) using the cls.predict() function.
Calculate Daily returns
Calculate Strategy Returns
Calculate Cumulative Returns
Output :
Calculate Strategy Cumulative Returns
Output
Output:
As You Can See Our Strategy Seem to be Totally Outperforming the Performance of The Reliance Stock. Our Strategy (Blue Line) Provided the return of 18.87 % in the last 1 year whereas the stock of Reliance Industries (Red Line) Provides the Return of just 5.97% in the last 1 year. We can further fine tune our model for better accuracy.
1. TCS
Stock Return Over Last 1 year - 48%
Strategy result - 48.9 %👁 Image2. ICICI BANK
Stock Return Over Last 1 year - 48%
Strategy result - 48.9 %👁 ImageGet the complete notebook link from here:
Notebook link : click here.