In this video, I'd like to talk a little bit about backtesting. What is backtesting, and how do you use it to evaluate the performance of an algorithm? So, backtesting is probably the most key components of developing an effective trading strategy, and not just algorithms, you could develop and try to do this manually as well. You use historical data, and you execute your strategy as if you were doing it in real time. Then, you evaluate your day's performance as you go through time. This allows you to gauge the effectiveness of your strategy. It allows you to determine if you're making money or not making money. It also allows you the opportunity to fine-tune your parameters. I pick the number of days for this example arbitrarily, but you would want to try different days and different combinations to evaluate your strategy, and then not only that, to evaluate your algorithm against other types of trading algorithms. So, in this case, we used a moving average strategy and to backtest this, we're going to use the stock price from Advanced Auto Parts and carry out the following steps. So, we're going to calculate, in this case, our trading strategy is to calculate moving averages for 12 days and 26 days. We're going to convert the stock price to returns by calculating the log difference. Then, we check the position of the fast and slow moving average for each day and to see if there's a signal that's been generated. As I mentioned in the previous video, if the fast moving average is above the slow moving average, it's a buy situation; otherwise, it's a sell situation. So, this is more algorithmic implementation, but we denote a long position as a plus one and a short position as a -1. So, now we have the actual returns based on the historical data, we can calculate that by using log differences. We also know the positions that were initiated by the strategy, the signals that were generated. So, now we want to understand whether or not our algorithm is working. So to evaluate the performance of that algorithm, we multiply the position initiated on some day with the returns of that day. If we have a positive return on a long day, it's profitable and so is a short on a negative return day. So, it depends if the trend is going up or if the trend is going down. Conversely, we would have a loss situation. So, Backtesting the Trend-Following Strategy. We're going to execute the trades as if we're doing it in real time, and we will end up with some daily return for each trade. Then, the average of the daily returns will give us the expected daily return, from this strategy. We can study the volatility of the returns as well and the Sharpe Ratio by calculating the variance. This will also give us a better understanding of the performance of our strategy. So these are some of the metrics that we could use. So, I'm going to give you the results before I show you the actual R code. But, we used Advanced Auto Parts data from 2010 to the present. We calculated a return of about 13.5% with a standard deviation of 1.86, and we have a Sharpe Ratio of 6.2, which is the expected return over the standard deviation. So, looking at these results, using our choices of duration for the moving averages, it seems that our strategy is working reasonably well. So, before I show you the actual R code to calculate these values and implement our algorithmic trading strategy, I want to show you the results table, and this will also help to understand the inner workings of the algorithm. So, here we have the prices, the returns or the log differences. So, you take the log of the prices and take the first difference. We have the moving averages, simple moving averages for 12-day period as well as a 26 period. The up or down is really depends on, excuse me, if these short term moving average is above the long term moving average. So you see here. You can see here, there we go. 39.69 is bigger than the 39.29 for the long term moving average on that day. So up or down, you say it's a one. Then, you can see it switches down here, where the long term moving average is greater than the short term moving average. We code that as a zero. Equivalently, in plain English, we have buys here and then sells down here. We've also coded this 1 -1, and that's just to help the algorithm work. Then, in the last column, excuse me, we have calculated the returns of the algorithm. Okay, in the next video, I'm going to show you the actual R-code.