Hello,
This simple code (if it can be simplified) can be used for identifying earliest reversals. The approach is to identify Bullish Bar Closing above 2 Bearish Bar and vice versa as shown in the document. The issue is if we use “if” statement then the program is becoming very big. Any simplification will be highly appreciated.
Regards.
c=close
o=open
l=low
hi=high
c1=c[1]
c2=c[2]
c3=c[3]
c4=c[4]
c5=c[5]
c6=c[6]
c7=c[7]
c8=c[8]
C9=C[9]
c10=c[10]
C12=C[12]
o1=o[1]
o2=o[2]
o3=o[3]
o4=o[4]
o5=o[5]
o6=o[6]
o7=o[7]
o8=o[8]
O9=O[9]
o10=o[10]
O12=O[12]
l1=l[1]
l2=l[2]
l3=l[3]
l4=l[4]
l5=l[5]
l6=l[6]
l7=l[7]
l8=l[8]
L9=L[9]
h1=hi[1]
h2=hi[2]
h3=hi[3]
h4=hi[4]
h5=hi[5]
h6=hi[6]
h7=hi[7]
h8=hi[8]
H9=HI[9]
h10=hi[10]
H12=HI[12]
if c>=o and c1<o1 and c2<o2 AND H1<H2 and c>h2 then
t=10
ELSif c>=o and c1<o1 and c2>=o2 AND C3<O3 AND H1<H3 and c>h3 then
t=10
elsif c>=o and c1<o1 and c2<o2 and c3<o3 and h1<h3 and h2<h3 and c>h3 then
t=10
elsif c>=o and c1>=o1 and c2<o2 and c3<o3 and h2<h3 and c1<h3 and c>h3 then
t=10
elsif c>=o and c1<o1 and c2<o2 and c3<o3 and h1>h2 and h2<h3 and c>h3 then
t=10
ELSif c>=o and c1>=o1 and c2<o2 AND C3<O3 and c4<o4 AND H3<H4 and h2<=h4 and c1<=h4 and c>h4 then
t=10
ELSif c>=o and c1<o1 and c2>=o2 AND C3<O3 and c4<o4 AND H3<H4 and c2<=h4 and c>h4 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3<o3 and c4<o4 and h3<h4 and c1<h4 and c>h4 then
t=10
elsif c>=o and c1>=o1 and c2<o2 and c3>=o3 and c4<o4 and h2<h4 and c1<h4 and c>h4 then
t=10
elsif c>=o and c1>=o1 and c2<o2 and c3<o3 and c4<o4 and h3<h4 and c1<h4 and c>h4 then
t=10
elsif c>=o and c1>=o1 and c2<o2 and c3<o3 and c4<o4 and h2>h3 and l2<l3 and h3<h4 and c1<h4 and c>h4 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3<o3 and c4>=o4 and c5<o5 and h3<h5 and c1<h5 and c>h5 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3>=o3 and c4<o4 and c5<o5 and h4<h5 and c3<h5 and c2<h5 and c1<h5 and c>h5 then
t=10
elsif c>=o and c1<o1 and c2>=o2 and c3>=o3 and c4<o4 and c5<o5 and h4<h5 and c3<h5 and c2<h5 and c>h5 then
t=10
elsif h4<h5 and c5<o5 and c4<o4 AND c3<=h5 and c2<=h5 and c1<=h5 and c>h5 AND C>=O then
t=10
elsif c>=o and c1<o1 and c2<o2 and c3>=o3 and c4>=o4 and c5<o5 and c6<o6 and H5<h6 and c3<h6 AND C4<H6 and c>h6 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3>=o3 and c4<o4 and c5>=o5 and c6>=o6 AND C7<O7 and H4<h7 and c3<=h7 AND C2<=H7 and c1<=H7 AND C>h7 then
t=10
elsif c>=o and c1>=o1 and c2<o2 and c3>=o3 and c4>=o4 and c5<o5 and c6<o6 and H5<h6 and h2<h6 and c4<=h6 and c3<=h6 AND c1<=H6 AND C>h6 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3<o3 and c4>=o4 and c5<o5 and c6<o6 and H5<=h6 and h3>=h6 and c4<=h6 and c2<=h6 AND c1<=H6 AND C>h6 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3>=o3 and c4>=o4 and c5<o5 and c6<o6 AND C7<O7 and H6<h7 and h5>=h6 and c4<=h7 and c3<=h7 AND C2<=H7 and c1<=H7 AND C>h7 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3>=o3 and c4<o4 and c5>=o5 and c6<o6 AND C7<O7 and H6<h7 and c5<=h7 AND C3<=H7 AND C2<=H7 and c1<=H7 AND C>h7 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3<o3 and c4<o4 and c5>=o5 and c6<o6 and h3=h4 and h4<h6 and c1<h6 and c>h6 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3>=o3 and c4>=o4 and c5<o5 and c6<o6 and h5<h6 and c2<h6 and c1<h6 and c>h6 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3>=o3 and c4<O4 and c5<o5 and c6<o6 AND H4>H5 and h5<h6 and c2<h6 and c1<h6 and c>h6 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3>=o3 and c4>=O4 and c5<o5 and c6<o6 AND h5<h6 AND C4<=H6 AND C3<=H6 and c2<=h6 and c1<=h6 and c>h6 then
t=10
elsif c>=o and c1<o1 and c2>=o2 and c3>=o3 and c4<O4 and c5>=o5 and c6<o6 AND h4<h6 AND C3<=H6 AND C2<=H6 and c>h6 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3<o3 and c4>=O4 and c5>=o5 and c6<o6 AND h3<h6 AND C2<=H6 AND C1<=H6 ANd c>h6 then
t=10
elsif h5<h6 and c6<o6 and c5<o5 AND c4<=h6 and c3<=h6 and c2<=h6 and c1<=h6 and c>h6 AND C>=O then
t=10
elsif c>=o and c1<o1 and c2>=o2 and c3>=o3 and c4>=o4 and c5>=o5 and c6<o6 and c7<o7 and h6<h7 and c2<=h7 and c3<=h7 and c4<=h7 and c5<=h7 and c>h7 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3<o3 and c4>=o4 and c5<o5 and c6>=o6 and c7<o7 and h5<h7 and h3<h7 and c2<=h7 and c4<=h7 and c6<h7 and c1<=h7 and c>h7 then
t=10
elsif c>=o and c1<o1 and c2>=o2 and c3>=o3 and c4<o4 and c5<o5 and c6>=o6 and c7<o7 and h5<h7 and h4>=h5 and H1>=H5 AND c2<=h7 and c3<=h7 AND c>h7 then
t=10
elsif h5<h7 and c7<o7 and c5<o5 AND C6>O6 and c4<=h7 and c3<=h7 and c2<=h7 and c1<=h7 and c>h7 AND C>=O then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3>=o3 and c4>=o4 and c5<o5 and c6>=o6 and c7>=o7 and c8<o8 and h5<h8 and c1<h8 and c2<h8 and c3<h8 and c4<h8 and c>h8 then
t=10
elsif c>=o and c1>=o1 and c2>=o2 and c3<o3 and c4>=o4 and c5<o5 and c6>=o6 and c7>=o7 and c8<o8 and h5<h8 and c1<h8 and c2<h8 and c4<h8 and c>h8 then
t=10
elsif c>=o and c1>=o1 AND c5<o5 and c6>=o6 and c7>=o7 and c8<o8 and h5<h8 and c1<h8 and c>h8 then
t=10
elsif c>=o and c1>=o1 AND c2<o2 and c3>=o3 and c4>=o4 and c5<o5 and h2<h5 and c1<h5 and c>h5 then
t=10
elsif c>=o and c1>=o1 AND c2>=o2 and c3<o3 and c4>=o4 and c5<o5 AND C6<O6 and h5<h6 AND C4<=H6 AND C2<=H6 and c1<=h6 and c>h6 then
t=10
elsif c>=o AND C1<O1 and c2>=o2 and c3>=o3 and c4<o4 and c5>=o5 and c6>=o6 and c7>=o7 and c8<o8 and h4<h8 and c2<h8 and c>h8 then
t=10
elsif c>=o AND C1>=O1 and c2>=o2 and c3<o3 and c4>=o4 and c5>=o5 and c6<o6 and c7<o7 and h6<h7 and c5<=h7 AND C4<=H7 and c2<=h7 AND C1<=H7 and c>h7 then
t=10
elsif c>=o AND C1>=O1 and c2>=o2 and c3<o3 and c4>=o4 and c5>=o5 and c6>=o6 and c7<o7 and h3<h7 and c2<=h7 AND C1<=H7 and c>h7 then
t=10
elsif c>=o and c1>=o1 and c2<o2 and c3<o3 and c4>=o4 and c5>=o5 and c6>=o6 and c7>=o7 and c8<o8 AND C9<O9 and h8<H9 AND H2>H3 and c1<h9 AND C>H9 then
t=10
elsif h3<h5 and c5<o5 and c3<o3 AND c2<=h5 and c1<=h5 and c>h5 AND C>=O then
t=10
elsif h6<h7 and c7<o7 and c6<o6 AND C5<=H7 and c4<=h7 and c3<=h7 and c2<=h7 and c1<=h7 and c>h7 AND C>=O then
t=10
elsif h5<h8 and c8<o8 and c5<o5 AND c4<=h8 and c3<=h8 and c2<=h8 and c1<=h8 and c>h8 AND C>=O then
t=10
elsif h6<h8 and c8<o8 and c6<o6 AND C5<=H8 and c4<=h8 and c3<=h8 and c2<=h8 and c1<=h8 and c>h8 AND C>=O then
t=10
elsif h7<h8 and c7<o7 and c8<o8 and h6<h8 and c5<=h8 and c4<=h8 and c3<=h8 and c2<=h8 and c1<=h8 and c>h8 then
t=10
elsif h7<h10 and c10<o10 and c7<o7 and c6<=h10 and c5<=h10 and c4<=h10 and c2<=h10 AND C1<=H10 and c>h10 then
t=10
elsif h8<h9 and c9<o9 and c8<o8 and c7<=h9 and c6<=h9 and c5<=h9 and c4<=h9 and c3<=h9 and c2<=h9 and c1<=h9 and c>h9 then
t=10
elsif h9<h10 and c10<o10 and c9<o9 and c8<=h10 and c6<=h10 and c5<=h10 and c4<=h10 and c2<=h10 and c>h10 then
t=10
elsif h10<h12 and c10<o10 and c12<o12 and C9<=H12 AND c8<=h12 and C7<=H12 AND c6<=h12 and c5<=h12 and c4<=h12 AND c3<=h12 and c2<=h12 and c1<=h12 and c>h12 then
t=10
elsif c<o and c1>=o1 and c2>=o2 and l1>l2 and c<l2 then
t=-10
elsif c<o and c1>=o1 and c2<o2 and c3>=o3 AND L1>L3 AND c<l3 then
t=-10
elsif c<o and c1<o1 and c2>=o2 and c3>=o3 and c1>l3 and c<l3 then
t=-10
elsif c<o and c1>=o1 and c2<o2 and c3>=o3 and c4>=o4 and l3>l4 and c2>l4 and c<l1 and c<l4 then
t=-10
elsif c<o and c1<o1 and c2<o2 and c3>=o3 and c4>=o4 and c5>=o5 and c1>l4 and c<l5 then
t=-10
elsif l4>l5 and c5>=o5 and c4>=o4 and c3>=l5 and c2>=l5 and c1>=l5 and c<l5 then
t=-10
elsif c<o and c1>=o1 and c2<o2 and c3>=o3 and l1 and c1>l4 and c<l5 then
t=-10
elsif c<o and c1<o1 and c2>=o2 and c3<o3 and c4>=o4 and l2>l4 and c1>l4 and c<l4 then
t=-10
elsif c<o and c1>=o1 and c2<o2 and c3<o3 and c4>=o4 and l1>l4 and c<l4 then
t=-10
elsif c<o and c1<o1 and c2<o2 and c3>=o3 and c4>=o4 and l3>l4 and c1>l4 and c<l4 then
t=-10
elsif c<o and c1<o1 and c2>=o2 and c3<o3 and c4<o4 and c5>=o5 and c6>=o6 and l5>l6 and c3>l6 and c1>l6 and c<l6 then
t=-10
elsif c<o and c1<o1 and c2>=o2 and c3<o3 and c4>=o4 and c5<o5 and c6>=o6 and l4>l6 and c3>l6 and c1>l6 and c<l6 then
t=-10
elsif c<o and c1>=o1 and c2<o2 and c3>=o3 and c4<o4 and c5<o5 and l3>l6 and c2>l6 and c<l6 then
t=-10
elsif c<o and c1>=o1 and c2<o2 and c3<o3 and c4>=O4 and c5>=o5 and l4>l5 and c2>=L5 AND C3<=L5 and c<l5 then
t=-10
elsif c<o and c1<o1 and c2>=o2 and c3<o3 and c4>=o4 and c5<o5 and c6<o6 and c7>=o7 and l4>l7 and l2>l7 and c1>l7 and c<l7 then
t=-10
elsif l6>l7 and c7>=o7 and c6>=o6 and c5>=l7 and c4>=l7 and c3>=l7 and c2>=l7 and c1>=l7 and c<l7 then
t=-10
elsif l7>l8 and c8>=o8 and c7>=o7 and c6>=l8 and c5>=l8 and c4>=l8 and c3>=l8 and c2>=l8 and c1>=l8 and c<l8 then
t=-10
elsif l8>l9 and c8>=o8 and c9>=o9 AND C7>=L9 and c6>=l9 and c5>=l9 and c4>=l9 and c3>=l9 and c2>=l9 and c1>=l9 and c<l9 then
t=-10
elsif t[7]=10 and c7>=o7 and c5>=o5 and l5<=l7 and c2>=o2 and l2<=l5 and c<l2 and c<o then
t=-10
endif
return t as "2B-LS", 1 AS "L", -1 AS "S"
COND will be true whenever your pattern is detected:
Bullish = close > open
Bearish = close < open
c1 = (Summation[2](Bearish[1]) = 2)
c2 = (close > highest[2](close[1]))
Cond = c1 AND c2
Hello Robert, thank you for your response, however,
Code is throwing error: as per the first chart in the attachment.
When modified by remarking the “Bullish” line, it is not working as desired: as per the second chart in the attachment.
The desired behaviour should be like the below: (as per my lengthy code, which I want to be shortened): as per the last chart in the attachment.
Attach image file types for images, JPG or PNG extensions, please. Thank you.
I couldn’t detect what you wanted to highlight in your second pic.
I forgot to use the BULLISH variable. There you go:
Bullish = close > open
Bearish = close < open
c1 = (Summation[2](Bearish[1]) = 2)
c2 = (close > highest[2](close[1]))
CondL = c1 AND c2 AND Bullish
d1 = (Summation[2](Bullish[1]) = 2)
d2 = (close < lowest[2](close[1]))
CondS = d1 AND d2 AND Bearish
Cond = 0
If CondL Then
Cond = 1
Elsif CondS then
Cond = -1
Endif
Return Cond
I also added the viceversa.
In your first post you talked about “Bullish Bar Closing above 2 Bearish Bar” but your file had more patterns.
Thank you Robert, but it seems you have not correctly understood the conditions. Here they are:
For identifying DOWN trend the requirement is
-
- bearish bar should close below the lowest low of the previous 2 bullish bars (they (bullish and/ or bearish) may not be consecutive and the last bullish bar’s low should be higher than the previous bullish bar’s low).
For identifying UP trend the requirement is
-
- bullish bar should close above the highest high of the previous 2 bearish bars (they (bullish and/ or bearish) may not be consecutive and the last bearish bar’s high should be lower than the previous bearish bar’s high).
Your new code is throwing wrong results (enclosed chart named robert.jpg). It should be like enclosed correct.jpg
Many thanks for your time and effort.
No, you wrote you wanted to find “Bullish Bar Closing above 2 Bearish Bar”.
To find something different, a different code is needed.
I’ll make it as soon as I can.
There you go:
DEFPARAM CalculateOnLastBars = 1000
Bullish = close > open
Bearish = close < open
//
// DOWN trend detection
//
Count1 = 0
i = 0
WHILE Count1 < 2 AND i < BarIndex
i = i + 1
IF Count1 = 0 THEN
IF Bullish[i] THEN
Count1 = 1
MyLO1 = low[i]
ENDIF
ELSE
IF Bullish[i] THEN
Count2 = 2
MyLO2 = low[i]
d1 = MyLO1 > MyLO2
MyLO2 = min(MyLO1,MyLO2)
Break
ENDIF
ENDIF
WEND
d2 = Bearish
d3 = close < MyLO2
Dcond = d1 AND d2 AND d3
//
// UP trend detection
//
Count2 = 0
j = 0
WHILE Count2 < 2 AND j < BarIndex
j = j + 1
IF Count2 = 0 THEN
IF Bearish[j] THEN
Count2 = 1
MyHI1 = high[j]
ENDIF
ELSE
IF Bearish[j] THEN
Count2 = 2
MyHI2 = high[j]
u1 = MyHI1 < MyHI2
MyHI2 = max(MyHI1,MyHI2)
Break
ENDIF
ENDIF
WEND
u2 = Bullish
u3 = close > MyHI2
Ucond = u1 AND u2 AND u3
//
Cond = 0
IF Ucond THEN
Cond = 1
ELSIF Dcond THEN
Cond = -1
ENDIF
RETURN Cond AS "Signal"
Great help Robert. You are brilliant. The code is missing some of the signals as captured in the enclosed document. Can it be fixed please.
Also, can it be possible to make it continuous histograms as shown in the enclosed document.
Many thanks.
When you need to attach images, please use JPG or PNG files. PDF are just for documents AND TXT are for unfornatted text. Thank you 🙂
Please tell me the rules you want to add, using text, not pics.
If you remove line 55 you’ll have a continuous histogram, since the previous signal is retained when there’s currently none.
Roberto code above added as Log 230 here …
Snippet Link Library
Hello Robert, thank you for your time. The requirement is the same as before as written earlier and now as below:
For identifying DOWN trend the requirement is
-
- bearish bar should close below the lowest low of the previous 2 bullish bars (they (bullish and/ or bearish) may not be consecutive and the last bullish bar’s low should be higher than the previous bullish bar’s low ignoring the inside bar in between is any).
For identifying UP trend the requirement is
-
- bullish bar should close above the highest high of the previous 2 bearish bars (they (bullish and/ or bearish) may not be consecutive and the last bearish bar’s high should be lower than the previous bearish bar’s high ignoring the inside bar in between is any).
Many thanks for your time and effort.
I’ll take care of it early next week.
Try this one (not tested):
DEFPARAM CalculateOnLastBars = 1000
Bullish = close > open
Bearish = close < open
InsideBAR = (range < range[1]) AND (high <= high[1]) AND (low >= low[1])
//
// DOWN trend detection
//
Count1 = 0
i = 0
WHILE Count1 < 2 AND i < BarIndex
i = i + 1
IF Count1 = 0 THEN
IF Bullish[i] AND Not InsideBAR[i] THEN
Count1 = 1
MyLO1 = low[i]
ENDIF
ELSE
IF Bullish[i] AND Not InsideBAR[i] THEN
Count2 = 2
MyLO2 = low[i]
d1 = MyLO1 > MyLO2
MyLO2 = min(MyLO1,MyLO2)
Break
ENDIF
ENDIF
WEND
d2 = Bearish
d3 = close < MyLO2
Dcond = d1 AND d2 AND d3
//
// UP trend detection
//
Count2 = 0
j = 0
WHILE Count2 < 2 AND j < BarIndex
j = j + 1
IF Count2 = 0 THEN
IF Bearish[j] AND Not InsideBAR[j] THEN
Count2 = 1
MyHI1 = high[j]
ENDIF
ELSE
IF Bearish[j] AND Not InsideBAR[j] THEN
Count2 = 2
MyHI2 = high[j]
u1 = MyHI1 < MyHI2
MyHI2 = max(MyHI1,MyHI2)
Break
ENDIF
ENDIF
WEND
u2 = Bullish
u3 = close > MyHI2
Ucond = u1 AND u2 AND u3
//
Cond = 0
IF Ucond THEN
Cond = 1
ELSIF Dcond THEN
Cond = -1
ENDIF
RETURN Cond AS "Signal"