YouTube Video
YouTube Video Transcript
[Music] hey this is David for pig bits in this video we’re going to continue on to our next step of the scripting tutorial for the tradingview pine scripts and creating our own indicators and we’re going to do a little bit more here and we’re going to improve upon our last video and when you’re going to add the ability to select the source of the data used to calculate our moving averages and we’re also going to add another type of moving average to our indicator so in the last video we had four different moving averages and now we’re gonna have five we’re gonna add in the whole moving average which I personally really like and then we’re also of course gonna be changing this source so let’s go ahead and take a look at our end product here and once we discuss what we have at the with the end results of our code then we can take a look at how we got there so in this video you’re gonna see a lot of the changes come of course from these inputs that we’re having to create in the last video I showed you how we could do different moving average types but in this video we’ve added yet another moving average I wanted to show you how the if statement works when there’s multiple lines of logic and in the last video there’s really only one line and also we’ve added the source so when you’re calculating these moving averages you have to tell it what numbers to use from the candle information in order to calculate the moving average now by default we were using the close price which is the final price of the candle and in going forward maybe you want to use something different maybe you want to calculate based on the open the high the low or these other options adding the source option gives you the flexibility to change based on what you really need and a lot of times it might not make a lot of sense to do it with moving averages to use the high or the low but it might make sense in other indicators you know but for us using the close is just fine I’m gonna leave it there for now well once we get done let’s show you what its gonna look like first of all before I move on so we have a 50 period SMA using the clothes as the source now on this green line here for ma one let’s change it to where it’s only gonna use the highs you can see there’s a bunch of wicks to the top on these candles and that should make a pretty good adjustment here compared to what we’re seeing let’s change our source to the high yeah you can see the line is reasonably higher than it was before and I think it has a lot to do with these wicks if it wasn’t for a selecting high and using clothes you can see it used these values up here as opposed to the clothes values which we’re down here so you can see the moving average is a little bit higher now most people don’t use that so it’s probably not going to be too useful for you to use compare prices and such but it’s now an option for you it’s in your arsenal and you’re gonna see how to do that so like we’ve done before as you can tell from the inputs the inputs are just storing those selections that you’re making into a variable and we’re gonna have to use that variable later when we do our calculations now the source is a specific type of input as you can see I’ve highlighted that here that is what tells the modal pop-up here that you can select these types now this is a built-in list kind of like ant we had our options list that we created in the last video so you can select different types of moving averages this is actually a built-in list or if you’re familiar with development you might consider it an enumerator enumerated list or you know a predefined array whatever you want to call it we have got these available built-in for us now I’ve shown you you can change them but we’ve set the default value too close because that’s probably going to be the one that’s used the most I think that’s pretty fair to say now now we’ve done that you can see just how it worked on the inputs you select it and it saves it to the variable that we’ve defined now we’ve done this for all of the moving averages so that each one can be defined by its own so not only can you select a different moving average for each of the three lines you’ll also be able to select a different source so if you wanted as I’ve shown you before to just use the hi on the fifty period SMA you can do that if you wanted to use the low on the 200 period SMA you could do that you can have any kind of combination you want and that’s part of the development that we’re doing is we’re trying to make this code as flexible as possible so that you can do as many things that you want without having to recode the indicator entirely and that’s a very valuable thing to learn when it comes to creating your own indicators is making them flexible so that you don’t have to go back and change a lot of things all you have to do is change a setting and you’ve changed the entire indicator for the most part there’s a lot of good things you can learn just by making your code flexible like that and that’s something even if you don’t use tradingview and you’re just a regular programmer or software engineer that’s something you can take away and use in your actual code just to make it flexible now moving on we set our sources for our different moving average periods and this does make a change with our logical if statements and that change is that we have to instead of manually passing in the clothes here we have to make sure to tell it we use the correct source so when we’re setting our ma one values we have to tell it when it’s doing the moving average calculation that it has to use the MA one source now if we told it the MA two or three then it wouldn’t give us the right the right calculation now I’m also not sure if I switched over but on the inputs you can see there is a description in the code there’s a comment that’ll take you to that link that’ll show you all about the different input options that you have that are built in and the source is just another one of those built-in options that are already there now we’re here talking about the different logic to set our moving averages in the last video we discussed the logical if statements and how you can set a variable equal to the if statement and it always spits back the last line of code now this is the whole moving average calculation and originally when I looked at it it was two lines of code and I know I mentioned earlier we were gonna look at how multiple lines of code work in here and I apologize about that but really it doesn’t matter how many lines of code you have in here as long as the last line is what you want through set this value to when you set a variable equal to an if statement now I could go in here and I could put zero in here and it’s really not gonna do anything I mean I there’s nothing for it to do you would think if you wanted it to return zero then you would just comment out this line because now that’s the last line and it would set in a 1 equal to zero but if I left it in it would execute this line but then it would execute this line and only this line would be set to the ma one it wouldn’t even think about zero when it was setting that that value we don’t need that at all so I’m just gonna go ahead and take it back out but now you can see the whole moving average is in there and let me show you the whole moving average follows very closely along with price and it’s one of my favorite indicators let’s go with the 50 period home moving average you can see it doesn’t lag behind price very much at all it’s definitely one of my favorite indicators to use and from what I’ve read just a general change in direction of the whole moving average can indicate a small trend change so when the moving average started trending up that is when you would have a good signal that the price would probably move up now it all depends on what what look-back periods and other information you use so this one might not be the best example but it is an example nonetheless of the whole moving average if you’re curious about the different moving averages please just Google there’s tons information on these things and we’re gonna add more if you could think of more that you want to see in here please let me know in the comments for the video and we will continue to add more and more so this is our calculation for the HMA it’s pretty complicated compared to the other ones because it isn’t built in the HMA uses a combination of weighted moving averages and its uses a square root as well so we’re doing some more complicated math here but the point is whatever your last line is whatever this last line spits out gets assigned to the variable assigned to the if statement itself so that really that’s all there is we just got to repeat that over and make sure you have your spacing correct on your if statement so they execute correctly and that about does it you know that’s the video we have all of our different moving averages we have all of our different sources and we can continue to add moving averages now there are ways we can improve it I mean just looking at this code it looks a little redundant I’m pretty sure in our next video we are going to be discussing how to consolidate some of this code and improve it even more let me take a look at my profile and if you haven’t seen my profile yet I encourage you to go check it out on trading view it’s in the description of the video the URL is that is and you can come here and you can look at all the different scripts I’ve already created now we’re on scripting tutorial 4 I’ve already got 6 and I’m working on a 7th already so make sure you stay tuned to the video series subscribe on YouTube and you’ll get to see all the videos for all these other things so in the next video it looks like we’re gonna be covering crossovers and I think this is the video we also reduce the code for the moving averages we don’t have to do new we don’t have to rewrite the same code over and over we’re gonna create a function if you’re familiar with development that we can reuse to calculate the moving average it was gonna be really helpful going forward and we can use the idea of a function in other areas as well you know then after that we’re gonna move into forecasting which can also show you where we expect the moving averages to go so a lot of great things coming up please subscribe if you want to see those and like this video if you like what you’ve seen so far but we’re moving on ahead and things are going really great with our scripting series if there’s any sort of indicators or anything that you want to see done specifically please leave a comment here on YouTube in the comments or on social media in general the links are all in the description I usually read all of those for now so please go on and leave a comment and I will take a look at it and if it’s really good we’ll probably end up doing it I love doing the things that people suggest those are usually the most fun thing to do and it’s rewarding for the people I do them for so looking forward to that so thank you for watching have a great day and I will definitely well hopefully see you in the next video [Music]