Disclaimer: this is a research project, please don’t use this as your trading advisor.

Why Support Vector Regression (SVR)

Support Vector Machines (SVM) analysis is a popular machine learning tool for classification and regression, it supports linear and nonlinear regression that we can refer to as SVR.

I this post, I will use SVR to predict the price of TD stock (TD US Small-Cap Equity — I) for the next date with Python v3 and Jupyter Notebook.

Import dependencies

import numpy as np

from sklearn.svm import SVR

import matplotlib.pyplot as plt

import pandas as pd



%matplotlib inline

If you have any not found modules, please use pip to install it.

Prepare the data

I’ll use stockai package to get the stock data from Yahoo! Finance. The prices are from 2019–01–01 to 2019–01–30.

from stockai import Stock



td = Stock('TD.TO')

prices_list = td.get_historical_prices('2019-01-01', '2019-01-30')



df = pd.DataFrame.from_dict(prices_list)

df.head()

# Get data function def get_data(df):

data = df.copy()

data['date'] = data['date'].str.split('-').str[2]

data['date'] = pd.to_numeric(data['date'])

return [ data['date'].tolist(), data['close'].tolist() ] # Convert Series to list dates, prices = get_data(df)

Build models and start predicting

This is a very simple task, I will use the date and prices data to predict the next date price of TD stock which is 2019–01–31.

Please keep in mind that this is a very simple predicting method for research only. There’re lots of things that need to be considered such as sentiment analysis, fundamental analysis, technical analysis, recurrent neural network (RNN), time delay neural network (TDNN)… so I don’t use this as your method of investing.

In this example, I will use three models: linear, polynomial and radial basic function (default kernal for SVR).

# predict and plot function def predict_prices(dates, prices, x):

dates = np.reshape(dates,(len(dates), 1)) # convert to 1xn dimension

x = np.reshape(x,(len(x), 1))



svr_lin = SVR(kernel='linear', C=1e3)

svr_poly = SVR(kernel='poly', C=1e3, degree=2)

svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)



# Fit regression model

svr_lin .fit(dates, prices)

svr_poly.fit(dates, prices)

svr_rbf.fit(dates, prices)



plt.scatter(dates, prices, c='k', label='Data')

plt.plot(dates, svr_lin.predict(dates), c='g', label='Linear model')

plt.plot(dates, svr_rbf.predict(dates), c='r', label='RBF model')

plt.plot(dates, svr_poly.predict(dates), c='b', label='Polynomial model')



plt.xlabel('Date')

plt.ylabel('Price')

plt.title('Support Vector Regression')

plt.legend()

plt.show()



return svr_rbf.predict(x)[0], svr_lin.predict(x)[0], svr_poly.predict(x)[0]

Get the prediction and plot the predictive models

predicted_price = predict_prices(dates, prices, [31]) # (73.18055746816138, 74.23818331643184, 75.30920098568245)

If we want to check the close prices of TD stock on 2019–01–31, we can use stockai to get it.

from stockai import Stock

price = td.get_historical_prices('2019-01-31', '2019-01-31') print(price['close']) # $74.0

I’m surprised that the prediction from the linear model shows the best match because stock prices don’t usually follow the linear model. However, the prediction prices are pretty close to the real one.

This is the end of my research and practice for this topic. If you have any recommendations, feel free to start a discussion. I’d love to hear your idea and here is the code of this stock predicting example.