TradingView Pine Script Tutorial 8 – Ribbons

TradingView Pine Script Tutorial 8 – Ribbons

YouTube Video

YouTube Video Transcript

[Music] hey this is David for big bits in this video we are in our eighth video now of the tradingview pine script development tutorials where we have created a triple mini moving average indicator and we padded all sorts of features in this video we’re going to add in ribbons you can see this on the screen here pretty clearly there are other lines between the ones we have done in the past so these kind of give you an idea when moving averages are kind of converging and basically help you find a knot and the price and usually all in some cases price action can follow when the ribbons can get tight it’s just another way to show you some of the features here and a little bit of the bath you know originally when I wasn’t wanted to do this video I wanted to go over loops but I found the best way to do this was just to go without it and use a little bit of math instead of looping through and just calculating the difference between these now what I was thinking of doing was just creating a ribbon based on the the difference of the price from here and here but instead what I did was I calculated a relevant period so I had five lines between ma 1 and ma 2 and what I did was I calculated how many periods would fit evenly in here if I wanted to show five lines and use the periods to run the same MA calculation now there’s a little bit of a downside to that but the ribbons aren’t a super flexible thing like the other things we’ve been doing there are some settings that we have for it let me go in here and show you this is the styling settings just a moment there’s really only one setting for the ribbons and that’s just to show or to hide the ribbons so if you have the indicator added and you don’t want to see that just turn it off now one of the other limitations and I’ll go over why this is a limitation later is that if you change either the type the source or the resolution for any of the moving averages it’ll get rid of the ribbons between it and its next Associated moving averages so if we change the MA one to an RMA you’ll notice that there is no ribbons now between the green and the yellow line like there were before that’s because it’s it’s just calculating the midpoint now if I had chosen to find the difference between those and just average out the price then it probably could have done it but it wouldn’t have made a lot of sense in my opinion I like the fact that it’s different periods and that the ribbons can actually go out of bounds of the of the other moving averages that are in here and that might not make a lot of sense if you’re not too familiar with it but it is the truth here so change that it’ll get rid of it also with the clothes because if you’re calculating and moving average for all of these different lines if you change the source for the data then if this one doesn’t match this one how’s it going to know what source to pull for the data here and I didn’t want to just assume and the same thing goes for the resolution all right so now you’ve seen kind of how this works you can see we’ve color-coded it it looks pretty nice in my opinion so let’s actually take a look at the code if you’re following tutorial series that’s probably why you’re here so let’s scroll back up to the top of the code the first thing we added was another input for showing the ribbons is gonna default to on on this particular one this is scripting tutorial 8 so you won’t find this on the scripting tutorials before that so you’ll find that new here and then let’s also go down I did want to mention there was a tiny error not really an error but more of a typo on the plot titles for the forecasting they were just labeled incorrectly I fixed that in stripping tutorial 8 so if you’re following along and you notice that earlier good catch alright so now we are on to doing our ribbons now I like to make my code very flexible and ideally like I said I would like to use loops to calculate these values because as you can see there’s a little pattern right here and I increment up one every line here for each ribbon and that’s pretty ideal for a loop because I only have the one number that changes the problem is that the oh the the plots don’t like to operate within the loop and it really doesn’t really save me much coding resources to convert these five into a loop whereas since these can’t do it it doesn’t make much of a difference in my opinion so I just let these out of a loop I think eventually we’ll get into a loop video if you’re curious about those it is in the pine script reference manual the four loops are so you can go out and find that there but you’ll notice we calculate these values okay we’re doing the same thing we did before we have to tell up the security so that we can give it the correct resolution and we’re gonna use the type source and a new function that’s called the ribbon period it’s kind of abbreviated for it’s our period and this is how we’re gonna calculate what period we’re going to use for these different ribbons and this is what does all the magic of deciding how it’s going to calculate the specific period and we pass in the MA one and ma two so we’re doing ribbons between ma one and ma two first and then this okay so these are the variables the inputs into the function here the next one is the step so this is just the rivet count essentially this is ribbon one two three four five that’s really all that’s for but you’ll see why it’s useful in a minute and then we also have the input for ribbons which is how many ribbons there are total and that’s also gonna be pretty apparent while we need that in just a moment now that takes care of calculating the MAS but how do we calculate the ribbon periods it’s really quite simple it looks pretty complicated but it is not the first thing we want to do is we want to find the difference between period one and period two okay I’m actually gonna break out my whiteboard here because it’ll make a little bit more sense when you see the math step by step it’s kind of hard to see it when you’re looking at a bunch of parenthesis and you’re trying to remember PEMDAS from high school if you’re out of high school that is if you haven’t had an algebra class then you might not even know what I’m talking about so we’re going to find the difference between period 1 & 2 so when we’re looking at M a 1 and M a – that is actually 50 and 100 and the default values so it would be 50 – 100 I hope you can read this okay yeah I think that looks right maybe I’m not sure but 50-100 I’m just gonna step through it if it doesn’t turn out right if the text is backwards in the video oh well we’re just kind of going through this out loud here so we have 50-100 that’s actually a negative 50 okay so the reason it’s negative 50 is we’re finding the difference from a smaller number to a larger number that’s why we use the absolute value this way the difference is measured as a length and it’s always a positive value so it’s actually a positive 50 here now we take that number 50 and we divide it by the number of ribbons that we have and if you’ll remember the ribbons were the last input here and that’s five we wanted five ribbons between here but the thing is if we just used and divided by five here we would actually have our fifth line end up being on the other moving average so we have to add one to it so that we have six groups and it’s gonna be hard to see but you’ll have one area two three four five six I know it’s really hard to see I’m not zoomed in enough but trust me that’s how it works that’s how the math goes here alright so now we divide that 50 by in this case 6 okay so let me pull that up and do that really quick because that might have a rounding in there yeah eight and a third it’s probably going to round down because I don’t think you can do fractional periods on here so we have 8.now all right but if the period was just eight then it wouldn’t be here you know we would have it much closer to the actual price if we were looking here if it was just eight so we actually have to add the ribbon in for this now to get them to show up between each other to get them to be the appropriate value between them we have to add the minimum value between period one in period two so in this case it is 50 the minimum is 50 so we have 58 period moving average which is this line just under the thick green line that is the 58th period moving average and it might not make perfect sense if you’re just thinking of the math for the first time but it worked out and let’s actually try this the other way around now I don’t recommend doing this but if we were to set our MA one much larger than ma two it still does the ribbon but keep in mind the ribbon for those lines are always between ma one and ma two so I recommend and based on the coloring is that when you’re using this indicator you always keep in may one period shorter than the ma two period and the ma two shorter than a May 3 I deal a they’d be all on the same resolution but if you’re using different resolutions you would want the quickest moving average first that might be hard to understand but if you’re using like a fifty week here yeah fifty week moving average you would actually want that to be that’s actually the same thing as the 200 period to error close to it well not even close to the city at 357 times yeah all right my brains getting a little fried right here but I’m not gonna go into the math anymore because I think I’m starting to lose my anyway we know how the ribbons work now and as I’ve mentioned let me change that back to 50 it works the exact same way from two to three okay now one of the things I did was I set the transparency on the lines really low or really high depending on how you look at it here the numbers kind of faded in the closer they got to the yellow and faded out the closer they got to the outside closer to the outer moving average now and well its outer in this case doesn’t always have to be this situation let’s see it was there anything else here that really stuck out stood out now the only other thing we did that I really need to point out other than the whole I don’t think even we’re able to see the colors I apologize about that let me change that okay so now you can actually see the transparency values I really do apologize about that but you can see they kind of faded in scroll down a little bit one line at a time here or try to okay that’s as far as it’s got it but you can kind of see what I mean we set the transparency less and less they’re fading out as they go on but that’s what I mean by alpha color-coded alpha a lot of times refers to the transparency okay the last thing and I keep getting sidetracked I really apologize about that but we also wanted to make sure we only show these when these conditions are true so we need our input that check box for show ribbons to be checked we need the MA one type to match MA to the MA one resolution to match the MA two resolution and MA one source to match the MA two source we do the same thing for two and three and now we’ve got five ribbons so now you might be thinking that’s great but I want more okay well that’s easy enough you’re watching the video so I’m going to tell you how you can do more it’s pretty simple make a new line here under ribbon five name it ribbon six just increase your step size here that’s all you do to calculate the value then do the same thing for the plot and just change your transparency and the plot value here on the next line – ribbon six and you just continue to do that on and on now like I said ideally this would be done in a for loop but the plots don’t seem to work very well with the for loops so I’ll just leave it at that for now and honestly I don’t think you need me more than this this is on a daily chart and they’re already fairly close together I don’t personally I don’t think there’s any more that you would need but it’s always an option there for you okay so if you liked this video please leave a like if you liked this series so far please subscribe we’re gonna continue it on I’ve already had requests for people to do convert this into some sort of strategy or also to do back testing and other features on trading view and we’ll get to that it’s just gonna take a little bit of time I have to develop all this stuff every feature we add to it takes a little bit more time and then of course if they record the videos so we’ll get there it just might not be tomorrow or the day after but we’ll continue to put out videos and we’ll get caught up and thank you I really appreciate you watching the videos and have a great one [Music]