TradingView Pine Script Tutorial 3 – Customized Options

TradingView Pine Script Tutorial 3 – Customized Options

YouTube Video

YouTube Video Transcript

[Music] hey this is Staters are big bits in this video we’re gonna take a look at our next iteration of the scripting tutorial and that is scripting tutorial three and we are going to be discussing triple many moving averages and and it sounds kind of confusing but essentially all we’re doing is we’re having three moving averages plotted on the chart like we did before but instead of having the moving averages fixed as being either a simple moving average or an exponential moving average now we’re gonna have the ability to change which type of moving average we use for each of these lines so I’ll go ahead and show you the end product here and you can see of course that the realigns but the big change is in our inputs on our settings is for each moving average so for moving average one we can change it to have a different type of moving average let’s have it be a exponential moving average and you can see that it changed and since an exponential moving average usually follows price a little bit more closely than a simple moving average you can see it’s a little bit closer to the actual price let’s go ahead and change it back for the video and we can go ahead and discuss kind of what we’ve done now the the first thing you’ve noticed obviously were the inputs how did we have it set up to where you could select whether you’re going to use an SMA EMA RMA or WMA now if you watch the last videos you know that the inputs are handled by storing a variable with the result of the input now the inputs that we’re using are defined by an options array or a dictionary or whatever you want to call it we’re using options now let’s go to the input documentation you can see the options parameter here this is useful to provide users with a list of constant values they can choose from when using a drop-down so for example in this documentation the options you would have in the drop-down or a or B and it shows you how to use those values here so you can see that choice was assigned to the value and the options list that you defined it’s pretty simple so in our case we have four different moving averages we’re working with right now we have the RMA and the SMA EMA and the WMA now we had to store a moving average type for every moving average that we have in a list so we have three moving averages so many three types to be stored one for each moving average so this is the moving average type for inmate one two and three and so forth if you wanted to add more for whatever reason now that alone doesn’t make it to where it will actually change what’s plotted here we actually have to tell it to calculate the correct moving average to display the correct data so for example if all we did was just change and add this input if we changed it from SMA to EMA it would still show the SMA lines and there wouldn’t be any difference in order to make it to where you do change it and it will recalculate those values you have to actually do the logic to where it’ll look and see which moving average is supposed to use and use the appropriate moving average calculation now I’m going to expand our code here so maybe it’s a little bit easier to see if we’re gonna start learning how to use logical conditions and the most simplest logical condition you can use in just about any programming or scripting language is a simple if statement basically you’re checking to see if a certain condition is true then you will do a certain action so for example if our ma one type is equal to the SMA option that we defined then we’re going to run the SMA calculation based on the clothes which is the current price and the amount of look-back period that we want her to use so for ma one the default was 50 our MA one type was SMA so it would check our MA one type was SMA yes so it’s now going to execute the SMA calculation using the clothes price for all of the candles that it’s using and it’s going to look back 50 periods to calculate the SMA now there’s something in here you may not have seen or be familiar with with other languages and this is in my opinion fairly unique to pine Script I haven’t delved into tons of different programming languages but for the ones I’m familiar with this is kind of different so in pine script you can set your variable value equal to the if statement now what’s going to happen is your if statement is going to return a value and it’s always the last function or expression that you use is returned back so in this case setting the MA one value which is where our green line is is dependent on what the last function is that we call here so when we calculate the SMA here it sets the value of ma one now if you’re not too familiar with coding you’ll notice this looks really similar you know are we setting in may 1 equal to if no we’re setting it equal to whatever the condition is that gets executed last in this if statement this nested if statement I guess you would call it but you’ll also notice there’s just one equal sign here and two equal signs here now that’s because here we are comparing in a one type to SMA you use two equal signs and pine Script to do comparisons to see if the condition is true or false so if we did less than the condition let’s simply be at and then it’ll tell you whether or not the MA one is they may one type is less than that but that doesn’t make any sense we’re comparing strings there but just to give you an example we’re comparing the type to the string here and if they’re equal then it’s going to execute this and it did and the single equal symbol that just means we’re setting the value so whatever is on the left or setting that value to whatever is on the right and since it’s an if statement it’s whatever expression gets calculated I know I probably rambled on a bit about that just trying to explain it for those of you who were interested a bit more but essentially it’s doing what we want we’re setting the ma value of ma one to the calculated value based on whatever moving average that we have selected now we continue to do this for our other moving averages as well ma two and we make sure to use the correct MA type we got to make sure that we replace ma one what they made to here to make sure it’s comparing the correct moving average type and no longer still using ma one and also with the periods as well and then you continue to do this for your other ones and now you can plot your moving averages that we set with our logical if statements so there actually was quite a bit of work here if you’re new to programming or scripting to understand what’s going on now to keep in mind with pond script you have to get the spacing and the indentations on each of these lines after the if statement absolutely correct or they won’t execute correctly that’s kind of what I’ve learned so when you run an if statement the next line I believe has to be four spaces in or so but just using the tab just using the tab key will take care of that and it will give you the correct spacing if your next line isn’t at the correct spacing beneath the if statement it won’t execute so you’ll notice every if statement line that we want to execute as part of that logic is always indented by for now when you want to end your if statement and do something else the else statement needs to be directly below the if or at the beginning of the line so you can see here it’s not directly below the if but it’s back at the beginning of the line and these need to be done correctly from what I have learned that may not actually be the case but I do know it works perfectly well just like this so do keep that in mind all right so that pretty much takes care of this indicator if anyone has any questions please let me know if you like this video please leave a like on the video and subscribe because we are doing several more I’ll go ahead and I will show you well first of all I’ll show you the the if statement here the documentation is in the code you can get more information about how it works here but let’s take a look at my profile I want to show you the scripts that we’ve done and we are going to be covering in the videos let’s go back here let’s get the scripts you can see we’ve gotten six and I’ve already started working on a seventh at this point and we’re gonna continue doing more and more as time goes on so if you’re curious please check out my profile on trading view it’s going to be in the description of the video make sure you subscribe to YouTube here so that you can get updates on this series if you’re wanting to learn more about calculating your own indicators you can always take what you learn from this series and apply it to other ones as well so I definitely appreciate everyone for watching thank you and see you in the next video [Music]