TradingView Pine Script Tutorial 22 – Measuring Trend Length and Value Carryover with RSI Strategy

TradingView Pine Script Tutorial 22 – Measuring Trend Length and Value Carryover with RSI Strategy

YouTube Video

YouTube Video Transcript

[Music] hey this is David for big bits and in this video we’re gonna take a look at creating an RSI based strategy to try to find the bottom on the chart and maybe even potentially find a bullish divergence on the chart using the RSI and the smooth RSI so I’ve got the smooth RSI indicator that I’ve created in another video here if you want to check that video out just go to my channel and you can go back and it’s in the tutorial playlist here and you can see how we created this one but what we’re gonna be focusing on in this video as part of the strategy is looking at how we can capture the length of a downtrend so you can see here in the strategy with the code that I already have it bought at a low point here but what we’re gonna want to do is we’re gonna make sure that we captured that the length of the smooth RSI the downtrend was at least three candles long and that way we can basically guarantee that the price has continued to move down while the RSI is low and we’re also gonna pair that with making sure that the RSI and the smooth the RSI are below a certain number as well that way we can pretty much guarantee that that downward trend always occurs at a low price because the RSI and the smooth RSI are both going to be considered over sold so we’re gonna go ahead and take a look at that we’re also going to look at carrying over data from previous candles that way if you want to keep track of like the lowest price in a series and you want that line plotted over and over and over until it gets broken again and you have new lows then what we can do is we can carry that data over from previous candles and continue to plot those and I’ll show you how to do that in the video as well although we’re not gonna actually add that to the strategy I will show you how to do it so to get started we have the smooth RSI indicator here and I haven’t set up a little bit different than the defaults that we use because I found the whole moving average to where a little bit better and a shorter whole moving average that’s because the RSI moves pretty quickly and the whole moving average usually gives you pretty smooth curves and with a shorter value like that it’s going to have those nice smooth curves much quicker so that’s why I went with the whole moving average and that one’s actually baked into the script it means this isn’t an option for you in the script that I’m showing you but it is actually hard-coded in there by hard-coded I mean you can’t change the calculation through any setting that’s basically all that means is that the code is put in there there’s no way to manipulate how that is calculated with any sort of setting okay so the first thing we’re gonna do I want to show you some of the things that are already part of the strategy and we’re gonna take a look at strategy tester goddammit hey this is David for big bits and in this video we’re gonna take a look at an RSI based strategy but we’re gonna really focus on capturing the length of down trends so we’re gonna look at the smooth RSI here on this orange line and what we’re gonna do is we’re gonna keep track of how long a downtrend has been occurring on the smooth line which is the orange line on the chart and the reason we’re gonna do that is because we want to buy on our strategy when we have had a downtrend of at least let’s say three candles while the RSI and the smooth RSI are both below a certain level that way we can kind of try to find a bottom in the price because usually if the price has been going down and both lines are oversold it usually means that you’re probably due for a bounce and that that might have been a bullish divergence on the RSI although in this particular case it is not a divergence but there is a chance it could catch those as you are having a pretty much second downtrend oftentimes but we’ll go into that a little bit later in another video for now we’re just focusing on carrying over data from previous candles and I’ll show you what I mean by that but also capturing the length of the downtrend and we also do that by carrying over the data I’m going to show you another way to work that as well that isn’t really related to the strategy but say for example you want to plot the lowest lows or some sort of level on the charts and you want to carry that over from candle to candle and you can actually combine that with the trend length to say you know once we’ve had this trend going for so many candles move our lowest low up something like that you can do that by capturing the trend length and also combining that with carrying over the value of the lowest low from candle to candle so we’ll take a look at that in a little bit but for now I think I’m going to remove my face from the video and we’re gonna go ahead and take a look at this particular strategy a little bit more in-depth and before I do that first of all I should mention that the smooth RSI is not using as a default setting so if you add this indicator which is available in the trading view Public Library it is different than the default settings and the reason I’ve done that is because I needed a quicker smoothing period and because we’re working with 5-minute candles and we’re working with a smoother trend with the HMA so I wanted shorter periods so that we can capture the downtrend quicker it turns out when you use longer periods on the smooth RSI with this particular strategy that it usually kind of catches things either too early or too late and I found out that the whole moving average with an eighth period works out the best so now that’s over with let’s go ahead and take a look at some of the actual code we’ve got here there’s a few things that we’ve already covered in the series so far that are already built in we have our take profits these are inputs for the strategy you know I’ll just kind of go over all the inputs right now since they’re gonna be shown next to the code here the first one we have is to take profit percentage and a stop loss percentage then we have an RSI look-back period that’s how far back how many candles we go back to calculate the RSI and we also want to require that the current RSI is below 20 based on the default settings so the blue line here is below 20 so that condition is met then the smooth RSI we’re gonna use an eighth period hole moving average the reason I have the smooth moving average indicator shown here is so that we can kind of see what’s going on without actually having to plot anything on the screen now the last input that we have is for a minimum smooth RSI downtrend length and this is what we’re gonna be focusing on in the video is capturing how long the downtrend has been for the smooth duress eye so that we can kind of carry this information over from candle to candle now we have this stored in here and we actually need to find the length of the trend down we’re actually gonna be doing that so let me hold that out you can see we’ve calculated the RSI using the close I don’t have an option for you to change that in here if you want that please let me know I don’t think you really need to do that but we also have the whole moving average kind of hard-coded in here so that it will use the RSI is the source and also the smooth RSI link and the way it’s worded in the variables here is the SRS a curve then of course we also calculate our profit target which is based on the close price times the take profit percentage divided by 100 so we also got that to work with and then we also do something that’s a little bit new I don’t know if I did this before but we are creating a boolean variable and we’re going to be putting multiple conditions in there to tell us whether or not it should qualify as a buy signal or a sell signal so to buy right now we currently have the RSI below the input that we had for the RSI currently below and also to be true must be the smooth RSI also must be below its s RSI currently below input so can I have an idea of what’s going on here now I know we also set the profit target and the loss target but what what can happen a lot of times is the price can kind of just go sideways and you want to get out of the trade so if you flip your equity more and more on high-percentage winning trade you don’t want to be stuck you don’t want your equity stuck in a trade for a very long time perhaps so you want to get out of it eventually so what we’re doing here is we’re just going to sell whenever the RSI goes back above sixty which might be another way to capture profit I believe in most cases that would be unless there’s kind of like a slow bleed in the price and then it just kind of bounces up just a little bit but for the most part having the RSI above sixty and setting that to our sell signal to pair along with the profit and loss target is pretty good idea to try and capture some profits basically now now that we’ve done all of this now we get to actually focus on what I’ve been wanting to get to I’ve described all the code here so you have an idea of what’s actually going on below where we have calculated the RSI and the smooth RSI we’re gonna be putting in some new code we’re gonna start with a new variable and that it’s gonna be our smooth RSI trend down length and this is going to be storing the length of our trend now you can see I’m setting it to zero now and that’s because we’re just kind of using that as a default and we’re actually gonna check what the previous value was now and not only that’s we are going to actually set the trend down length and increment it up one if it was already above one so essentially what’s going on here actually we’re not actually carrying over the value here my apologies what we’re gonna be doing is we’re just gonna check if the trend is down and we’re gonna add one to the previous value so since we set it to zero by default if the trend is down now and we add one to it it’s always gonna go up one but if the trend changes and now it’s up it’s just gonna stay at zero and I’ll show you how we do that just now I apologize about the confusion there but I’ll show you how we carryover values again in just a moment so this was pretty simple so we’re just gonna check see if our current smooth RSI is below the smooth RSI of the previous candle and remember we’re putting one in here because these arrays are zero based indexes and we’re going to look back one candle so if that one is true it started a new line hit tab and make sure you do that if you’re typing the code in yourself because if you don’t have the spaces correctly done it won’t work and then we’re going to check we’re actually we’re just going to set the s RSI trend down length equal to the s RSI trend down length of the last candle so if the last candle were one or two its value was one or two then we would have carried that value over here but what we’re actually going to be looking for is to increment this value up so we’re actually adding one to it every time so it does carry the value over but it actually adds one to it so just to be clear this can increment your value up and it also carries your value over but I’ll show you how to just carry the value over next in in a few minutes here so we’ll continue on we’ve got our downtrend length and we’ve already got the ability to have our input our variable for our minimum downtrend length so before we actually look at how this how applying this to our conditions is gonna work let’s take a look at the strategy right now and it actually can’t compile so it looks like I have a typo somewhere so what is this Oh yep so I forgot to add the : here and if you’re gonna be setting the value of air ever variable later you have to do the : and then the equal son so yeah that was all there was to it so now we can see our strategy it’s actually pretty profitable even with as simple as it is but we want to try and improve on it then use these other things that we’re learning throughout the series so let’s take a look at her performance summary it would have gained eight point seven eight percent and I actually have the fees added in here this would be if you were on by dance the strategy is that maybe I took the fees out my apologies I thought I had those in there nope there they are they just weren’t loading a second ago so the Commission value is 0.1 and that is the same as by Nantz so if you’re trading on – is the same and if you wanted to account for slippage you can do that as well so back to the strategy tester you can see it was pretty profitable and out of all the trades there wasn’t a whole lot going back through January 21st on the five-minute chart but they’re pretty accurate as far as trying to predict a chance to get profit because all but one of them were profitable you can see we had a total of twelve trades so that means eleven out of twelve we’re profitable which is actually really good and the max drawdown was only one point seven nine percent and you made eight point seven eight percent profit using this particular strategy just since let’s see that’s about a month and a half ago now so in a month and a half you could have made eight point seven eight percent using just this strategy against Bitcoin if you had followed it during that time now of course that doesn’t mean that it’s gonna continue to do that and don’t take any of this as financial advice of course I always have to say that no no this is actually financial advice even if you make a strategy on trading view and it looks great that doesn’t mean that it’s actually gonna translate once you actually go to trade against it because the markets change and how the price action works changes but we’re just trying to identify what would work best so back to our pond editor actually I want to take another look at the performance summary because we’re gonna be comparing mostly our net profit which is eight point seven eight percent and now what we’re gonna do is we’re gonna have to account for that smooth s RSI downtrend so we’ve set that here so let me copy this variable length and let’s go back to our is a bye and we’ll add one more condition to the is avaya signal and we’re gonna make sure that our s RSI trend down link is greater than or equal to since we said it was a minimum the minimum s RSI downtrend input that we used let’s go back here add that in there and that’s all there is we we captured the length of the downtrend simply by incrementing the value up by one every candle then we had used our input and we compared our trend down length to the input based on what we would want and that’s all there is to it so let’s save this and hopefully I don’t have any syntax errors okay perfect so this trade still would have occurred because it was going down for at least three let’s go back to our strategy tester now our net profit actually went down that’s because it took a trade out that it wouldn’t have otherwise hit so you do need to play around with these to try and find the optimal settings but for the most part we did one less trade because there’s only eleven now that there still is the one that was not profitable but you have to be willing to accept the risk when you’re looking at these sorts of things to trade one that’s very profitable a lot of times when I was playing around with the settings on this particular strategy what would happen is you would have a strategy that was profitable 100% of the time but it only traded once a month so that wasn’t really great because it would only get the profit of that one trade and you want your profits to compound if you’re trying to make money so you take that one or ten percent profit as part of and there’s my bat-knife notification but you would take the profits that you made off of this trade and include that in the amount that you put in the next trade so you made more and more every single time you make profit so you want to flip your equity more to compound it more often and that of course it’s not financial advice again but if you want to see more profit on your performance summary you usually do want to have more trades because the higher win rate that you have with profit and the more trades means you’re gonna net more profit it’s pretty simple math that when you think about it but let’s take a look back at our code I promised you I was gonna show you how we did something here so let’s see let’s look at the lower lows here and I’m gonna show you how to plot this on the chart just because [Music] this is probably how you would use it if you were creating an indicator so I’m gonna create one called lowest low set this equal to zero all right oops that’s a habit from c-sharp we have our lowest low set to zero and let’s actually set this actually I want to do something different I don’t want to do the lowest low I want to do trend change trend change down so we’re gonna plot now the price whenever the trend changes down and we’re gonna be able to tell that the trend just changed down because it’s gonna just equal one because it incremented up one from zero this is a little bit different than what I was going to show you but it’s the same same principle here so we have our trend changed down equal to zero then similar to how we did that before we’re also going to check to see and we can actually do this in one line by the way instead of having this if statement here we could have done this in one line before but it just it’s a little bit more clear that way so we’re going to do this in one line so if the trend changed down we’re gonna do this by checking and we are going to check the s RSI trend down length so if that is equal to one we’ll know that it just started its trend down change so and if you’re not following along with the syntax here and you wonder why I’m putting in question mark here please go back to the previous videos basically we’re checking to see if this condition is true and if it is we’re going to return a certain value and if it’s not we’re gonna turn another one so let’s see the trend changed down so we are going to set that equal to the closed otherwise if it did not change down we are going to set it to the previous value of trend changed down excellence and we have to tell it was the previous candle now I actually want to go back and set a default value here just to make sure that we don’t run into any errors with it trying to access the value from the previous candle so it’s pretty much just one line of code but we’re initializing our variable here and now let’s actually plot that trend change down and that should take care of our video hopefully if everything works out like I expect this is live coding so we’ll get to see how that works out integer tide ah okay so we have to do this occasionally where we have to tell the the script what type of series we’re using or what type of variable we’re using and this particular variable by default is just going to assume it’s an integer but since we’re gonna assign it a value of close which is a float you can see on the screen when I’ve hovered over now the type is float we have to make sure that we declare our variable as a float and it should take care of it now okay after holding my breath it worked you can see now whenever the trend changes down it plots a new price so the trend changed down here and you can tell when we look at the chart this is gonna be on these little red arrows on the smooth RSI because that’s what those mean on that particular indicator you can see it plotted a new price trend change down here plotted new price new price new price new price so you can kind of see how you can use this to carryover values but yeah I hope this has been useful for you if it has please like the video and also subscribe especially if you like these this video you’ll probably like a lot of my other content because we cover a lot of pine script tutorials here and if you haven’t seen those please go back and watch those but we cover this and we also discuss trading indicators and also crypto related things as well so please subscribe if you haven’t already check out my trading view profile where you can see a lot of the stuff we’ve already created in this series which probably be really helpful if you haven’t already seen the other videos you can take a look at code we’ve already done that’s open source this particular indicator probably not gonna be open source at least not for now and let me add myself back to the video and now that I can’t add myself back to the video these this indicator or the strategy that we’re working on is probably not gonna be open source for now but if you’re following along as I’m doing it you can probably copy the code it’s not a big deal and when we’re done with the indicator then or when we’re done with the strategy I should say then I’ll probably make it open source since I mean the codes already open source in the video and you copy it from that anyway I just want to make sure it’s a final product when I actually publishing it in case anyone wants to actually use it so thank you for watching and and for those of you who have been watching please check out the description of the video there is a link to my referral for trading view if you don’t already have a paid account on there and you want one I would appreciate if you use the the referral link because it does help me keep my subscription up on their site and the premium subscription is pretty expensive so I would appreciate if you are going to do that you might as well use the referral link I would really appreciate that but other than that thank you all have a nice day and see you in the next one [Music]

YouTube Video Description

< br/> #bitcoin #crypto #cryptocurrencies #tradingview

Website: https://bigbits.io
Discord: https://discord.gg/rapMn4z
Twitter: https://twitter.com/BigBitsIO
Facebook: https://www.facebook.com/BigBitsIO/
GitHub: https://github.com/BigBitsIO
TradingView: https://www.tradingview.com/u/BigBitsIO

Referrals……
Trade on Coinbase: https://www.coinbase.com/join/johnso_dxz
TradingView: https://www.tradingview.com/gopro/?share_your_love=BigBitsIO
Brave Browser: https://brave.com/big406

DISCLAIMER: All my videos are for educational and entertainment purposes only. Nothing in this or any of my videos should be interpreted as financial advice or a recommendation to buy or sell any sort of security or investment including all types of crypto coins and tokens. Consult with a professional financial advisor before making any financial decisions. Investing in general and particularly with crypto trading especially is risky and has the potential for one to lose most or all of the initial investment. In simple terms, you are responsible for your actions when trading.