Code for percentage from Candlestick
Forums › ProRealTime English forum › ProScreener support › Code for percentage from Candlestick
- This topic has 8 replies, 2 voices, and was last updated 7 years ago by Marcel van Vliet.
-
-
09/28/2017 at 6:12 PM #47713
Hi, Next question in my programming career :-).
What is the code for dividing a candlestick or bar in a percentage, if there is any?
I need this for the coding of a piercing pattern. (the second candle is piercing for more than 50% into the first).
Thank you………..
09/28/2017 at 8:57 PM #47730There is an indicator with a lot of candle patterns, also good for source code reading/learning, that’s here:
https://www.prorealcode.com/prorealtime-indicators/candlesticks-patterns-indicator/
Have fun exploring it.
09/29/2017 at 12:56 PM #47811@AVT, Thanks for thinking with me. I already know this indicator, but cannot manage to fathom it yet.
09/29/2017 at 6:30 PM #4784509/30/2017 at 12:04 PM #47866In 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
1234567891011| <---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 ---12455bar1=n+1 <---- bar2=n reference barNow 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=0Now let’s have a look into that complicated code of the candlesticks-patterns-indicator. The pattern has this code:
snip PiercingLine1PiercingLine=(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”:
12bar1=n+1 <---- bar2=n <--- reference barbar1=1 <---- bar2 <--- codeWe 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:
definition of variables12345678910111213body=close-open // the opposite way of calculating the bodyabody=abs(body) // absolute value: see Mathematicalif range>0 then // range is a reserved word: see Constantsratio=abody/range // ratio of body to rangeelseratio=0endifmiddle=(open+close)/2bodytop=max(open, close) // greatest value: see Mathematicalbodybottom=min(open, close) // smallest value: see Mathematicalshadowtop=high-bodytopshadowbottom=bodybottom-lowlongcandle= (ratio>0.6) // condition: ratio must be above 0.6With this at hand we analyse
parts of piercing line12345678PiercingLine=(body[1]<0 // means body-value of bar2 calculated as close-open, same as our open[n+1]-close[n+1]>0longcandle[1] // means ratio between body and the range of bar1 is more than 0.6longcandle // same for bar2open<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]>pierceclose<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.
2 users thanked author for this post.
10/04/2017 at 2:54 PM #48199@AVT, Thanks for this detailed explanation. It will take me a while before I have translated it in for me understandable pieces, but I am sure I gonna get the piercing pattern screener working with it.
10/04/2017 at 5:21 PM #48212@AVT, If I understand your explaination correctly, this should be the code for the Dark Cloud Cover pattern (The opposit of the Piercing pattern).
Would you please be so kind to check this for me, so I know I am on te right track?
Dark Cloud Cover1234n=0pierce=(close[n]<open[n+1])/2+close[n+1]C5=(close[n]<open[n+1]) and (close[n]<pierce) and (open[n+1]-close[n+1]>0 and (open[n]<low[n+1]))Screener [C5]10/05/2017 at 1:10 AM #48226You are on the way of getting it, some corrections (don’t lose hope, I am fighting this thing called “logic” more than once in new code)
Bulkowski: Look for two candles in an upward price trend.
The first candle is a tall white one
followed by a black candle with an opening price above the top of the white candle
(an opening price above the prior high),
but a close below the mid point of the white body.|
—<–open —12425 |
| <–high |X| —12415 |
^ —<–close –|X| ——— —12400 |– pierce=close+middle=12400+(-75)=12325
| | | |X| |
| | |<–pierce–|X|———- —? ——|—12325
| | | |X| | middle=(-150)/2=(-75)
| —<–open — —<–close —12250 |– body=open-close=12250-12400=(-150)
| |bar1=n+1 <— bar2=n reference bar
white bar black barpierce=(open[n+1]-close[n+1])/2 + close[n+1]
white=open[n+1]-close[n+1]<0
bar2: open[n]>high[n+1] and
close[n]<pierce and
close[n]>open[n+1]n=0;
pierce=(close[n]<open[n+1])/2+close[n+1] // (open[n+1]-close[n+1])/2 =>cannot divide close<open by 2
C5= (close[n]<open[n+1]) // > =>below mid point of white body<–ends at open, so must be still above
and (close[n]<pierce) // ok
and (open[n+1]-close[n+1]>0 // <0 =>first candle goes up, open-close becomes negative
and (open[n]<low[n+1])) // >high[n+1] =>open above the top=high of white
Screener [C5]open-close>0 black candle (reminder: you need more black color)
open-close<0 white candle (reminder: you need less black color)1 user thanked author for this post.
10/05/2017 at 10:33 AM #48244@AVT, Thank you for your help again…….
-
AuthorPosts
Find exclusive trading pro-tools on