Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Saturday, May 25, 2024

Detecting HAMMERS & SHOOTING STARS • Pine Script [OUTDATED V4] Tutorial

 hey traders welcome back to another pine script lesson this one is going to cover how to detect hammer and shooting star candles now this lesson is straight from the pine script mastery course and i've adapted it for youtube and this is just a bit of a sneak peek into what you can expect if you ever sign up to the mastery course but let's get on with the lesson now i'm re-recording this lesson well after releasing the mastery course program because i found a much better way to detect these candlestick patterns so what you see here is the old code this was the code from when i originally released the course and we are going to delete all of this and start again and i will show you how to detect these candlestick patterns with much much better accuracy and with more simplicity too actually for that matter so the way this method works is instead of just comparing the wick size to the body size like the old code did we're actually using a fibonacci calculation so let me get rid of some of these lines here we're going to use the fibonacci mathematics in order to determine whether or not a given candle closes within a certain range so right now i've set my fibonacci tool to display whoops to display the 33.3 percent fibonacci level and what i'm looking for is a candle whose body closes in this upper third range of the fibonacci and of course you can adjust this level to whatever you want in the settings menu when we're done with the script so for bullish hammer candles we're looking for the candle to close in the upper third range of the candle size so this candle here would actually no longer be considered a hammer candle according to these rules but of course we can adjust this later but anyway for uh bearish candles we're doing exactly the same thing but to the downside so we're calculating the fibonacci range to the downside and if the bar closes within that third of the candle size then it will be considered a valid shooting star candle so let's open up the pine editor and get coding the first thing i need to do is get user input not in pit input and we need two inputs here we're going to get the fib level that we want to use and so this input is just going to be of data type float and its default value is going to be 0.333 or 33.3 percent the next input we're getting is our color filter so this will be a boolean input this will allow the user to select whether or not they want the shooting star to be red in order to be considered valid so this candle that's printing right on my chart right now this would be considered a valid shooting star candle using our new fibonacci rule but some users prefer their shooting stars to be a red color so this filter that we're writing in here will be used for that so by default i'm going to leave this turned off so it'll be false in the settings menu and the user will have to come up to the settings menu and turn this on if they only want to detect shooting stars that are red and hammers that are green so the next thing we need to do is calculate our fibonacci level for the current candle and to do that the math is actually quite simple we need to calculate two different fibs though we need a bullish fibonacci and a bearish fibonacci because the math is not the same depending on which direction you're using this tool so for a bull fib we need to minus the low from the high and then multiply that by our fib level and then add the high back onto that result so this here will give us the 33.3 percent fibonacci retracement of any bullish candles for the bearish version we need to do the same but we need to flip our price sources around so we need to get the high minus the low multiplied by our fib level and then we need to add the low onto that value and that will give us the 33.3 percent uh price for bearish candles so that's it for calculating our fibonacci levels you don't need to understand this math in order to use it so i'll move on to the next part of the script which is to determine which price source closes or opens higher or lower so this will make sense in a second so so for this we're going to get two variables here we're going to declare two new variables one is called lowest body and one is called highest body and so this is for determining the body of the candle so for a bearish candle the open will be higher than the close and so the highest body will be the open but on a bullish candle the highest price source of the body will be the close and these values are used uh to determine whether or not the current bar closed above or below our fibonacci level so for our lowest body we need to check did the closing price close lower than the open if so then we want to reference the closing price otherwise we want to reference the open price same for the highest body did the current candles closing price close higher than it opened if so then we want to reference that closing price otherwise we want to reference the opening price now we have all the information we need in order to determine whether or not the current bar meets our criteria for a hammer or shooting start candle so that's what we're going to do next we're going to say determine if we have a valid hammer or shooting star so for a hammer candle we're going to check is the lowest body so for a bullish candle we're checking is the lowest body greater than or equal to our bull fibonacci if so then we have a valid hammer candle same for our shooting star candles we need to check is the highest body less than or equal to our bare fibonacci these two boolean variables here will uh flag any candles that meet our criteria for a bullish or bearish hammer or shooting star candle so the next thing we need to do or the final thing we need to do is plot the signals to the chart so we're going to use the plot shape function we're going to check do we have a hammer candle if we do then we want to plot a shape dot arrow up and we want to give it a location of location dot below bar and we want to give it a color of color.green and then we need to do exactly the same thing for our bearish candle so i can just copy that line of code change this to star candle change this to arrow down change location to above bar and the color to red and finally just because it's easy to do we will trigger alerts if the user has set an alert on this script so for that we need to use the alert condition function and we need to check do we have a hammer candle or a star candle then we can title this alert hammer or shooting star alert i mean give it a message of we'll just put the ticker in here the price sticker and of course this ticker placeholder will be replaced by whatever ticker of the market that you set the alert on and that's it if we save the script we'll now be detecting hammer and shooting star candles that meet our fibonacci criteria as you can see there the final thing we need to do is add in our color filter so for this i'm going to say and open bracket has the user disabled color filter then this one would turn true and if our lowest body exceeds our bull fib then our hammer candle will be detected otherwise if the user has actually turned color filled on then we want to check does the current closing price close higher than the current opening price or in other words do we have a green candle and now i can copy this to our star candle flip this operator around and now we have our color filter integrated so you can see here we have a red hammer candle that has a body that closes in the upper third of our fibonacci range on this candle if i come up to the settings menu and turn on our color filter that setup will go away now we're only detecting green hammers and red shooting stars and then finally you can adjust your fib level here so if you wanted to be a little more lenient with your field level you could set it to maybe a 382 and now you can see we are now detecting this hammer candle again and if you wanted to detect this shooting star candle here then you need to set it to about a 40 percent fibonacci so if we set that to four it still doesn't pick it up maybe a 0.5 50 fib there we go so now it's detecting these much larger bodied hammer candles so that's pretty much it for this script it's a pretty simple script but a very effective and simple way to detect these candlestick patterns in your indicators and strategy scripts so that's it for this lesson the source code will be available beneath the video as always so you can go and play around with this if you want to i hope you found this lesson interesting i'll see in the next one so that's it for this lesson i hope you found that helpful if you did and you want to learn more about pine scripts head over to panscriptmastery.com here you'll find my advanced pinescript courses and my basics course which is free so if you want to learn more about pinescript head over here and you'll find out a little bit more about me and a little bit more about i hope you found this lesson interesting and helpful i'll see you in the next one you

No comments:

Post a Comment