hey what's up traders welcome back to another lesson in this lesson [Music] hey what's up traders welcome back to another lesson sorry this one is a little bit later than usual i was on holiday last week with my girlfriend we actually went up to noosa where the great nick raj lives got to eat at some of his recommended restaurants that he recommended on twitter recently and it was fantastic anyway we're back to work now and in this lesson here this is from the mastery course so i'm answering a question here that was asked by a student in the course and the reason i'm posting this on blog is because while it's a bit of an edge case and not everyone will want to do the sort of thing that the student wants to do in in his particular script working with the ichimoku cloud in a particular way which i'll explain in a moment i think that the techniques in this lesson can be useful especially to traders who are relatively new to panscript and still trying to wrap their head around things like tracking values after a certain condition is met detecting price patterns around certain conditions that sort of thing i think this will be a valuable lesson for you guys the next lesson i'm working on i'm hopefully going to revisit pine connector because there's been a lot of changes with the pine connector expert advisor so i do want to go back into automation and automating our scripts through plan script i want to revisit that a new and improved series on that subject but for now i hope you find this lesson interesting if you have any suggestions as always leave them in the comments section i read all the comments i can't always respond to all of them but i do check on them some are really kind some are not so kind and some are very helpful in guiding the subjects that i cover in these lessons so thank you for that thank you for your support enjoy the lesson [Music] question comes from marty i believe is how you pronounce your name sorry if i butchered that um he asks a question about the ichimoku cloud he says here as i know one of the features of using ichimoku cloud is finding potential support and resistance in the image he sent me if i drag this over can i do that yes this is the image he's talking about this is the baseline of the ichimoku cloud and his plan is to write a script in which it shows the baseline only when it is horizontal and he also wants this horizontal line to be saved into a variable that he can reference to tell whether price is above or below that horizontal line so this is actually quite a easy thing to achieve the first thing i'm going to do is throw on the ichimoku cloud inbuilt indicator from the trading view team i'm going to open this up copy and paste all this code out into a new blank script and i'll just call this delete for now since i'm not going to keep this script but you can name this whatever you want obviously and if i get rid of these two indicators and throw this on we now have the inbuilt ichimoku indicator on our charts um a couple of things i'm going to do here is i'm going to disable all of the displays here i'm not going to bother formatting this code to look nicer like i normally do just to save time because it's not really relevant hopefully you guys by now have your own style of formatting your scripts and you you know how to do that so for this sort of lesson or demonstration i'm going to turn off all of the default plots since we don't want them in this particular case if i save that code all of those lines should go away there we go we still have the background color there i'll leave that on for now just so that we know what the ichimoku cloud is doing and if you come into the settings menu and go to style you can turn all these back on if you want but they're now off by default so in order to track the baseline let me turn that one on for a moment the bass line is this uh red line you see here on my chart we want to track this line only when it's moving sideways so when it's doing this this oops sorry my phone is ringing i need to answer this one second all right i'm back sorry about that uh now i believe i was talking about tracking this line only when it's moving horizontally so when it's doing this we don't want to draw it we only want to draw it when it's going sideways and we want to track that price value as well so this is pretty easy to do all i'm going to do here is create a var persistent variable so remember var variables do not get reset on every bar so this will track the price when it's going horizontal i'm going to call it baseline saved and it's going to be set to baseline by default when it's first initialized and then i'm going to use an if statement here that says if the bass line which is our red line the inbuilt calculated donchi and whatever you say that word donchian line if this red line is not equal to its value on the previous bar that means it's moving either down or up if this value is equal to the previous bar then it's going sideways obviously so this if statement will track that all we need to do now is save our baseline saved variable so now this variable will be assigned the value of baseline and then we can say else so if the baseline is not equal to the previous baseline that means the line is moving down or up as i said and we want to set baseline saved to n a so it doesn't draw anything on the chart and so now we can plot this onto our chart just using the plot function baseline saved is the value we want to plot i'll give it a color of color dot purple and we need to make sure that we set the style whoops to plot dot style underscore line br br is short for break and that will break the line um whenever its value is not equal to the previous value so i'll show you what i mean here first of all let me title this really quick we'll call it baseline saved save my code and now we have this purple line drawing here and sorry it's first thing in the morning i'm still waiting for my coffee to kick in i have put these around in the wrong order so i'm actually tracking only when it's moving down which is the opposite of what we want so here we're saying some of you would have picked that up while i was explaining this but now if the baseline on the current bar is not equal to the baseline on the previous bar then we set baseline saved to n a or nothing otherwise if these two values are the same that means that we are going sideways or horizontal and we do save the baseline value so now if i save my code we should be getting the correct thing happening here there we go so now the purple line is only drawing when the red line is horizontal and so now i can turn off our baseline red line save my code and uh whoops what have i done i have forgotten to put in the parameter name that'll do it save the code let that load there we go so now you can see that we have a really nice support resistance kind of thing happening here and it is tracking our baseline only when it's going horizontal now as soon as this baseline changes the value changes we stop drawing a horizontal line if for whatever reason you wanted to continue drawing that line you could just get rid of this statement and make this equals and then save my code and now it's going to save that line regardless of if there's a new baseline but for now i'll leave it how it was whoops save my code and to wrap up this lesson i'm going to show you how to track your prices above or below this purple line so that too is pretty easy i can just say here is price above baseline equals is a closing price greater than baseline saved and then just to track this in an easy manner i'm going to use a background color function and we'll just say if price is above our baseline then set the color to color.green actually color.new color.green and then we need to add some transparency otherwise it'll be very bright and otherwise if price is not above if the closing price value of the current candle is not greater than our purple line then we want to set the color to color dot red with 50 transparency and that's it now if i save my code we should be getting a background color there we go it's still pretty bright but you can see that the script is tracking when price is above our purple line now because we set baseline save to n a that kind of complicates things here because what it's doing is now when our baseline is n a it's just plotting red so what we need to do here is say i'll simplify this a little bit um let's say uh price trading above bl for baseline and price trading below bl for below the baseline and what we can say here is is price above baseline and not n a baseline saved now i can copy this down here and say not so this is going to be a little bit confusing especially for those of you who are newer to pine you probably shouldn't be watching this lesson if you aren't you should go through the course as it's laid out as a lot of this information is explained throughout the course in a methodical way but what we're doing here is we're saying this is a boolean value that will only be true if price is trading above the baseline so the closing price is above our baseline saved and our baseline saved is not an a so now i can change this code here maybe to say let me cut this out this will make sense in a moment i'll have two background color functions here price trading above baseline and price trading below baseline i'll paste that in and so now when i save this code hopefully i've done this correctly and i have now what we're seeing is that when our purple line is drawing let me lower the transparency of this a bit because it's still a bit too bright so now when price is trading above this purple line and the purple line is drawing onto our chart our background turns green and this boolean value here is tracking it'll be only set to true if this condition is met so we have a baseline that's horizontal and price is above it this variable on the other hand is the opposite we must have a baseline going horizontal and price must be below that baseline and that's it that uh pretty much um if i turn off the cloud that is answering the question from marty now what you do with this information is up to you and obviously there's a million different things you could do with this you could track uh price action patterns while prices above or below this purple line um you could track tests so like here for example it's a very nice test bullish engulfing candle uh hammer candle followed by a bullish engulfing candle right off this baseline while it's moving horizontal really nice trading opportunity there you could track this sort of thing by checking um you know you could have candle pattern equals price trading above bl and low let's say previous low is lower than our baseline saved and the closing price of the current bar is greater than baseline saved that would detect a candlestick rejection off our baseline and if i use the plot shape function here and we say candle pattern color let's go style equal shape dot uh triangle up color dot green save my code that should be tracking yeah there we go i've got the thing in the wrong place uh location equals location.below bar save my code and let me lower the transparency on this too it's annoying me now well that was the opposite whoops i need to go the other way there we go so now you can see we're tracking rejections off this baseline when it's moving sideways and you could obviously do the opposite for short trades it's not perfect needs some refinement this for example isn't a rejection off the line this is a recovery back above the line but you get the idea with a few more conditions added to these checks you could detect candlestick patterns above or below or on these uh baselines in the mastery course i left this lesson off here but i'm going to expand this lesson just slightly to explain how you can better track actual rejections off these purple lines so all i did here if i scroll down to the bottom of my script let's add some white space here i added one more condition to our candle pattern bull and candle bat candle pattern bear boolean conditions so now instead of just checking if price is trading above the baseline or below the baseline we check is price trading above the baseline and was it trading above the baseline on the previous bar that gets rid of a lot of false signals so now you can see we're getting a lot more sort of accurate rejections off this purple line so whenever the purple line is going horizontally and price tests it trades above it and then gets rejected and closes back below it we get this arrow drawing above or below that candle now i'm not saying you should trade this obviously it's very rudimentary but with a bunch of additions you can detect really nice trading patterns like this for example you could check for hammer candles of this purple line you could check for bullish engulfing candles this candlestick pattern right here a long wick to the downside followed by a close in the upper third of the candle followed by a bullish engulfing candle or a bearish example in the opposite direction uh would look something like this and we're rejected to the downside sorry i don't know why i bother drawing with this tool i'm terrible at it but hopefully you get the idea that is a candlestick pattern that i love to trade in my own trading i often implement this candlestick pattern into whatever sort of market condition i'm trying to capture so for example i have my ultimate pullback indicator if i throw that on really quickly once it loads you can see that this also picks up this exact same pattern above a moving average so those are just some ideas to play around with i'll wrap the lesson up here the source code will be below with the added changes to the code to make the candlestick pattern detection a little bit more accurate and so yeah i'm going to leave this lesson here as always a link to this source code will be below in the description if you did find this lesson interesting please hit the subscribe button and all of that youtube jargon that i'm supposed to say and i'll speak with you in the next lesson don't forget to check out the mastery course if you're new to plan script i think you'll find it incredibly valuable a lot of traders have so far that's why i feel confident saying that if you sign up i'd love to have you there and i'd love to help you on your journey if not that's fine too stick around on the blogs and i'll be back soon with some more content take care and good luck with your training as always [Music] you
Sunday, May 26, 2024
Tracking the Ichimoku Base Line in PINE SCRIPT
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment