iHighest strange result, cant figure out

 

Can anyone see a problem with this snippet:

I first try to determine which bar the trade opened on, then using the result of that I want to use iHighest to locate the highest bar since (and including) the trade opened. But I get a strange result...Let's assume the trade opened on bar 0 (the current bar), I then tell iHighest to look back, in this case start at 0 and end at 0, so only one bar to process, but it tells me the highest is bar 4 (which looking at the chart is correct, it is the highest). But what I cant figure out is that I didn't ask it to look back that far.

Appreciate the below code can be condensed in lesser lines, I just like to draw it out at first. I checked the correct order was selected and it is.

if I let the code continue to run, a new bar opens. Now the opened trade is sitting on bar 1. And now my code (iHighest) reports that bar 0 is the highest, which glancing at the chart is not correct.

                     //first find on which bar the currently selected trade was opened
                     BarNumberOrderOpened=iBarShift(NULL,0,OrderOpenTime(),false);
                     Print("THE ORDER OPENED ON BAR ", BarNumberOrderOpened);
                     
                     //now find the highest bar going back to the bar of the open time
                     HighestBarNumber = iHighest(NULL,0,MODE_HIGH,BarNumberOrderOpened,0);
                     Print("The HIGHEST bar was ", HighestBarNumber);
                     Print("the currently selected order is ",OrderTicket());
                     
                     //now we know which bar was highest, grab it
                     BarHigh = iHigh(NULL,0,HighestBarNumber);
                     Print("the recent HIGH was ", BarHigh);
 
idh:

Can anyone see a problem with this snippet:

I first try to determine which bar the trade opened on, then using the result of that I want to use iHighest to locate the highest bar since (and including) the trade opened. But I get a strange result...Let's assume the trade opened on bar 0 (the current bar), I then tell iHighest to look back, in this case start at 0 and end at 0, so only one bar to process, but it tells me the highest is bar 4 (which looking at the chart is correct, it is the highest). But what I cant figure out is that I didn't ask it to look back that far.

Appreciate the below code can be condensed in lesser lines, I just like to draw it out at first. I checked the correct order was selected and it is.

if I let the code continue to run, a new bar opens. Now the opened trade is sitting on bar 1. And now my code (iHighest) reports that bar 0 is the highest, which glancing at the chart is not correct.

You have to add 1 to BarNumberOrderOpened when use as "count" parameter for iHighest :

//now find the highest bar going back to the bar of the open time
HighestBarNumber = iHighest(NULL,0,MODE_HIGH,BarNumberOrderOpened+1,0);
 
angevoyageur:

You have to add 1 to BarNumberOrderOpened when use as "count" parameter for iHighest :



thank you angevoyageur, that seems to have fixed the issue. However, Im not sure I understand why. From the help file for iHighest, count parameter:


count=WHOLE_ARRAY

[in] Number of bars (in direction from the start bar to the back one) on which the search is carried out.


to me this says tell me how many bars back you want to from a given bar, e.g. 0 the current bar which we can refer to as the start, and for count, I may feed it 0, so Im saying actually the highest of bar 0 only. If I feed in the number 1 as my count, I'd then expect it to determine the highest out of bar 1 and 0.

to me the +1 seems to just say go back one more bar you were asking, one more that you don't want.
Confused :-)

 
If I not mistaken, +1 means you are including the zero bar.
 
idh: Im not sure I understand why.
HighestBarNumber = iHighest(NULL,0,MODE_HIGH,BarNumberOrderOpened+1,0);
How many numbers are in [2 3 4 5] (inclusive?) 5-2+1=4. [0 1] 1-0+1=2. [0] 0-0+1=1
int iLeft = BarNumberOrderOpened;
int iRight = 0;
int Count = iLeft - iRight + 1; // But iRight == 0
//  Count = iLeft          + 1; // Drop iRight
HighestBarNumber = iHighest(NULL,0,MODE_HIGH,count,iRight);
 
deysmacro:
If I not mistaken, +1 means you are including the zero bar.

ah ok it's the old array thing starting at zero :) but I'm still not sure why it didn't like start=0 and count = 0, I know that the function isnt meant for using it on one bar, but I thought it would be OK.


I.e. my bar opens on bar 0, and Im still on that same bar and I want to find the highest price since the trade open,.... +1 would seem to imply I want to look at bar 0 and bar 1, yet I was getting a bar 4's high price which seems arbitrary.

 
idh:

ah ok it's the old array thing starting at zero :) but I'm still not sure why it didn't like start=0 and count = 0, I know that the function isnt meant for using it on one bar, but I thought it would be OK.


I.e. my bar opens on bar 0, and Im still on that same bar and I want to find the highest price since the trade open,.... +1 would seem to imply I want to look at bar 0 and bar 1, yet I was getting a bar 4's high price which seems arbitrary.

Count parameter is a the number of bars. That makes no sense to search the highest 'something' on 0 bar.

By the way using 0 for this parameter, is like using WHOLE_ARRAY, as value of this constant is 0. So with your initial code you got the highest of the whole array, which explains your result.

 
angevoyageur:

Count parameter is a the number of bars. That makes no sense to search the highest 'something' on 0 bar.

By the way using 0 for this parameter, is like using WHOLE_ARRAY, as value of this constant is 0. So with your initial code you got the highest of the whole array, which explains your result.




aha! with you now. thanks for helping me out.
Reason: