How to Backtest a Trading Strategy on Tradingview - YouTube

Channel: unknown

[0]
Have you ever thought to yourself, I wish I could instantly test my strategy with out
[4]
waiting months or even years, to see if my trading strategy is even good or not.
[9]
Well, what if I told you there s a way to test any trading strategy you want on years
[14]
of data from the past, instantly. To see right then and there if your trading strategy is
[19]
profitable or not. Instead of just believing a YouTuber that
[23]
says they have a 100% win rate strategy, why not test it for yourself to see if it really
[28]
is that good or not, and see how it compares to your strategy that you re currently using.
[33]
In this video, I m going to show you exactly how to do that. By backtesting your strategy
[37]
on TradingView with past years of data. So you can see just how successful your trading
[43]
strategy really is. Let s get straight to it.
[45]
First things first, we have to go to TradingView. I ll have a link in the description. And as
[49]
a side note, this is completely free to use and you don t need a paid subscription or
[53]
anything. Which is pretty awesome. Once your on tradingview, go to chart option
[57]
on the top here. Then type in any security you want to test
[61]
your strategy on. This will work with anything. Stocks, forex, crypto, whatever.
[65]
For this example, I m going to test the Vanguard S&P 500.
[69]
Now for this video we are going to test a very simple trading strategy. Just so you
[73]
guys can kind of get a grasp on the basics of backtesting. If this video gets a good
[77]
response and you guys really want to see more of this, I ll do a 2nd video where I ll get
[81]
a little more advanced. The trading strategy example we will do, is
[85]
just a simple moving average strategy. It s pretty simple. We have a 20 day moving
[90]
average and a 50 day moving average. We want to buy, when the 50 day crosses below the
[94]
20 day, and we want to sell, when the 50 day crosses above the 20 day.
[98]
Like I said, very basic and simple trading strategy, but this is just to show you the
[102]
basics of backtesting. I should also mention. That Tradingview has
[106]
a page where it has a bunch of pre bult in variables functions, so you don t have to
[110]
custom make all of them. We are going to be using a lot of them in this video. So I ll
[114]
leave that in the description as well if you want to check that out.
[117]
So once you type in whatever security you want to test. You are going to go down and
[121]
click on this bottom tab that says pine editor .
[124]
The very first line, is what you want to name your trading strategy. So, we are going to
[128]
name this simple moving average strategy . Next, we are going to set our variables. The
[132]
first one we ll call ema20 for our 20 day moving average and the second one we ll call
[137]
ema50 for our 50 day average. Next we are going to use the built in ema
[142]
function from the tradingview page I was talking about before. So we just type ema( . Now this
[148]
function takes two parameters. The first parameter is if you want the ema to read from the open
[153]
or close of a candle as a reference point. With moving averages, I think its best to
[157]
read from the close. So we ll do that for this strategy.
[161]
Then the 2nd value is the time frame. So for a 20 moving average we are going to set it
[165]
to 20. Then you re going to do the exact same thing
[167]
for this one, but make it 50. Next we are going to set our long and short
[182]
conditions. So we want to go long, if ema20 is above the
[185]
ema50. Then we want to go short if the ema 20 is below ema50.
[191]
So keep in mind, these aren t entry points, but they are just simply telling the bot what
[195]
we define as a long and what we think is a short.
[199]
To do entry points we are going to use another built in function, which is strategy.entry
[204]
. The first parameter is the name, so we ll
[207]
call this entry long, then the 2nd parameter is what we actually want to do, so we type
[212]
in strategy.long , the 3rd parameter is how many shares you want to purchase, so in this
[217]
example we will just do 1000 shares. Then the 4th parameter is when we want to
[222]
actually execute the trade, so we type when = long . Which is saying we want to execute
[227]
a long trade when these parameters above are met.
[231]
So we have the name, whether we want to go long or short, the amount of shares, and when
[236]
want to execute. Then we are going to do the same thin g for
[239]
a short trade. So name this short , strategy we want is short, still have it as 1000 shares,
[245]
then we want it to execute it when this short condition is true.
[248]
Right now we have it setup to where its longing and shorting. But it s not actually closing
[253]
positions at this time. It s just constantly opening trades.
[257]
So lets setup to where it actually closes them.
[259]
To do that, just simply use strategy.close . Then we are going to give it a few values
[264]
to tell the bot when we want to close a position. So we want to reference what position we want
[268]
to close, so we want to close a long trade, when short is true. Then we will do the opposite
[274]
for a short. So close out a short position when long is
[278]
true. So just all this code alone, in theory, will
[281]
buy every time when the 50 day crosses below the 20 day, and will sell, when the 50 day
[286]
crosses above the 20 day. It will also do the opposite with short trades.
[291]
Let s run it to see if it works. To do that, go to this tab up here add to
[294]
chart and that will start our backtest. So it looks like it worked. The cool thing
[299]
about tradingview, is that it will actually display on the chart where it bought and sold.
[303]
This way you can check to see if the bot is running correctly and buying and selling where
[307]
you actually want it to. You also have these 3 different tabs.
[311]
The overview tab will show you an equity curve, your net profit, amount of trades, max drawdown,
[316]
avg trade, etc. The
[325]
performance summary basically gives the same information, but in a table format.
[330]
Then theres the list of trades tab, where it will show you every single trade and when
[334]
and how it was executed. So you can see here, that we went all the way back to the 1970s.
[338]
So maybe you don t want to test all the way back to 1970? Well you can actually edit the
[343]
script to where you can tell the bot a specific amount of time you want to test.
[347]
To do that, just go to this line right here and create a start and end variable. Then
[351]
we are going to use the prebuilt timestamp function So we are going to set our start,
[356]
lets say 2019, august, 1, then hours, and minutes.
[361]
Then we ll just copy and paste this and set our end point to 2020.
[366]
So this will test our strategy from august 2019 to august 2020 on the s&p 500.
[372]
Then we have to make an if statement. So if time is greater than or equal to our start
[377]
variable and if time is less than or equal to our end variable. We will run this code
[382]
below. Then, you just indent this.
[385]
Go to the add to chart button, and boom, you see that it tested trades from only 2019 to
[394]
2020. Now maybe you want to see your actually indicators
[404]
from the bot on the chart. Well, theres actually a way to do that.
[408]
Theres a prebuilt function called the plot function . So you just do plot( then whatever
[413]
indicator you want, the name, the color, then the width.
[417]
Then boom, it shows you the moving averages on the chart. Which is pretty cool.
[420]
B ut maybe you want it actually overlayed on the chart as you normally would have it
[424]
when you add indicators. To do that just go up here and set overlay
[428]
to true. Then botta bing botta boom, you have the indicator
[431]
overlayed on the chart. Like I said guys, this is a very basic strategy
[435]
and example, but I just wanted to show you the very basic opportunities you have with
[440]
backtesting. If you want to see a more advanced tutorial
[442]
on how to back test some advanced strategies, like this video and let me know in the comments
[446]
if you want that, and I ll make a more advanced tutorial.
[451]
Because there s some pretty crazy things you can to with this tradingview backtesing.
[455]
Now that you know how to backtest, you should backtest one of my strategies. Like this one,
[460]
where I show you one of my secret strategies that has a high pretty high success rate.
[463]
Thanks for watching and I ll see you guys next time.