How to Backtest any Strategy for Free in Trading View - VRD Nation (2024)

So guys welcome to VRD Nation today. We are going to learn how to back-test any trading strategy for free using the trading view. Now trading view for those of you who don’t know is a web-based platform where you can do tons of cool stuff? you can do charting create alerts create strategies and of course, you can do backtesting.

Now there are a couple of reasons why we are using the trading view. Number one is that it’s free. I’m using a free version of the trading view.

But more importantly, the beauty of the trading view is that you get the cleanest data possible. Before trading view one of the biggest challenges that I had was finding a source where I can get clean historical data not just clean for the past six months or seven months. Going back let us say five years 10 years or 15 years. It’s very hard to find good clean reliable data from other sources.

That’s the reason why we are using trading view. if you’re new to trading view you don’t know how to use it. Don’t worry because we have made a series of videos that explain every aspect of the trading view. we started from the basics. Then we covered the chatting functionalities in trading view and then we made a couple of interesting videos on the alerting functionality.

This particular video here is the 5th one in the series covering backtesting. just go through this watch list from top to bottom and we will be all caught up.

With that said let us get going with backtesting. what is backtesting? Backtesting is the art of checking whether a strategy would have performed in the past or not. two words back and testing. you’re testing a strategy looking backwards.

What you’re essentially doing is you are trying to create a strategy. You then apply that strategy to the data of the past 2,3,5,10 or 15 years and you want to see that if I would have taken this idea this strategy and applied it with my own real money with let us say 1lakh or 10 lakh rupees how much money would I have made or I would have lost.

It is that simple. We are basically doing simulation. logically speaking backtesting should be the first place to start for the new trader. That unds like the most obvious thing to do but the unfortunate reality is that the majority of the retail traders don’t do any backtesting.

I think the biggest reason for that is lack of awareness. People feel that okay and maybe it is too technical and too expensive. Of course, a lot of people feel that I don’t know how to do it and don’t know where to start. What I have seen is that there is a lack of knowledge and a mental block when it comes to backtesting.

In this session, we will break that mental block. This is what we’re going to do. We will take a very simple strategy, create that strategy in trading view, backtest it and find all the faults in that strategy. Then we will fix those faults and backtest it again till it gets better.

Don’t worry. We will do this systematically slow and steady. Even if you have no technical background no problem and you would understand everything that I’m going to say because it’s going to be super intuitive and easy to understand.

What is our strategy? We will be using the moving average crossover strategy on Nifty.

Now the strategy is very common and simple. I’m not telling you any secret strategy and it’s been out there for a very long time. The reason I’m using this is that it’s easy to explain and it is easy to understand. In this strategy, we will work with 2 moving averages, a 20-day moving average and a 50-day moving average.

It can be a simple moving average or an exponential moving average and that doesn’t matter. When the 20-day moving average crosses above the 50-day moving average, we will take a long position in the nifty future and when the 20-day moving average crosses below the 50-day moving average, we will exit that long position and create a fresh short position.

This is a very simple trend following a system where these crossovers are indicating that the trend is picking up on the upside or the downside. The strategy is very simple. Now let us build this strategy in trading view. The first thing that we’re going to do is of course look for a nifty future.

We will go to a Nifty future. I’m going to pick the current contract and launch the chart. Here is a daily continuous chart for the nifty futures. We can just click on “All” and can see that we have data back in 2008. We have plenty of data to do back-testing on. Now how do we build up a strategy?

You see this little pane here. I’m just going to pull it up a little bit. Here we have a couple of tabs that we have to pay attention to. Number one is the Pine Editor. I’m just going to pull it up now. Pine editor is the place where we will create our strategy and strategy tester is where we will do our backtesting.

It starts with the Pine editor and here I’m going to paste the strategy which I have created for the moving average crossover. Another focus of this video is not to how to create a strategy because that is going to be the next topic in this series of videos. The focus of this video is of course on the backtesting.

That is why I have not spent a lot of time explaining how I wrote this code but let me just give you a high-level idea of what this code is doing. On the top, I have given a name to this strategy and then I have specified the date ranges between which I want to test this strategy. I’m saying that I want to apply this strategy back from 2007 till 2021 June.

Then I have defined the 2 moving averages, 20-day moving average and 50-day moving average. The condition for taking a long position is that whenever the 20-day moving average crosses over the 50-day moving average, that is when we enter into a long trade.

So when do we get out of a long trade? Whenever the 50-day moving average crosses over the 20-day moving average. Simple. .and exactly the reverse is for the short trade.

Now what I will do is I’m just going to bring this guy down and add it to the chart. I’m just going to click on this button.

Now you can see I’m just going to bring this thing down first. You can see on this chart the long trades and the short trades. We can visually see how our trades were taken at what point in time and the 2 moving averages are here of course. The blue one is a 20-day moving and the green one is the 50-day moving average.

Now let us focus on the backtesting. That is what we are here for. For that, we will go to the strategy tester and I’m just going to expand the screen up. When we come to the strategy tester tab there are three tabs that we need to know.

One is the overview tab. The other is the performance summary tab and the third one is the list of trades. We will go one by one. Let us start with the overview tab. When we come to the overview tab there are a couple of graphs that we see here. The one in the red is called the drawdown graph and the one in the blue is called the equity curve.

What you can do is you can always hide by clicking on equity and you can hide the drawdown graph as well. Now, what is the big picture?

See whenever we are backtesting a strategy we need to find faults in that strategy. We need to see where this strategy is lagging so that we can fix those problems.

We can then come up with a better strategy the next time. Let us put on those critical caps that we can find faults in our strategy. Let us start from the beginning and this here is the limited net profit. It is telling me that I have made 478.51% of net profit and that is very impressive.

That’s one of the reasons why people like this strategy much because it is of course profitable in the long run. Let us say I don’t see a lot of problems with the net profit. It’s a profitable strategy and I’m just going to leave it like that.

The second thing is the total number of trades. Now if I’m taking, let us say, thousands of trades because of this strategy, that would have been a problem because for every grade that I’m taking out, I have to pay commissions, taxes and that can add up a lot when it comes to charges but this strategy only created 57 opportunities. So far good.

Now the third one is the percentage profitability. Now, this is a little bit concerning for me.

What it is telling me now is that this strategy is only profitable 36.84% of the time. This means if I took let us say a 100 trades and out of those 100 trades, only 37 trades will be profitable. The remaining will be in losses. That would be the first fault that I found with my strategy and that I need to improve the percentage profitability of this strategy. But let us just pause here for a second and think about it.

If only 37% of the trades are profitable how is it possible to have made 478.51% in the long run? Well, the mystery behind that can be resolved if we understand the logic of the profit factor.

What is the profit factor? The profit factor is a ratio of the gross profit divided by the gross loss. What we’ll do is let us go to the performance summary tab and here you can see this is the gross profit that we made from this strategy and this is the gross loss that we made from this strategy.

Let us just do one quick math here. What is the total gross profit? Rs.8,99,627.5. Let us divide it by the gross loss which is Rs.4,21,117.5 and we get 2.136 which is exactly what our profit factor is.

The good thing about this strategy that we have just noted is that even though it is profitable hardly 37% of the time, whenever it is making a profit, it is making much bigger profits and almost 2 times profit than the time is incurring losses. The strength of the strategy is the profit factor. It means it’s making much bigger profits than the losses. I don’t see any fault with the profit factor and we can move on from here.

But here is where we have a big problem with this strategy. You see that maximum drawdown of 52.29%. That is a big red flag for this strategy. Whenever you’re doing backtesting you need to focus a lot on the maximum drawdowns because it’s a very very important concept to understand.

What is a drawdown? Let us say for example you started with the capital of one lakh rupees and this is how your account has been growing. You went up to let us say 1.2 lakh rupees and then to 1.5 lakh rupees. Then you came down a little bit to incur my losses. You came down to 1.3 and then went up to 1.7 then 2. From 2 again you came back a little bit to 1.8 and then let us say you went all the way up to 3 lakh rupees. From 1 lakh rupees, you went to 3 lakh rupees and you made a lot of money. The times here when you incurred losses are called drawdowns.

As you can see here that in this journey you had a couple of hiccups and these are very small drawdowns but let us say now that you’re sitting on a capital of 3 lakh rupees. Let us say you incur a loss of 1.5 lakh rupees. From 3 lakh rupees now you’re down to 1.5 lakh rupees. Do you think that is a problem?

That is a problem because from 1.5 lakh rupees, just to go back to where you were earlier,i.e. 2-3 lakh rupees, you have to make a profit of 100%. . That is exactly the problem with this strategy. It is giving me a maximum drawdown of 52% which means that at any point of time in this journey I lost over 50% of my capital.

Let us take a look at the drawdown graph here. This drawdown graph is very interesting and this is the axis on which we measure the drawdown. This is zero. This is -100. This is -200,300, 400 and 500. Somewhere around here, we incurred a loss of 50% and then, of course, the loss was recovered and we became profitable in the long run.

But the one interesting side question for you guys and I want to see how many of you answered this correctly is that let us say you had a capital of 3 lakh rupees and you lost 50%? Does that make sense? It came to 50%. Then why on this axis do you have -200,-300 and -400?. Shouldn’t this graph be between 0 to -100?

If you have a capital of 3 lakh rupees, the question to you guys is that can you lose more than 3lakh rupees? Is it possible? Let us know in the comments below and I will give a detailed response to that in the next video. Anyway, this is the second fault that we have identified with this strategy and that is the maximum drawdown is way too high. It is unacceptable. Now the other thing that we have to notice here is that when you click on this blue thing you get the equity curve and the equity curve is very straightforward. This is how your capital has been growing.

From 100, it went up to 578.5 which is how we reach the profit of 478.5%.

Now one more thing is very interesting here which is the buy and hold equity. It means that all those long-term investors who just bought and did not trade at all. How are we performing compared to them? If somebody bought exactly at the same time when we bought and did nothing; no longer trades and no short trades.

No long trades and no short trades and just sat on it. How are we doing compared to them? Well if somebody just bought and held, they would be making a profit of somewhere around 300 and we of course are making much higher profits compared to them now. This is another parameter to analyze your strategy with.

You are doing all this effort; you’re doing all this backtesting, analysis and research. But if at the end of the day if a passive investor is not doing much and if that person is making more money than you, then whatever you’re doing is useless. So this here is showing that yes, whatever we are doing is better than this passively sitting person. That is the overview tab.

Now let us go to the performance summary tab. Now in the performance summary tab, there are 3 columns here. The “All” column shows us the net result of all the trades that we have taken and the long and short columns are giving us the breakdown of this “all”, for example, the gross profit here.

Out of the gross profit of almost 9 lakh rupees, we can see that almost 7 lakh rupees are contributed from the long trades and about 2 lakh rupees are contributed from them short trades. But now my eyes are immediately going to fault number 3 and that the majority of the profits that I’m making from this strategy is coming from the long trades and the short trades are incurring losses.

That to me is a problem because especially I like taking short trades and what does backtesting telling me? That my short trades were not working at all. I incurred a loss in the short trades. That is fault number 3 and we need to rectify this strategy. Now let us just understand this a little bit more.

These rows are very intuitive here; net profit, gross loss, maximum drawdown. We already talked about this. How much have we lost at any point in time, buy and hold return? We also talked about this.

If somebody if a passive investor just bought and held onto it, they would be sitting on a profit of 212% which is of course much less than the profit that we are making as of now.

We also talked about the profit factor. Sharpe ratio is more applicable for investors. I’m just going to ignore it for now. Then you can find more details such as the number of winning trades, the number of losing trades, average winning trade and average losing trade. You should also spend a lot of time digging deeper on how you can make this strategy better.

As far as I’m concerned, I already have identified 3 faults with my strategy. First is the percentage profitability. Second, the big one with the maximum drawdown and third is of course the fact that I’m not making money from the short trades. Now it’s time for me to improve my strategy.

Now that I know that these are the 3 areas that I need to improve, I went back to the code and identified all those places that will directly impact those 3 specific areas. I then came up with a new code. What I will do is I will just paste that code here and save it.

First of all, just give it a name and save it. I will of course make sure that there are no errors. Now let me apply this to the chart and let us take a look at the results. Now things look a little bit better.

First of all the net profit itself has gone up which is a very encouraging sign because I think we were somewhere around 478. From 478 we have already gone up a 100%. They have improved the profitability of the strategy but our main focus has to be on those three faults.

The number 1 fault was with the number of profitable trades. Earlier we had barely 37% of profitable trades and now that number has significantly improved to 47% and that is a big win.

Even the profit factor has gone up. I believe that the profit factor was somewhere around 2.3. From 2.3 the profit factor has gone up to 2.6. Fault number 1 has been addressed and it had no negative consequence on the profit factor.

Now let us take a look at fault number two2 which was with the drawdown. Remember the drawdown that we had with version number 1 was about 50%. and now from 50% drawdown, the maximum drawdown has come down significantly to 32% which again is a big win. Fault number two has all been addressed in this particular version.

Now the only thing that is remaining is our fault number 3. Let us go to the performance summary tab and here you can see that fault number 3 has all been taken care of. It’s the short trades that were unprofitable the first time. Now they have become profitable and they’re contributing to the overall profitability of the strategy.

This version of the strategy is doing much better as compared to the previous version. Now the one thing that you can all do is that it can go to tab number 3 which is the list of trades and here you can see the details of every trade that was taken as part of the strategy.

That will help you pinpoint exactly what is going on, where the big losses are to make the strategy even better. So this is how a typical backtesting cycle works.

Now I couldn’t cover all the aspects of backtesting in this video. There are a few things that we couldn’t cover here and there are all custom-defined input parameters that we can define on our own.

We will see if you guys are serious then we can spend more time understanding backtesting in more detail. But in any case, I hope that you guys have learned how to do back-testing, analyze your results and how to improve and enhance your strategies.

That’s the beauty of it. I mean everything is for free. The only thing that you have to do is just to put in a little bit of your effort and learn something new.

How to Backtest any Strategy for Free in Trading View - VRD Nation (2024)

References

Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 6085

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.