Tradingview Pinescript Version 5 FULL TUTORIAL 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi quants so today we'll do a full course on the version five of pine script so trading viewpinescript has created a new version version five and i've looked through youtube and i couldn't find a single video about it because it's quite new and we need to change we need to update ourselves in newer versions because after a while they might release version six and that'll be similar to version five and not similar to version four so it's very good for you guys to watch this video get to understand how the game has slightly changed the new developments in the pine script so you can code better most of the new indicators now are all written in version five uh by trading view so it'll be really good for you guys to adapt to this version five so i'll just go to the basics from the beginning till the end so we'll discuss it from the basics so anybody who's new to programming or who is not very efficient in pinescript they all can learn but if you know some of the some of the factors that i'm discussing some of the variables that i'm discussing you can keep on moving forward so we'll just start with the basics operators so when you when you create open a new blank indicator this is what you get so what i'm going to do right now is i'm just going to write a equals b equals 5 and then let's plot a so we'll just save it as my script and then let's add it to the chart so what you see is you can get 5 in a single line here so let's say i want to change a equals b equals a plus one so that plus sign adds the value of a to one so now if i plot b what i'll be getting is 6 instead of 5 because now i'm plotting b now let's do another thing let's make a change to a so a is 5 here now i want to make a to something else so let's say a equals um 6 now now the problem is now if i do this there will be an error because a is already defined so what you should do is you need to have a colon here and then equal to so basically whenever you define a before and you want to make a change you need to use the colon and equal to so now when i add it to the chart now a becomes 7 because 6 plus 1 equals 7. so whenever there's a change when you plot it the a has to be put colon and equal to so just like that you can do is multiplied and divided by all these things can be added to the some of the most common operators so now you get 18 because 6 multiplied by 3 equals 18. so i'm going to close some of these so plot is a function that's available in trading view so when you put plot here b um you basically get the graph being put there so that's for the simple operators of plus or equal to now let's take a bit further i'll teach you something else now so that's called um let's declare b equals five and then i'm going to do something called a plus equals b so now what this means is basically um let's do a comment here it's basically a equals a plus b but we can write in a very short code which is a plus equals b so basically it will take a is five and then adds five to that of b and again let's run this see what you get now let me add this to this chart b is equal to 5 we've just asked to plot b so there's no point there so let me change this to a so you get 10. so they've added a equals a that's 5 plus 5 which is equal to 10. so similarly you can do the multiplication as well so then you'll get 25 so that'll be a equals a asterisk b so let me save that and add this to the chart there you go so a equals 5 multiplied by 5 that is equal to 25 so these are some of the common operators so now let's do our first if-else condition so basic syntax for if-else condition is if and then basically there's a condition set uh and then we do what we have to do if that condition takes place so let's say if a is greater than 7 and then let's do a tab which is basically a syntax so put tab here and then let's change the value of b so as said before you need to put a colon if you have to change the value to a different already already declared variable so b will be equal to b plus a so basically we'll get 10 plus five and then let's get out of this loop and then let's plot b so basically uh what it does is it checks the condition if a is greater than seven and if it's true then it changes the value of b and then it plots b so let's save this and decided to the chart so basically we get 15 because b equals 10 plus 5 which is equal to 15. so a is greater than 7 because a is 10. now let's make a small change let's make a equals 5. what do you think is going to happen it's going to go to the loop it's going to check the if-else statement uh and it's going to check is a is greater than seven no a is not greater than seven so this this uh won't be performed so b would basically be five so let's save this and let's see what we get in that chart so as you can see b is 5. so this is a very simple if statement so i can add brackets to this and still it gives out pretty much the same result now let's make a small else condition as well so we'll have else space um there's no no conditional we're not going to declare any condition and again we're going to put a tab and instead of this we're going to do we're going to copy paste this and instead of a we're gonna add two so what happens is that five plus two will be seven so either will get seven or you will get uh the previous number so let's save this and let's see what we get so now we've got seven here because a is less than seven so this condition is not met so the else condition is met so then b becomes b plus two which becomes seven five plus two equals seven and then we plot b and that's what we get so let's do our else if condition as well just to give you an idea so if you put else if a is less than 7 will get the exact same result because a is now five so what we'll get will be b equals b plus two will be seven so let's say a equals three and i'm going to put a condition a equals less than or equal to 3. now because i put an equal to sign and a equals 3 this condition will still be going through this else if condition is going to go through so now we'll get seven so let's create another else statement and we'll do the exact same function but we'll just add three so that we can i kind of get a an idea of what difference this makes so let's save this so i'm going to explain it to you exactly how this works so if a is greater than 7 b equals b plus a so we'll get 8 there uh a is less than or equal to 3 we get b equals b plus two and any other situation b equals b plus three i e if a is between uh four and seven we will get this function going on this else condition going on so in this case a equals three so a is less than or equal to three so this else if statement is executed and then we get five plus two equals um seven so let's change this to a equals four let's see what we get so in this case this condition is not met a is greater than seven no that's not met this condition is on med a is less than or equal to three so basically we go through this condition which is basically four plus three which is equal to eight so i hope you guys got a general idea of how if else if an else condition works now we're gonna add a bit more complexity now in the next one we're going to add an and condition so let's close this let's create c equals 2. so we're going to do if a is less than equal to 3 and c is less than or equal to 2 b equals b plus 2. so let's save this see what it works it does work so what happens here is gonna so in this elsif condition it checks a is less than or equal to three which it's not because it's actually 4 but c is less than or equal to 2 because c is equal to so this condition is not met is this condition met no so basically what we get is plot b and b equals five here so basically we get b equals five here so let's make this change here to three and let's see what we get so in this case we get seven why did we get seven because a is less than or equal to three that condition is met c is less than or equal to two that condition is also met so this b equals b plus two is executed which is five plus two which is equal to seven so this is a very simple and condition so similarly we can do an or condition so our condition is either one of these conditions are met and you basically get this executed so you can actually create a brackets for this so you can really make it more simple the coding becomes simple just as we did brackets here um so either of these two conditions are met so let's make this a four let's save this see what we get so basically is a less than equal to 3 no it's not but since we've decided an or condition c is less than or equal to 2 because e equals 2. so b will be equal to b plus 2 which is equal to 7 and that's what we've got here so there you go it's a very simple if else if we also discuss and or or condition as well so this is amazing how much things you can do from the if else statements so let's create something else let's insert a plot b let's create something called plot close i forget about all these things none of these conditions are met now we're just going to plot the close so basically what we got is the close of every single day drawn on a line um none of this is gonna be plotted here because we haven't deserved we haven't told them what to plot so we're just going to plo close so similarly we can do the open we can do the high we can do a low so that's another thing that you need to be aware with open close eyes are some of the things that we'll be working a lot in our coding um these are all built-in uh variables so understanding that is very important in our next step in coding because we're going to do some practical examples so let's now add a small change to what we're going to do we're going to remove this plot open here and then what we're going to do is we're going to plot b but we're going to decide what b is going to be here so if a is greater than 7 we're going to put b equals close and else if this condition is met we're going to put b equals open now let's see whether this works no it doesn't work because b was declared as a simple int type cannot assign it expression of type series float so what does this mean so as you know close and open has got decimal points like 0.8 1.78 integers on the other hand are like single digits like four five two so what we need to do is that we need to change the b to a float so in order to that we just use our built-in function in point script by writing float p equals five so now we made we decided that float b will be a float so now if we save it and we run it there shouldn't be an error so there you go so the condition if a is greater than seven no a is not greater than seven a is actually four so this condition is also this condition is matt notice conditional met this condition is met c is less than equal to n because it's an or b will be open so this line graph would just be the open so and we're putting plot b there so that's the idea of float so it can also be a boolean as well so boolean is basically true or false so basically the only thing you need to remember is uh integer is numbers four five six seven uh boolean is true or false that's bool and then float is basically decimal points and string is just basically alphabets like or letters like a b c d that's a string so i hope you understood the difference between float and int let's now add a for loop so i'm just going to delete off this if else just to make it more simple so let's say for i equals one two three and then you do a tab and then b equals b plus one so i'll explain to you what this works so four is a thing called loop so there are many loops there's four loops there's one loop so now we'll discuss what a for loop is so what a for loops does is um it goes through this loop multiple times as decided by the number here so here it is doing the loop thrice so from one two three so first it goes b equals b plus one so which is five plus 1 will be 6 and then again it goes through 6 plus 1 equals 7 and then 7 plus 1 equals 8. so that's how the loop works so after three loops it comes out of this and then it plots b so let me save this and see what i get so as you can see i've got eights here so what if i do um an addition to this like here the step by default is one so one two and three so how about i do by two steps we'll just start with just one step so it'll get exactly the same value because by default its step is one so you can see again it's eight so let me change this to five and let's step this by two so what we get here is eight so i'll explain to you how this works again so the first the loop is i equals one so b equals b plus one so that's five plus one it'll be six and then the loop adds by two so now i becomes three so again six plus one equals seven next the loop again goes to five because the step is two the step before was three so add three to uh two it becomes five and again six seven plus one becomes eight so the final value we get is eight so let's see if i change this to six now i'll get the uh exact same value eight because after the one loop goes to three and then it goes to five then it goes to seven but then we only have declared that it to go to only six so the val the loop doesn't go again if that makes any sense so that's basically for loop so if you're taking this out it's basically by default as step by one but if you actually want steps to decide on how many steps you want you can do two three four uh whatever number you want so this is the initial one which is the one and that's the maximum one which is including the maximum the maximum is always included in the full looping point script so that's about it for the for loop it's a very simple syntax normally we don't use it much but in our course there's one strategy a bit complicated strategy which we use the for loop but again it's good thing to learn about the for loop try applying this uh wherever you can it's just kind of good for coding now let's discuss a while loop so a while loop is something new to version five so if anybody wants to do a while loop uh this will be the way to go so uh let's start with a simple while loop so in this case i'm gonna do while um a is less than let's say 30. um a equals a plus one and then let's plot a so if i save this what i get is 30. so what happens here is um a is now 4 so 4 is less than 30 so every single time this loop goes by ie 30 times because a becomes first 5 and then 6 and then 7 it waits till 30 until it becomes 30 and then plots a so that's why you get 30 here so what if i choose a equals 100 what i get is a equals 100 because the a never entered this loop because this loop has a condition and that condition was a is less than 30 but a is actually 100 here so it skipped this loop and went straight to plot a so now let me do something different i'll just make it slightly different complicated so instead of a is equal to a plus one we'll also do b equals b minus 1. and let's plot b i'm just going to give this um zero let's see what we get so we get here minus 25 so how do we get minus 25 here so first a equals zero so it checks this and yes a is less than 30 because a is equal to zero so it adds a uh becomes the new a becomes one and the new b becomes minus four and this keeps on going so minus four uh minus three minus two minus one all the way to minus twenty five because the loop repeated 30 times and we started at b as 5 so 30 minus 5 will become 25 and hence it becomes -25 so that's basically while loop again it's i like to use if else statements but then some people prefer while loop as well but again it's again a good addition for your coding skills the other thing to look for is the break statement so what a break segment does is that it gets you out of the while loop so in this example if i can change this to an if condition if a is greater than 25 and then if i plot a and then let me do a break here so what happens here is the moment a reaches 25 um greater than 25 it gets this break thingy gets out makes this gets out of this while loop completely so now when we plot a what we'll get is 26 so let's let's give it a go and see what you get so there you go we get a equals 26 so a was initially zero and the while loop gets in because a is less than 30 so a is added to 1 so it becomes 2 3 4 it goes all the way to 25 but once it reaches 26 this break condition is executed and it gets you out of this while loop and then it just blots a so let's now discuss the new addition to point script version 5 which is basically the switch statement so what the switch statement does is that it kind of is similar to the if else condition but then it contracts the um it reduces the number of lines of codes that you need to pull this off so i'll give a simple example so let's say if a equals to equals to 10 then we use an operator here b will be equal to 1. and so basically what this operator does is that it's used in user-defined functions and also in switch operator so it checks the condition if a equals 10 and if that condition is true then b equals one so basically it's like a it's like an operator to use uh to check these two things and then do this task here so then let's do a equals equal to 20. the same principle applies actually put an additional equal to that and then b will be equal to 2 and if a equal to equal to 30 then b is equal to 3. so now let's plot a here i mean plot b and i'm just going to leave a equals 10. so let's see what we have here so now we've got 1 here that's because a equals 10 um b has been equal to 1. now if i change to 20 we'll get b equals 2. there you go so b equals 2 and if i change it to 30 we'll get b equals 3. so basically switch statement is just like the file statement so in the fl statement i'll be like if a equals equal to 10 then b equals 1 and then if a equals 20 go down tab b equals 2 and then if a equals 30 go down tab b equals 3. so that's like a lot of line of code so switch statement makes it quite simple which is like three lines of code you can pull this off so that's basically the switch statement it's very nice for you guys to code uh really easily so remember these these things here this operator here that's also an additional thing um that's being taught here and it's only used for switch statements and also for the uh user defined functions now since we have dealt with this operator let's create our first function so basically what a function does is basically does a function like it does a job so we need to give out instructions for the function to do certain duties so let's create a function which inputs two variables so basically all you do is this and then you create that operators that we did for the switch there we go tab and let's say sum equals x plus y divided by 2. so in a function the the last line is basically what gets returned to the function so whatever is the last line that will be the operation of this function so how do we call this function so let's say k equals um f of a comma c and let's plot k so what happens here is a and c gets sent to this function f of x comma y and the sum what it does is that it adds this a and c which we send there and divides by 2 and this function returns the value of the sum so then k gets stored to the sum and when we plot k we get the k graph there so here it will be 30 plus 2 divided by 2 because a is 30 and c is 2. now let's save this analysis chart and see what i get so as you can see we'll get 16 because 32 divided by 2 is 16. now this is just using a and c we can change it to anything you can do it like open comma close so we'll basically get a graph of open plus close divided by 2 every single day since the beginning of s qqq so we can do something else like open of one and open f2 so this is where i'll talk about the arrays so open a one and open off close of one is like yesterday's open and yesterday's close so open f2 will be two days back the open of two days back and open up three will be open three days back so if you do that and we save it we'll basically get another graph which calculates the sum divided by 2. so basically that's what a function does you can actually do many things the function so before remember i told you like it only executes the last line is what's being returned by the function so let's say sum of two is equal to sum divided by two now we'll get a completely different chart because the one that's returned is basically the last line is what's been assigned to the function and that's what's assigned to k and that's what we plot so this is pretty much the usage of function in version five it's almost similar to what i've seen in version 4 as well but it's really good to use functions in your code you can simplify the codes a lot so when you look at most of the indicators they're all technically functions when we call them so let's now discuss the indicators and as i said before motion indicators are basically functions if this is built-in function this is a user-defined function we can and we are calling this user-defined function here similarly we can call the built-in function uh just like that so the difference between the built-in function in version four as compared to version five which we're using something called ta which is a technical because of the technical indicator so ta.sma which is simple moving average and close comma 200. so basically what this does is that it calculates the 200-day moving average and stores it decay and when we plot k we will basically get the 200-day moving average here so this is a very simple example of a built-in function so you can see the the chart is separate to the chart of the this one here so let me hide all these things the 200-day moving average in the 50-day moving average to make it clear and i want this line this 200-day moving average to be there on the chart so for that all i need to do is overlay overlay equals true and then let me save this so we still have it here i'm going to close this here and now let me add it to the chart and there you go we've got the 200-day moving average plot here now let's see if it's as same as the uh indicator that i've called from using this one here so i've got the ma200 here so i'm just going to display it and you can see it's exactly above the 200-day moving average that we just created from our pine script code so i'm just going to hide it again so that's a very simple 200-day moving average we can actually do rsi as well so i'm going to remove this overlay equals true because rsi is better to have it in a completely different chart so let's do the technical indicate rsi and i'm going to do a 14 period rsi let's see what we get so there you go we've got the rsi here it's a beautiful line and that's it really i'm just going to save this let me first remove this thing because i think it's still in the same chart and let's add this chart there you go now we've got a separate chart all over so just as we created a built-in fun user-defined function we can just call that uh built-in function just by using these brackets here just like we call f of x comma y we're just calling rsi close comma 14 but remember to use t a so that's a huge change in the version four to version five so there's a huge list of um built-in functions which you can find in the fine script manual which you can use from ranges to pivot high pivot low in rsi divergence uh indicator that we created in the uh in our youtube channel we've used the pivot high and pivot low similarly we can use the highest and the lowest to get the highest value of the past n periods so let's let's just do a simple example of the highest so it's a ta dot highest close over the past 200 years so basically calculate the highest close of the past 200. well let's let's make it 50 days and let's see what we get so there we go so basically we get the highest uh high of the close highest close of the past 50 periods now if i put this over equals true you can start seeing some patterns in the chart and henceforth you can create some trading strategies so let's see here yeah so you can see the highest close you can see there's a line here and there's a line there and the market breaks up so this is a very simple example of creating a strategy based on a donchian breakout system again we have discussed this in a youtube channel you've created a beautiful donchian breakout system uh strategy as well so here's one of the challenges for you go back to that video in our youtube channel for the dungeon breakout system which worked in many stocks and just try converting it to version five basically everything is going to be the same except the ta dot will be added to it as well so there you go that's basically a difference between a built-in function and a user-defined function now remember i told you the function when you create your own function the value returned is the last line of the code now imagine if you want to get multiple return values so in that case what you could do is basically you can create like a square bracket and write in the two things that you want to get returned some and sum of two will both now be returned now let's plot both of them so let's write here k1 and k2 i'll just give it a bracket there k1 k2 bracket there equals of um since we have to pass out two value two values so it could be open comma close so open and close will be sent to the um function here and the sum of two and sum and sum of two will be calculated and we'll return both these values so now let's plot k1 and k2 i'm going to remove this overlay equals true i'm going to save this and remove this from here because something wrong somewhere so let's see where we made a mistake line four oh yeah this is a bracket here so we're just going to save this again hopefully there's no errors uh now let me add this to the chart so as you can see we've got two lines so one is the open and one is a close and they both are divided by two and we have plot both k1 and k2 so this is the other way of returning not the other way the basic way of returning two or three or whatever number of values to a function now since we're plotting two lines here i want to separate since the color is exactly the same i don't like it so i want to add colors to my line so one thing i could do is i could write color equals color dot new and then i need to find the code for the color so one of the things you could do is you could write color codes in google and html color codes here so let's say i want this one here 332b95 which is blue let's take red for a change so this color here so i'm going to copy this one and then i'm just going to go here i'm just going to type this one there we have to leave a hash as well and then comma zero then bracket now what is a zero so zero basically is the transparency of the color so i'll give a simple example i'll just save this to it and then i'll just add this to the chart so you can see it's two different colors here now k1 is the one on the top and k2 is some other color now let's change this to 70. now you'll see it's less bright or more transparent because sometimes you want to make it especially when they overlay it's in the chart um you want to make sure it's kind of transparent so it doesn't interfere with your uh reading of the chart by any means so if i put this hundred you basically will be 100 transparency so yeah you you won't see anything so we'll just leave there to 50. so basically that's the color and color dot new you can choose whatever color you want from the color codes and it's as simple as that now you can see by default the line is a simple line so you can change the style of it by putting style equals plot dot style underscore and you can use the style so i can use things like histogram instead histogram instagram so style spelling's wrong there so there you go should work now there you go so basically you've now got a histogram for the k1 so by default it's just line uh we can also do area so we'll get a completely different graph there as well and you can give the title to this and you can call this title equals experiment and i'm going to save this now where will the experiment appear so when i go to the settings you'll see here experiment written right there so these are all good ways to write a code because sometimes you will come up with strategies or indicators which got lots of lines and it's better to identify things like this so by default will be plot and by default it will be a simple line but otherwise you can change things by using histogram or area and many other styles available in the pine script library so another thing we need to discuss is the other development in the version five which is the math option so um let's take a very simple example here i'm just gonna delete this off i'm just gonna write plot b then before that what i'm going to do is i'm going to do b equals m math dot cos b so basically it causes a cosine like a sine theta cos theta kind of thing so the graph i get here will be 0.28 so back in the time of the version 4 you don't have to do this math dot option so another thing will be math of max um close comma open so basically what this does is that it takes the uh maximum value of these two variables and then it gets stored to b so let's see what we get here so let's see the result of this one so say for instance here on the 17th of november um the open is 469 and the close is 468.14 so obviously the open is higher than the close so what we should get the return is 469 so that's exactly what we got if you look at the my script side it's 469. so we've got lots of things like mad.min uh if you can go to the pinescript language operator there's like a huge list of things you could do matter random is another one but whatever it is as compared to before now it's um math dot option comes into play so that's the other development of version five i rarely used the max men and course and sign but again if you guys are interested in coding complicated strategies then feel free uh the other thing that i want to discuss before we start with our official one example of a strategy using all the new uh availabilities of the version five pine script i'm gonna discuss something called libraries now we're not gonna discuss practical examples libraries because most of the time i don't use it personally so libraries are this thing which you can get some of the codes from other people so for instance i can create this code here and this function say for example here and share it to um my my profile in tradingview and make it available for the rest of the traders for the rest of the world so they can export this function and use it for their codes so that's the advantage of having libraries now the reason why i'm not discussing this here is that in all fairness most of the people watching this they're not thinking about sharing their functions that they've created or the codes that they created they really just want to create simple strategies or back to strategies and try to implement that in trading and that's their fundamental goal to get quantitative strategies and baptise them and get money i mean theoretically that's all what we're here for so unless you are in the field of going through the libraries and sharing these libraries and exporting and importing libraries i would not recommend go through it but then obviously there is a section here on how to create a library in the pine script use manual version 5. so again it's up to you i'm not going to discuss this because just really personally a waste of time for majority of the traders out there so let's now create our first strategy so in order to do that let's first open this and then create new blank strategy and then delete off every unnecessary thing so i'm going to delete these things off as well so the overlay is going to be true so that we can identify the trades where it's going to be so before i write the strategy i'm going to explain what the strategy is going to go about so a strategy is pretty much simple it's going to be kind of like a mean reverting strategy so if the close is less than the close of 10 days lowest close about 10 days ago we're going to go long go along and when close is higher than close 10 days ago close highest of the 10 days we're going to close the position and in order to filter any kind of bad rapid movements down south like a stock market crash or something we're going to make sure that the trades entered are closed if it's trading below the 100-day moving average 100-day moving average and we also will use that as the entry filter so we are actually in an uptrend so uh i'm gonna write that and only if the market's trading above 100 day moving average so i'm just going to go through it this is close is the lowest close uh closes lower than the close lowest of the 10 days ago we go long and when the close is higher than the close highest close of 10 days ago then highest close 10 days ago then we closed the position just write the exit just to avoid some confusion and then close all positions if trading is below 100 day moving average and we only enter if the market's trading above the 100-day moving average so let's use all the new uh available features of the pine script version five um so we can really understand how it works so that's all point of the strategy so let's initiate the first by condition so i'm going to call it by c1 equals close is less than so i'm going to use that technical indicator so this is ta.lowest so it takes the lowest of the past 10 days so close of yesterday comma from yesterday and then 10 days ago so we don't want the close of the present there because then sometimes the closest to lowest it doesn't return a true value so what happens is that if the close is less than the lowest close of the past 10 days ago then this by c1 will be true or by c1 will be equal to 1. now let's create the while loop so that we can store our this condition as well only enter if the market is moving at 100 day moving average so while close is greater than the sma the moving average close combat 200 so if the close is higher than the close of the 200-day moving average then something happens so what i'm going to do is i'm going to write in by condition two so by c two equal to one uh and then once by c two equal to one then there's no point in repeating the loop so then we can actually break that loop now let's create the exit condition as well so we'll call it exit condition exit condition will be equal to close should be greater than t a dot highest close of one comma ten so it's actually a hundred day moving average not two hundred day moving average so i'll just change that to 100 day moving average so close is greater than theater at highest close of 1 comma 10 and or or it's an or condition or close is less than the 100-day moving average so that will be ta.sma close comma 100. so yeah there you go associated high is close one comma 10 and then close is less than t8 or sma close comma 100 so that those are the exit conditions so this exit condition will be true or equal to one if both or any of these conditions are met because of the or condition we have decided there so now let's use our switch condition to execute our trades because which is also an additional new uh characteristics in our trading viewpoint script so pi c1 has to be true and pi c2 has to be true then we use that operator that we discussed about a few minutes ago for the switch and the functions then strategy dot entry called long strategy dot long comma strategy dot long and then our exit condition is true then we do that one again the operator that we discussed before strategy dot close long so there you go i'm just going to save it as a new strategy version 5 strategy 5 strategy so let's see some of the errors it's good to find these errors and correct it so you guys learn where you're missing so here there's a bracket missing so line 10 so that should solve that problem now let's see what undeclared identifier by c2 so um yeah so this one is quite tricky but i'll explain it to you so you see by c2 equals one so what happens is that the next time the code goes through the next day again by c2 will be equal to one so the trade will be executed so we need to define by c2 as something else each time the strategy gets run and then only change it if this condition is true so i'm going to do is i'm just going to declare int by c2 equals some random number five it doesn't matter and only when this condition is met we're going to change it so i think that should sort out the problem so so far everything seems to be working i'm just going to add this to the chart now see the results that we get it's not the best of the result i'm going to change it to 100 equity let's see we have done everything perfect the closest below the close of 10 days ago and the close is greater than closed 100 days ago and then the exit condition is closest the highest of the close 10 days or uh close is less than close hundred days so the buy one condition is true and by two conditions through under the strategy and we exit the strategy if this condition is true strategy of course so now let's see with the other stocks i've just performed it's 53 in paypal facebook is 183 jpmorgan 26 procter gamble 623 visa 91 so be careful with the maximum drawdowns as well this is not really the most efficient strategy um the only reason why i created this strategy is for that you guys can understand how the version five uh pine script strategy back tester works so again walmart has given 442 jnj 543 hd the maximum drawdown is quite high which i'm not quite happy about apple performed pretty well amazon again for the air intc so obviously we can make massive changes in this we can change the number of days from 10 to maybe 15 or the 100 day moving average like i did before maybe accidentally you can change the 200 so let's see what happens if we make it change it to 200 the result might completely be different we might get more of a profit sorry dc 249.5 1074 in amazon but again we have sacrificed a drawdown for if we've got a bigger returns in some of these stocks but then the drawdowns has gone up as well so uh we need to really find out where we stand in our risk profile in deciding what should be our maximum drawdown we can take on these traits so let's uh go deep into the strategy tester so you guys can understand what goes on here so one of the things that should be noted is that the trading view point script unlike other platforms don't take into account the open long position so if there's an open long position here unfortunately we don't have an open long position let's find somewhere where we have an open long i think apple might have an open log or just closed recently so i can't find this okay here there you go so here there's on the 9th of november in jpmorgan chase we went long on 10th of november so if i scroll down below you'll see the market is still in a position so this will not be reflected on the net profit section so that's one of the red flags of the pine script trading i hope maybe in their next version they might adapt to it this is one area the strategy test to analyze the section where they really need to improve on but otherwise with the respect to performance summary and everything you get to know practically every details so if i go to apple here you can see the gross profit and gross loss and then the sharp ratios and the buy and hold return so buy and hold return is basically calculated when the stock started so i had a few emails long time ago saying that some of the strategies don't beat buy and hold of the specific stock and it won't because the stock was listed at um i don't know penny stocks basically so it's like 0.4 or 0.5 and somewhere in those ranges and then obviously it's gone now to um 150 so the return is like substantial return is like huge and technically you can't buy all the stocks in the world which are being listed now so you can't compare any strategy with the buy and hold return of the specific stock but on the other hand you can compare your portfolio with the buy and hold return of spy so how did spy perform well as compared to your portfolio that's what people are always looking for did you beat the market that did you beat the market is basically saying did you beat the s p 500 returns this year um and that's your target that's your goal to be the smb500 most of the time s p 500 return gives you some five to ten percent return every year on average so if you are getting more than ten percent like fifty percent on your portfolio returns every year or twenty percent of portfolios every year now that makes you a successful trader then you're looking into that warren buffett kind of style of returns um so that's what we do here we actually backtest our strategies and then place it on multiple uh stocks and then some of the stocks it performs well like for example apple performs as well some of the stock he performs just average but going through this rough individually uh gives us a general idea so training view does not have portfolio back testing either which is also again another thing the trading view might want to add in future so again the other thing to look for is here average winning trade to average losing trade which i look a lot so because this is a mean reverting strategy normally you will have an average win an average loose trade to be quite close to each other it'll be similar but if you're following a trend-following kind of uh strategy then most likely you need to have an average winning trade which is like two is to one or three stone higher than the average losing trade so this is basically the uh real difference between a mean reverting strategy and a trend following strategy i'll go deep into it in the course uh where i discuss multiple trend following strategies and multiple mean reverting strategies but this is just an example like i said before just an example for us to get used to the pine script version five here i've used the while loop and here i've used the switch statement as well so if it wasn't this my lines of code would have gone amazingly high i will have to use if file statement here and then e-file statement for each so this here itself will be like four or five lines of code and then again this one here again will be four or five lines of code so the codes are now uh more easy to write more simple now so within just you know this is just 19 lines of code and i've created a pretty good uh decent strategy so i hope you guys enjoyed this video series on version five uh pine script so i've gone through everything from how to write some basic codes from indicators all the way to strategy so feel free to ask me any questions the email or even the comment section and i will write it down but as always the pinescript library is a great tool for you guys to learn if you want to have any complicated strategies in it but at the end of the day i find it more simple strategy seems to work more efficient than anything else hope you guys enjoyed the video have a great day
Info
Channel: QuantProgram
Views: 56,445
Rating: undefined out of 5
Keywords: Trading, Finance, Pine script, Pinescript, Tradingview Pinescript, Tradingview pinescript, Pinescript tutorial
Id: G4ErIBbUZOc
Channel Id: undefined
Length: 55min 33sec (3333 seconds)
Published: Fri Nov 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.