# Problem with loop

I try to write a loop in order to detect a double top pattern.
The problem is that i don’t get any results when I run the exploration.
Can anybody help

``````ATR1=ATR(30);
for( i = 8; i <= 12; i++ )
{
PEAK1=Peak(C,i,1);
PEAK2=Peak(C,i,2);
CONDITION1=abs(PEAK1-PEAK2)<.2* ATR1 ;//AND PEAKB1-PEAKB2<60;
PKB=IIf(CONDITION1[I],I,100);}

PEAK11=Peak(C,PKB,1);
PEAK21=Peak(C,PKB,1);
CONDITION11=abs(PEAK11-PEAK21)<.2* ATR1 ;//AND PEAKB1-PEAKB2<60;
Filter=CONDITION11 ;

``````

@Mike11 take a look at the user library for original solution and scroll down to user modifications,

http://www.amibroker.com/members/library/detail.php?id=19&hilite=Peak

As for your code, debug your code and see that your variables are not calculating what you think they are. I haven't had a close look but you probably need to change it.

1 Like

@portfoliobuilder thanks for the reply. It looks as though my code is always producing a PKB=100 without testing CONDITION1 .
What I want to do is to find an appropriate % peak (PKB) from 8-12% so that the Peak1 (c,PKB,1) is approx equal to Peak2 (c,PKB,2) .
Changing the code as follows

``````ATR1=ATR(30);
for( i = 8; i <= 12; i++ )
{
PEAK1=Peak(C,i,1);
PEAK2=Peak(C,i,2);
CONDITION1=abs(PEAK1-PEAK2)<.2* ATR1 ;//AND PEAKB1-PEAKB2<60;
{if (CONDITION1[i]) PKB=i; break;}}

Filter=CONDITION1 ;type or paste code here
``````

It does produce some results but it doesn’t go through the loop and stops at i=8.
removing the break comand from the last line calculates values only for i=12.
I can’t figure out what to do in order to go through the loop and break only when condition1 is true and return the value of i.