So, I'm here in the RStudio environment, and I'd like to show you the code used to implement the algorithm for trading. The libraries you'll need here are in the first few lines here. So just run these, and that'll load them into environment. If they not loading correctly make sure that you've installed them into your environment. In this case, we're going to pull the stock prices from Yahoo Finance, and we're going to pull it for Advanced Auto Parts, an auto parts company in the United States. We're going to put it into this variable name AAP. We're only going to use the sixth column. That's what that [,6] means. If I commented on and just run it, let me just show you what that does. We can see that here are all the stock prices, and we're going to use the closing price here in Column 6. So, you can use the open price, the high, the low etc. But we only want Column 6, so let's, and that's the adjusted closing prices, run that, and now if I look at that I should have just one column of data. Yes I do. We're going to calculate the returns, that's logged differences. So, it's simply that, I take the log of the prices, I take the log of each number, and then I take the first difference, and I put that into this variable I've named AAP for the stock price and then return. So, let's run that. I'm going to give it a column name. We can look at that data if you like. So, let's do that. There it is. There's that second column. We saw that in the PowerPoint slide. Just for the sake of brevity, I'm just going to show 2010 data and forward. Now we can plot it. There it is. So, there is a plot of the Advanced Auto Parts from January 2010 forward. Now, we're just going to calculate our simple moving averages. We saw this in, I think, it was Week 2, but the simple moving average command SMA, open PREN, what column vector, what data are you going to use, and then the number of periods you're going to use to calculate that moving average. So, in the first line here SMA 26, I could calculate a 26-day moving average, and then the next line I calculate a 12-day moving average. So, let's do that. So now I have two column vectors. We can look at one of them if you'd like and there it is. Notice that there are all these NAs for the moving average on this 12-day once. So there's the first 11 are not there. I'm going to put them all into one dataframe. A one giant table. That's what this bind column bind function does. So it creates a column with the actual prices, the return, then the moving average for 12 periods, and then the moving average for 26 periods, that'll treat it as a dataframe. Then this NA omit, the function of this command is to get rid of these NAs here. So, these null values or NA values in the table will make functions act erratically. So, we're just going to get rid of those. Let's do that. Run and column name became some column names. So, now we can look at our data. This is the table that you saw a snippet of and the PowerPoint slide, in the previous video, and the first column has the prices, the second column has the lot of differences, and the two moving averages. So, condition for trend following strategy. So, we're going to add another column up down, and this if else command works just like the if else command does in Microsoft Excel. So, here's the condition, if the short term moving average is greater than or equal to the long term moving average, code is one, otherwise code is zero. So, it just looks at those two columns. If short term moving averages are above the long term moving average, it's a one. So, let's run that. We can look at data now, and we should see that column up down, and short term moving average is bigger than the long term moving average equal to one. Let's look for a zero down here, 40.05 is less than 40.08, so it goes to zero. So if that's working. There's a typo, devise a trading strategy and back tests. So, here's where we actually put our logic in here if the up-down code is equal to one, we're going to code it as a buy; otherwise, it's a sell. Here our position, if our trade is equal to buy, it's a 1, sorry, or a -1. We'll be using this in a second, in fact, in the next line. Our algorithmic return is equal to our calculated return, the log differences multiplied by our position. Then, we annualize the return and plot it. So, let's run these commands. Here, we can see the plot of the returns, here is our SharpeRatio, I'm assuming a risk-free rate of 0.02. That's something you're going to want to look out. You could use a government bond or something like that, to calculate the risk-free rate. SharpeRatio, so, it's the annualized return that we'd calculated, minus the risk-free rate, which we have looked up somewhere, and we divide that by the standard deviation, which we calculated here in line 40. All right. Then, we can print the results. Let's do that. So, we have a little message here. The trend-following algorithm was applied to AAP stocks, Stan was able to achieve an annualized return a 0.13 percent. So, I think it's more interesting to maybe plot the data. So, let's do that. Let's make the spring a little bigger. So, that's the actual prices and read. Next, we're going to plot the short term moving average in green, there it is, and then the long term moving averages in blue. You can see the long term moving average is a lot more smooth, smoother than the actual data. You'd want to zoom in make a graphs zoom in so that you could see what the blue line is crossing the green line etc. So, I hope you enjoyed this algorithm, I'm not suggesting by any means a little disclaimer that you actually trade on this strategy. I do think it takes a lot of experience to develop the right signals. Some companies are actually very successful at this and a lot of companies are not. But it does help us to understand algorithms signals, and it does help us to tie together some of our past learnings in this class, and wrap it up in a nice little bowl.