TradingView Pine Script Tutorial 18 – For Loops & Adding Bonus Points

TradingView Pine Script Tutorial 18 – For Loops & Adding Bonus Points

YouTube Video

YouTube Video Transcript

[Music] hey this is David for big bits and this is going to be our third video in the scoring development and playing on this to be the final video in the scoring development in a miniseries within our big series here and what we’re going to be doing is we’re going to be adding loops and we are also going to be adding some bonus points to our chart based on its current values now the reason we’re using loops and I’ve mentioned this in the past videos if you haven’t seen those please go back and at least watch the last two videos so that you’re aware of everything that’s going on I know they’re a little bit longer than my normal videos but this is a pretty big topic overall to cover and there’s a lot of big things going on inside of it that are important to understand especially loops here so what we’re trying to do with these loops is we’re trying to loop back through the previous Candles and try and catch this trend where the price is going lower and award it in more points as it goes on that way we don’t get the best signal back here we get it closer to the bottom now in order to do that what we’re going to do is we’re going to have to create a new value here make a section for our indicator bonuses and let’s make one for the RSI and I made this a float value because I got some errors when I tried to do this before and it needed a data type associated to it so in order to do a loop in pond scripts it’s similar to a c-sharp loop if if you’ve done those but we’re going to be using their standard format for the for loop and it’s like this so if you wanted to loop from one to ten you would make your loop like this you would want to have four spaces on the next line code goes here and that would take care of your for loop now if you wanted to break out of your four loop at any point you would just use the break keyword and I used a semicolon because I’m used to that with c-sharp now for us to be able to go back and loop back or look at the candles in the past then what we need to do is we actually need to use a negative number so if we start at one it’s not gonna work so I’m gonna show you how this works and we’re gonna add points to it and hopefully it all makes sense at the end now what I use was I wanted to go back seven candles so let’s pretend we’re looking back here at this huge dip and we’re on this particular candle and we want to look back seven candles and around here and we want to see where our RS I was at that particular time now to do that of course we need to go back seven candles and the reason I wanted to start with the negative seven is because I want the highest or the furthest candle back first now if I wanted the first candle first and that’s easy I just start with one and we’ll go backwards but what I want is I want to start with the farthest candle back first and then get closer now I’ll set that to native one and we’ll go up by one now that’s going to do is that’s going to set us seven candles behind and each iteration through the loop it’s going to go up that one so it’s going to go one candle to negative six negative five four three two one until we get to the current candle so how are we going to give it bonus points well the first thing we need to do is we’re not going to give it bonus points unless it’s RSI is less than 35 and because that’s where we did with the actual points up here we had to make sure that the RSI was less than 35 why would we give it bonus points if it wasn’t already less than 35 as well we want to see that the RSI is still below those numbers so let’s get back to our RSI so in this particular instance let me make this thing there’s the whole screen here so once it dips below 35 we want to continue adding more and more points the further below 35 it is so not only do you get the points for how far below 35 it is you’re also going to start getting more and more points add it on the lower it is and these are going to be cumulative so the farther back it stays underneath of that the more points you continue to get so we’re under 35 now how do we check well that’s for the current RSI and I apologize I should have done this when I started typing in a minute ago we want to get that candle back but if we just passed an eye it’s gonna be negative seven is a zero based index that only goes into positive integers so that’s not gonna work we have to call the absolute value of I so if convert that negative seven on the first iteration to an actual seven it’ll go back those seven candles to the eighth candle back now this is how we look back in the past we’re looking now at the seventh candle back making sure it’s below the 35 value RSI and to make sure that this is still valid so to make sure we didn’t have a huge jump in the RSI and the price jumped along with it we also want to make sure that the price continued to go down so what we’re going to check is we’re going to check to make sure that the current close is actually less than the closed eye periods back so we’re not only are we checking that the RSI back here if it was below 35 but we’re also checking that the closing the price was below that as well so it just kind of confirms that we’re giving this bonus points because the price continued to drop even though we were correct in noting that it should have been worth points below 35 so let’s go ahead and continue here we’re working with our if statement we need to give it another four spaces I believe that was correct and now we are going to use our RSI bonus value and we are going to set it equal to now to reference the value outside of the if statement we have to use this colon equals and you would have known that if you would watch the other videos so far it’s way back in one of the earlier videos where I covered the if statements now I believe also covered functions with those as well but for this we’re going to set our RSI bonus now we also need to include its bonus already so that it’s cumulative and then we’re also going to add on top of that the the value of the current candle back now I on the first iteration is negative seven so with the absolute value that will make it seven so if it’s true on the very first candle but current price is below that candles price which it appears to be and the RSI is below 35 which it appears to be it’ll give us seven points now in the next candle if one of those conditions weren’t true it just wouldn’t had any points but we would still have seven bonus points for the current candle and then it continues on and as a price continued to drop those points continue to accumulate as a bonus for the current candle so it means your points are gonna continue to go up now they may fluctuate a bit but for the most part they’re gonna continue to go up now there’s ways we can take care of averaging that out but for now we’re still just doing an example so hopefully this will make more sense how it is now we don’t need this brake line here and with our total points we also need to add in now our RS I’d bonus now I’ve already gotten a code for the stochastic it’s the exact same code just for the stochastic excuse me just for the stochastic and I’m gonna paste that in here but I need to fix the capitalization because I was kind of lazy earlier and I did not do it correctly give me just a moment this would be a lot quicker than if we did this with live candy with live coding excuse me or yeah if we had coded this all again [Music] we would have had to have done this anyway but we’re saving a little bit of time but not having to type all the conditions and stuff got that now let’s add those stochastic bonus points and on our total points as well save it and you’ll notice there’ll be a lot more points here if I did it correctly ah I also wanted to use this stochastic D and of course I did not capitalize all of those my apologies thank you for holding on and waiting right now it’s hard to keep up with all of these different variables between everything that I’m working on okay I believe that hazards and actually what I wanted to do was I wanted to use the stochastic D now the reason I you wanted to use the stochastic D it’s because it’s a little bit slower and it’ll stay at a lower level longer and if we’re trying to catch that drop even if it continues to go up on the K the D you’ll still be lower so that’s one of the reasons I wanted that one now let me see of course I referenced it wrong still here the joys of live coding okay that took care of that so you see our value is actually a lot higher here now but how do we make that even higher because it looks like it’s still even lower point values than that and it’s hardly any higher than Nats and there was a dip in between here but honestly it wasn’t a terrible time to buy any time it got below or it got above a certain point here they seem like decent times – by going back you can check they’re actually pretty decent of course that was during a bear market but it did correctly kind of called an uptrend they’re small in their said very good one here that one probably wasn’t high enough those probably weren’t high enough but you get the idea it’s kind of helping you here by combining all of these different points together based on your confidence value the lower those values are the more points they’re worth and looking back and accumulating all those previous points it starts making a difference and you can see these points are adding up to a value that’s gonna be helpful and you can imagine a scenario where you have different indicators that you’re more familiar with that you know how the points should be weighted and how you can come up with a scoring system to work for you and that’s what this little mini series is all about here with scoring developments kind of help you with your own point system if you want to do something like that now another idea that I came up with when I was doing this was to use these points that we’ve come up with similarly to how we used our stochastic in our RSI bonus to reward the candles that have higher points cumulative ly like we did with the price drop so if the price continues to drop and we had high values before why should we continue to not add points to them the farther you go along the more valuable they should be so just based on our points alone we’re gonna add more points on top of it now to do that we’re actually gonna have to come up with a new total here and let’s just go ahead and copy this code yeah let’s copy that code from the stochastic and we’re gonna call this self bonus go back and change some of the wording here and thank you for watching this video I know it’s going on for a while but hopefully you are learning quite a bit and we have to reference our total points instead of any specific indicator our indicator is our total points that we’re using and we also need to change this from a P to something else because it’s already being used in that particular loop and we also have to change the references to it excellent there we go and when you change these as well to the self bonus excellent that’s got that now we need to plot another total and that is going to be our total points plus our self bonus and let’s color this one blue all right so let’s try and save this hopefully it works ah it worked I don’t believe I did this correctly No okay I know exactly what I did wrong here but you see it does apply bonus points but it’s only doing it to the ones below 35 I actually meant to give it bonus points for the ones above 50 50 yeah 55 or 50 so I actually need to change the condition here then make that only give bonus points based on previous candles that were above 50 ah and now you can see even though there was that kind of jitter there it continued to go up as the price continued to go down now you know sort of huge dip between those that’s gonna be kind of hard to avoid because the price did bounce but in this particular case maybe you can zoom out on the chart to say a three-day candle and it makes a little bit more sense so thank you for watching please leave a like if you liked the video and subscribe for other videos similar to this going forward we have a lot of different things to cover I’ve got a lot of ideas for pine script but there’s also going to be other video series on smart contracts I’m going to start sharing some practical examples on those soon hopefully if I haven’t already by the time this video comes out and if you want to get the indicator that we’ve been working on here I am going to end up adding it to my trading view profile that’s big bits IO on trading view the link should be in the description for the video now you see all the other scripts that I’ve done and take a look at those and if you’re curious about any of those I’ve pretty much made a video for all of them and you can go back and look at all those videos but thank you once again every one and a half a nice day [Music]