Data Analytics of Stock Price Movements with Python

Backtesting a Trading Strategy

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

Autoplay:
View previous videoPrevious video

This video ends with the development and testing of a simple technical trading strategy.

Keywords

  • backtest
  • technical trading strategy
  • trading signals
  • stratgey performance

About this video

Author(s)
Matthew Macarty
First online
07 March 2020
DOI
https://doi.org/10.1007/978-1-4842-5647-3_8
Online ISBN
978-1-4842-5647-3
Publisher
Apress
Copyright information
© Matthew Macarty 2020

Related content

Video Transcript

OK, we’re going to finish up our applied section and actually the whole course with this simple method of back testing some trading strategy. And let me just say that I am not saying you should go out and try what I’m about to show you in practice. Investing is risky. You can lose money.

But we’re just going to go ahead and show you how you can test any system that you’re trying to set up and apply. OK, so we’ll import our libraries. And for this demonstration, I’m going to be going back and getting about five years of history for the gold ETF. And I’m just going to process it quickly by dropping off that adjusted price column.

And then we’re going to go ahead and add a couple columns. So our system’s going to be based on moving averages. So I’m going to do a nine day. I’m going to run it on the open here. And then I’m going to do a 21 day.

OK, so for our simple system, we’re going to say, oh, when the nine day crosses above the 21 day, then we should go long. And when the nine day crosses below the 21 day, we should go short. So our system is going to cause us to always be long or short.

And yeah, it’s not going to really be representative because we’re going to ignore things like, oh, we couldn’t get the stock at exactly the price that we thought we could. We have transaction fees. So we’re ignoring a lot of stuff like that. And then I’ll just add our return as our exponential instantaneous rate of return.

OK, so there’s our columns. Let’s take a quick look at it. OK, so everything seems to be looking the way we expect it to. I’m going to go ahead and drop off those missing values. There’s how it looks after we get rid of the missing values.

And then I’m going to just visualize what this trading system looks like. So I’m showing you another method of setting up a plot. So I’m going to– instead of using seaborn, I’m going to use one of the built-in themes that comes with Matplotlib, and there’s a number of those. So I’m going to get ggplot, set a size, and then I’m just going to go back roughly about a half a year here to see what it looks like. I’m not going to go back the full five years. And then I’m going to plot our legend.

OK, so this is just a warning message. And I mentioned this in an earlier video actually. So it’s asking us to run these two lines of code, which I have been doing. But I wanted you to see this error message or, excuse me, this warning message you might get about dealing with dates in Matplotlib. If I run it again, it’ll suppress the error message.

OK, so this is what our system sort of looks like. The blue line is our nine day, the purple is our 21 day, and then it looks like, then, the system told us to go long here back in June sometime and then stay long and then get out and go short in September. We would have had a number of these trading signals going back in time if I graph the whole five years.

So now we’re going to add a column that makes us always long or short. So I’m just going to call it the– I can call it strategy or regime, something like that. And I’m going to use mp.where here. And I’m going to say if gold nine day is higher than gold 21 day, then I’m going to use one to represent I’m long, and then otherwise it must be less than the 21 day. So I’m going to use negative 1 to represent. So we’ll add that column.

And then we’re going to calculate our return, which is going to be that instantaneous rate of return, and then multiplied by whether or not we’re long or short. So we’ll call it gold strategy and call it straight return. How about that? OK, so it’s going to equal gold return times the gold regime. And we’ll just get a look at that.

So just looking at the first few rows, we can see the return pretty close to zero. It says we’re long, and so we would get that return. And then if I look down here, we’re still long, but we would get actually– we’d get a negative return here. So the return’s going to bounce around, and we’re just going to have to take the daily fluctuation in stride.

OK, with that done, I’m going to go ahead and plot the performance of the system versus the performance of buy and hold. And let me put the legend on there. OK, so we can see there’s a big difference in buying and holding versus using our nine day, 21 day crossover system. So it looks like you are quite a bit better off if you use the system. Now, this, again, doesn’t speak anything to how many trades you made, how much you paid in commissions, how much slippage you had, and whether or not we are trading on information that we don’t really have.

If we want to see this boil down to a single number, compare directly what this graph endpoint is, I can generate a couple of outputs here based on what happens with buy and hold and what happens if we’re in and out of gold based on our signals. So we’re up 150%, a little more than 2 and 1/2 times our initial capital, versus we’re up 117%, a little over twice our capital at the end. So then the question is, will this work in practice? And that is something that I wouldn’t want to speculate on.

So that’s our quick course on data analysis of stock prices. So we saw how to analyze the stocks, and then we saw how to sort of apply some of the statistics and numeric values we were getting. I hope this helps.