hello everyone and welcome to this video on the python programming language and machine learning so in this video i will attempt to predict the price direction of apple stock using a machine learning algorithm to create a machine learning model and then come up with a simple trading strategy so i'm currently on google's website it's called colab. research. google.
com and i'm on it because it makes it easy to start programming in python so all you have to do is go to this website and then log in using your google account and get started writing your python code so if you're going to code along with me go ahead and click on file then click on new notebook where a new tab will open up for you and then eventually a new cell will open up for you and in the cell i'm going to put in a description and comments so i'm just going to put this program uses machine learning to come up with a simple trading slash investing or investment strategy okay so i'm going to create a new cell by clicking this code button in the top left and now in this cell i'm going to import some of the libraries that i'm kind of using throughout the program so i'm going to import numpy as mp i'm going to import pandas as pd i'm going to import matplot live dot pi plot as plt and i'm going to import warnings and then i'm going to give my plot a style so i'll type plt. style. use and i'm going to use the 530 style and then i want to filter my warning so i'm just going to type warnings dot filter warnings and i want to ignore them so i'm going to put it ignore here all right and i'm gonna run this cell by clicking this button here to the left and this will let me know if i made any mistakes and it will run the cell for me okay so it looks like everything is good also before we begin or before we continue if you like the videos on the channel then be sure to click that subscribe and like button and to be notified about new videos on this channel hit that bell notification also you can get the code or data set or just support the channel on patreon at patreon.
com computer science and i will leave a link for that in the description below also the material in this video is purely educational and should not be taken as professional investment advice so invest at your own discretion alright so with all that being said let's create a new cell and now in this cell i want to load the data i want to load the stock data so i'm going to use google's library to do this so from google. colab i'm going to import files okay and i'm just going to type files dot upload to upload the file i'm going to run this and then click on choose files and i'm going to choose this aapl. csv file all right so that file is uploaded now i'm going to create a new cell and here i'm going to read in the csv file so i'm going to create a variable called df which will be short for data frame and i'm going to set it equal to pd.
read underscore csv and then i'm going to input the name of the file which is aapl. csv and i want to show the data so i'm just going to type df here to show it and let's run this okay and now we can see the data we can see the date column we can see the open price column the high price column the low price column the close price column the adjusted close price column and then the volume column okay and we have data from 9 17 2020 all the way to 9 16 20 21 okay and we can also see that this data set contains 252 rows of data and of course those seven columns that i just mentioned earlier all right so i'm going to create a new cell now that we have that data stored into df and i want to set the data i want to set the date actually uh to be the index so i want to set the date column as the index and to do that just type df. index and set this equal to pd dot to underscore date time and then input the date column okay so once i have that i want to then drop the the date column from the data set so let me see here i put pd dot to underscore date time this needs to be df not date okay and now i want to drop that date column so just set df equal to df dot drop and then input the column name which is date and then tell it which axis to remove and we want to remove the columns axis okay and now this should we should be done so i just want to show the data so i'm just going to type df here and let's run this okay so now we can see we have the indices as the date and then we have our columns up top all right so now we have six columns and we still have those 252 rows of data all right so let's go and create a new cell and now in this cell i'm going to create the independent independent variables so i'm going to create a new column called high minus low and i'm going to set this equal to the high price minus the low price all right and i'm gonna create another column called open minus close and we'll set this equal to the open price minus the close price all right and i'm going to run this and that's good so let's create a new cell now that we have those newly added columns and i want to store the independent variables that i just now created in a variable called x all right so i'm going to create a variable called x i'm going to set this equal to df and then i'm going to input the columns that i want which are open minus close and high minus low and i also want the close price okay and then i want to show the first five rows of data so i'm just going to type x dot head and let's take a look at the data set all right okay so now we have our x data set let's go and create a new cell and the x data set contains our independent variables that we're going to to be training the model on so now i want to store the target variable into a variable called y okay so i'm going to create a variable called y and set it equal to mp.
where df dot close dot shift minus 1 is greater than df dot close if that's true then we're going to put one else we're going to put zero so all i'm doing here is i'm checking to see if tomorrow's closed price is greater than today's closed price if that's true then we're going to put one else we're going to put zero and maybe i will put that here as well if tomorrow's close price is greater than two days today's close price then put one else put zero all right so this right here will give me my my signals so i will have a one for buying and i have a zero for no position okay so let's continue let's go ahead and show the data so i'm just going to type y here to show it let's run this and now i can see a bunch of ones and zeros okay so i think that looks good next i want to get the percentage to split the data so i want to split the data into 90 training or train data set and 10 test data set so i'm going to create a variable called percentage underscore split and i'm going to set this equal to 0. 9 for ninety percent and then we'll create a variable called row and set it equal to df dot shape at position zero so this will be the the length of the data set times that percentage so that's percentage underscore split and then i want this to be an integer value so i'm going to cast it as an it and this will allow me to split the data at a specific row all right so next i'm going to create the train data set so i'm going to create a variable called x underscore train and i'll set it equal to x and i want everything up until row from x and then we'll create the y train data set and i want everything from y up until row and then i'm going to create the test data set so i'll create a variable called x underscore test and set it equal to x and i want everything from row onward so i'm going to put row colon and then a blank and then i need to create my y test data set and so i want everything starting from row onward so i'm going to put row colon and then blank all right and i'm going to run this and then just so you know what i just now did let's go and create a new cell i'm going to show you x train all right so let's run this so you can see x train gets all the data from our x variable right if we go back up here we can see that our our x data set starts on this date 9 17 20 and that's exactly where our x train data set starts as well 9 17 2020 all the way until 8 10 20 21 and then what we'll see is that the the x underscore test data set will start after 8 10. so let's go ahead and run this and so we see now that the x test data set starts from 8 11 20 21 all the way to the rest of the data set in x which is to 9 16 20 21 all right and if you want me to i can also show you x so let's just type x here let's run this and so we see x goes all the way from 9 17 20 20 all the way to 9 16 20 21 okay so just kind of want to show you all what i was doing there and let's go ahead and delete these cells now all right i want to create a new cell okay so now it's time to create our model so i want to first import the library for the machine learning model called support vector classifier or svc for short and maybe i put machine learning model called all right so import library for the machine learning model called support vector classifier so from sklearn dot svm support vector machine i'm going to import svc alright and then i'm going to or next i'm going to create the model so i'm going to create a variable called model and set it equal to svc and just like that the model has been created and then i want to train the model so to do that i just type model dot fit and fit is another name for train and then i want to train it on the train data set so that's x underscore train and i want to use only the columns open mine is close and high minus low all right and they need to put in the y train data set as well okay so let's go ahead and run this and create our model alright so we've created and trained our model here let's create a new cell and now let's check the score of the model on the train data set so to do this just type model.
score and then input x underscore train and of course open minus close column and high minus low and then put in the y underscore train data set so let's run this and let's get a score okay so the score is about fifty four point four two percent and let's go ahead and get the score on the test data set so i'm just gonna come up here i'm gonna highlight all this and i'm gonna copy use control c and then come down here and paste it using control v and then just change a few things so instead of the trained data set i want to use the test data set all right and instead of x underscore train i want to use x on the square test and instead of y underscore train i want to use y underscore test so let's run this all right and we can see that the model is 61. 53 or has a score of 61. 53 for the test data set so it's a little bit better for the test for the test data set and i guess that's a little promising okay so let's create a new cell and now in this cell i want to make and show the model's predictions so to do this i'll create a new column called predictions and i'm going to set this equal to model.
predict and let's see we are going to get the predictions for the x data set so i'm going to put in x here and then he tell it which columns to uh use so that's open on open dash close and high dash low all right so it's going to use these two columns to make the predictions because these are the same two columns that we use to train the model on all right okay so next i want to go ahead and show the predictions so in order to do that i'm going to type df predictions and let's run this okay so now we can see some of the predictions let's create a new cell and now that we have all this information we can start creating the strategy and then compare it with just a buy and hold strategy alright so let's go ahead and calculate the daily returns all right so i'm going to create a a new column called return and i'm going to set this equal to df close dot pct underscore change and input the value 1 for one day then let's go ahead and run this and let's create a new cell and now i want to calculate the strategy returns all right so i'm going to create a new column called strat underscore return and i'll make that a capital r all right so that's a capital r now let's set this equal to df predictions dot shift um one right and then i want to multiply this by df return okay so i am shifting the prediction by one for getting the returns for that day right so the predictions are based off of a future date and now i'm actually getting the returns for that specific day okay so i think that looks good let's go ahead and show the let's show the strategy returns so just type df strat underscore return and let's run this all right and this is the strategy's return and of course just the buy and hold returns are here i didn't show that so let's go ahead and just show the returns so that's df returns okay so now we have our strategy our our strategy returns and then we have just the normal returns all right so let's go and create a new cell and now in this cell i'm going to calculate the cumulative qmo relative returns so i'm going to create a column called cumul underscore strategy and i'm going to set this equal to df strat underscore return dot com sum all right i'm just going to highlight this i'm going to copy us control c and let's paste it here using control v and this will be the this will be the calculation of the strategy cumulative returns and this will just be the normal one so here i'm just going to call this kumo rhett q more returns and this will be just return dot com sum all right okay so now uh we're storing the cumulative returns in a column called cumule return and we're storing the cumulative returns for the strategy in a column called qmo strategy all right so let's go ahead and let's run this alrighty okay so now time to create a new cell and now i want to visualize and show the data so i'm going to plot this so the plot is just type plt dot figure let's give the figure a figure size so the figure size will be 16 by eight and then i'm going to give the plot a title so i'm just going to call it returns and then i want to plot the data so i'm going to type lt. plot and the data that i want to plot are cumulative returns i'm going to give the color it will be orange the label will be equal to stock returns and then i need to do almost the same thing so i'm just gonna highlight this copy ctrl c and then come down here and paste it using control v and instead of cuemo underscore red it will be qmo underscore strategy the color will be purple and this will be the strategy returns okay then i want the information on the x-axis to be rotated so i'm going to type plt. xtx and i want the rotation to be 45 degrees and then i want to show the legend so i'm gonna type plt dot legend and i want to show the plot song type pealty.
show and let's run this okay so now we can see the stock returns in this orangish yellow color and we can see the strategy returns in purple here and it looks like the strategy returns are slightly better so let's actually create a print statement let's print the returns so i'm going to put here the stock gives a return of df cumulate at position negative 1 times 100 and then i'm going to put a percentage sign here and i'm going to highlight all this i'm going to copy control c come down here paste using ctrl v and i'm going to put here the strategy gives a return of qmo underscore strategy at that same position times 100 with that percentage sign so let's go ahead and run this okay and now we can see that the stock gives a return of about 34 and then the strategy here gives a return of about 37 or maybe 38 of you round up so that means that the strategy uh did better right and that's pretty cool to see so this is just a fun little project make sure to do some some more back testing on your own and try it out but please please use this as a grain of salt right think of this as a grain of salt this is just a fun project and is not intended for for for investing again if you're going to do anything with this just make sure that you do a lot of back testing all right so i hope you enjoyed this small little project and that's the end of the video if you would like to start an investment portfolio of your own you can click on the link below and get free stocks on weeble when you deposit 100 or more and you can also get 10 worth of bitcoin using the block file link below we need to deposit 100 or more so thanks for watching and thank you to the supporters supporting this channel on patreon. com computer science again if you would like to be a supporter on patreon.