finding nearest price level

 

Hi

To search via time or bar numbers I use this for example:

for(i=2;i<3;i++){

I am trying the use the same type of code against price, to search for the nearest pivot line to the current price but I cant seem to get it to work, here is the code is was using to search for pivot lines below the Bid price:

double i, close = Bid;

        if(Close[2]<Open[2]){
              for(i=0;i<close;i++){
                   if(pivot[i]>pivot[i-1]){currentpivot = pivot[i];}
                  }
               }

//currentpivot is doubled on global scope
//close is current price

Can anyone see what I am doing wrong?

Thank you

Antony

 

This makes no sense . . . .

for(i=0;  i < close;  i++)

i starts at 0, and the loop executes while i < Bid . . . for each iteration i is incremented . . .

 

Hi

Ok maybe something like this:

for(i=pivot[27];  i < close;  i++)

//pivot[27] being the lowest possible pivot line and the search upwards in price to find the nearest pivot line to the current price?

Or is this still wrong? :/

Thank you

Antony

 
tonyjms2005:

Hi

Ok maybe something like this:

Or is this still wrong? :/

I think it's still wrong, you need to increment the index not the price . . . I find while loops easier to use in situations like this.
 

Ok you mean something like this then:

int i;              
  while(pivot[i]<close){
                   i++;
                   currentpivot = pivot[i];
                  }

Sorry if I sound a bit stupid but I am a novice coder :/

Thank you

Antony

 
tonyjms2005:

Ok you mean something like this then:

Sorry if I sound a bit stupid but I am a novice coder :/

Yep, something like that.
 

Hi

Just another question, this code works well for finding the nearest pivot below the current price:

int i;  
while(Bid>pivot[i]){
                   i++;
                   currentpivot = pivot[i];
                  }
               }

If I want to search for pivot above the current price it doesn't seem to work, I have tried this:

  while(Bid<pivot[i]){
                   i--;
                   currentpivot = pivot[i];
                  }
               }

But it just wont work, any pointers?

Thank you

Antony

 
I don't really understand how your pivots are arranged within the array . . . but if the first bit of code finds the nearest pivot below Bid then it can also find the nearest above Bid . . . it will be the next pivot.
 

Basically, I have set it up so

R3 = pivot[1]

R2 = pivot[2]

R1=pivot[3]

pivotpoint=pivot[4]

S1=pivot[5]

S2=pivot[6]

S3=pivot[7]

So i am trying to find where the current price falls between whihc pivots.

Antony

 
Put them in ORDER and then you can use the above codes or ArrayBsearch. Your supports are reversed order vs resistance and are above R group.
#define INF 0x6FFFFFFF // Not quite infinite, Jul 2029, or 1,879,048,191
double pivot[9];
pivot[8]    = INF;
pivot[7]    = R3;
pivot[6]    = R2;
pivot[5]    = R1;
pivot[4]    = pivotpoint;
pivot[3]    = S3;
pivot[2]    = S2;
pivot[1]    = S1;
pivot[0]    = 0;

for (iPivot = 1; Bid > pivot[iPivot]; iPivot++){}
belowPivot = pivot[iPivot-1];
abovePivot = pivot[iPivot];
Reason: