In your first post you wrote you need that for a piercing pattern. (Just in case: the “master of candles” has decribed it here: http://thepatternsite.com/Piercing.html). Before beginning to code I sometimes make a little sketch, just to see what is needed, like this:
- Condition for bar one: down bar
- Definition pierce: middle of body bar one
- Condition for bar two: open below bar ones low and close higher than piercing line and below bar ones open
| <---high
...<---open -- | <---high ------12500 -- body=12500-12470=30
|X| ...<---close ---12490 | 30/2=15
|X|<---pierce | | ---? -----|--12485
|X| | | |
...<---close -| |---------------12470 -- 12470+15
| <---low | | ---12465
...<---open ---12460
| <---low ---12455
bar1=n+1 <---- bar2=n reference bar
Now as we broke down the task into small pieces, we can make a simple code. To make it general we define the reference bar number as n.
Condition for bar one, being n+1: condition1=( open[n+1]-close[n+1]>0 )
Definition pierce, middle of the body, the body is: body1=open[n+1]-close[n+1]
Middle of it, simple devide by 2: middle1=body1/2
Value of the middle, add half of the body to close: middle1price=middle1+close[n+1]
All put together: pierce=(open[n+1]-close[n+1])/2+close[n+1]
Condition for bar two, being n, open below bar ones low: open[n]<low[n+1]
and close higher than pierce: close[n]>pierce
and close below bar ones open: close[n]<open[n+1]
All together: condition2=( (open[n]<low[n+1]) AND (close[n]>pierce) AND (close[n]<open[n+1]) )
One thing missing, define what value n is: n=0
Now let’s have a look into that complicated code of the candlesticks-patterns-indicator. The pattern has this code:
PiercingLine=(body[1]<0 and body>0 and longcandle[1] and longcandle and open<low[1] and close>middle[1] and close<open[1])
First we need to read the whole line to see how candles are counted: we see bare names like “open” or “close” and indexed names like “body[1]” or “low[1]”. This means when comparing to our version the bare names correspond to our “[n]” and the indexed ones to our “[n+1]”. On my sketch I would just paint another “timeline”:
bar1=n+1 <---- bar2=n <--- reference bar
bar1=1 <---- bar2 <--- code
We can break it into pieces now. First question here, what is body? Seems to be some selfdefined stuff, usually those things can be found before they are used, so we scroll a bit up and find all those definitions:
body=close-open // the opposite way of calculating the body
abody=abs(body) // absolute value: see Mathematical
if range>0 then // range is a reserved word: see Constants
ratio=abody/range // ratio of body to range
else
ratio=0
endif
middle=(open+close)/2
bodytop=max(open, close) // greatest value: see Mathematical
bodybottom=min(open, close) // smallest value: see Mathematical
shadowtop=high-bodytop
shadowbottom=bodybottom-low
longcandle= (ratio>0.6) // condition: ratio must be above 0.6
With this at hand we analyse
PiercingLine=(
body[1]<0 // means body-value of bar2 calculated as close-open, same as our open[n+1]-close[n+1]>0
longcandle[1] // means ratio between body and the range of bar1 is more than 0.6
longcandle // same for bar2
open<low[1] // open of bar2 lower than low of bar1, same as our open[n]<low[n+1]
close>middle[1] // close of bar2 above pierce, same as our close[n]>pierce
close<open[1] // close of bar2 below open of bar1, same as our close[n]<open[n+1]
)
In our example we did not see whether both bars are longcandles, nor did we look at the trend condition. Both should be considered as they are in the candlesticks-patterns, longcandle because it performs better and because its shape might interfere with another candle type; and trend because a candle pattern is only valid if certain trend conditions are met. But this is just an example of how to break down problems into smaller pieces and make a code from that. So no need for a 50% value (which is by the way calculated as value50percent=OfWhichValue*0.5).
Pitfalls: Decide just for one way of caculating the body and stick to that. It’s a matter of view: if I look at the close first and then at where we started opening, I use close-open; if I look at the open first and then where we finally end up closing, I use open-close; depending on that the result is above/below zero the opposite way.
General versus simple version: Our example is in code a bit longer than the one of the candlesticks-patterns because we use a general version. In an indicator where you need this only once, it is shorter to use the simple version. I prefer to write all my stuff if possible in general form; this needs to set n=ReferencePoint but with that I can for example let the user define what “n” should be, or if I need the code in a loop or IF condition where I do some other calculation, I can simply copy and paste it. Example: Let’s assume this piercing pattern works and it’s best to let it run for 10 bars and then exit. As we cannot future our code, we must look back while our bars run forward. If we set n=10 that means the 10th bar before our current bar pierced and we can now set at our current bar a marker for exit, if we set it n=9 or =8 we include some reaction time.
Hope I did not include a mistake in this long stuff 😉 and it helps you a bit to do the remaining tasks for your piercing.