getting the highest bar

Am new to mql5 but so far i have been enjoy the ride, while try to get the highest price bar from the bar index of an open position,

```// getting the lowestbar and higestbar price after trade open

int open_price_time = (int)PositionGetInteger(POSITION_TIME);
int bar_index = iBarShift(_Symbol,PERIOD_CURRENT,open_price_time,false);
int bar_count = Bars(_Symbol,PERIOD_CURRENT,open_price_time,TimeCurrent());
int lowestbar = iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW,bar_count,bar_index);

int highestbar = iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,bar_count,bar_index);

// to get price of the lowest and highest bars

double lowestbar_price = iLow(_Symbol,PERIOD_CURRENT,lowestbar);
double highestbar_price = iHigh(_Symbol,PERIOD_CURRENT,highestbar);

Comment("lowest bar price:" + DoubleToString(lowestbar_price,_Digits),
"\nhigest bar price: " + DoubleToString(highestbar_price,_Digits),
"\nbar count: " + IntegerToString(bar_count,0),
"\nhighest bar index: " + IntegerToString(highestbar,0),
"\nopen price index:" + IntegerToString(bar_index,_Digits));
```

my problem is that the first candle is that the position is opened remains the highest bar, and the highest bar price, even if new bars move higher than its value, same is applied for the lowest bar. how do i solve this, that is, for it to scan through that current bar with index[0] to where the position was opened and return the index or the highest bar price.

thanks

```    int bar_index = iBarShift(_Symbol,PERIOD_CURRENT,open_price_time,false);
int bar_count = Bars(_Symbol,PERIOD_CURRENT,open_price_time,TimeCurrent());
int lowestbar = iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW,bar_count,bar_index);```

Suppose the position was opened ten (10) bars ago.

bar_index equals 10 as does bar_count. iLowest is looking at bars [10 … 19].

victor: my problem is that the first candle is that the position is opened remains the highest bar, and the highest bar price,

Why does that surprise you?

William Roeder #:

Suppose the position was opened ten (10) bars ago.

bar_index equals 10 as does bar_count. iLowest is looking at bars [10 … 19].

Why does that surprise you?

ok, but this is the problem, if position was opened 5 bars ago from index[0] to index[5](index 5 being the open position bar), if index [3] had the lowest price low, ilowest still return index [5] as the lowest bar

thanks I was able to solve it by removing the ibarshift function completely and using (0)
``` int open_price_time = (int)PositionGetInteger(POSITION_TIME);
int bar_count = Bars(_Symbol,PERIOD_CURRENT,open_price_time,TimeCurrent());
int lowestbar = iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW,bar_count,0);
int highestbar = iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,bar_count,0);

// to get price of the lowest and highest bars

double lowestbar_price = iLow(_Symbol,PERIOD_CURRENT,lowestbar);
double highestbar_price = iHigh(_Symbol,PERIOD_CURRENT,highestbar);```

used zero for the index number of the open position index, and removing ibarshift function completely

though I don't really know why this worked, any idea?

victor #: , if position was opened 5 bars ago from index[0] to index[5](index 5 being the open position bar), if index [3] had the lowest price low, ilowest still return index [5] as the lowest bar

Your lowest function is not looking at bar 3; only the open bar to the past.

victor #: though I don't really know why this worked, any idea?

Now you are looking from the open bar to current.

William Roeder #:

Your lowest function is not looking at bar 3; only the open bar to the past.

Now you are looking from the open bar to current.

alright, thanks
Reason: