# get highest and get lowest in range

3 replies

5 years ago #114474

Hi Mark and Team,

I’ve found a few strategies that look interesting that all rely on the HighestInRange and LowestInRange functions in SQ.  Looking at the MQ4 code generated we have two functions for this that take the start and end hour and minutes to return the highest price in this time range and the lowest price in this time range.

However it seems rather odd logic there.  Why 100 bars?  If I am on M1 timeframe that’s less than 2 hours to find a bar that covers a possible 24 hour period?

Also, it is very slow.  Why not use a combination of iBarShift, iHighest and iLowest to find the high and low between two times?

```double getHighestInRange(int fromHour, int fromMinute, int toHour, int toMinute) {
int indexTo = -1;
int indexFrom = -1;
int i;

// find index of bar for timeTo
for(i=1; i<=100; i++) {
if(timeIsBiggerOrEqual(Time[i], toHour, toMinute) && timeIsLower(Time[i+1], toHour, toMinute)) {
indexTo = i;
break;
}
}

if(indexTo == -1) {
return(-1);
}

// find index of bar for timeFrom
for(i=1; i<=100; i++) {
if(i <= indexTo) continue;

if(timeIsBiggerOrEqual(Time[i], fromHour, fromMinute) && timeIsLower(Time[i+1], fromHour, fromMinute)) {
indexFrom = i;
break;
}
}

if(indexFrom == -1) {
return(0);
}

double value = -10000.0;

for(i=indexTo; i<=indexFrom; i++) {
value = MathMax(value, iHigh(NULL, 0, i));
}

return(value);
}
```

I tried putting these and the other required functions into an indicator, but the results didn’t make any sense.

Bit confused as to if and how these are working.

Thanks,

Mike

5 years ago #134210

Digging a bit deeper I see the following problems:

1. In SQ, if the strategy uses the HighestInRange/LowestInRange, it takes 10x longer to process the strategy than one that doesn’t use this building block.
2. The exported strategy in MQL (for MetaTrader) doesn’t work in lower timeframes (e.g. M5) because it only searches 100 bars back in time.
3. If you change the code to have a bigger number than 100, it is very slow in MT4, in the backtester it crawls even on a very fast machine.

I can see why the code searches as it does (to cater for weekend gaps for example), but it is very inefficient.

I’m seeing if I can write a much faster version.

4 years ago #138963

No one else noticed how very slow SQ is if you select HighestInRange and/or LowestInRange?

For SQ4, I would suggest the output of these building blocks is calculated once at the start of the first run, with the results held in memory for each possible hour and minute for each bar.  Calculating them each iteration is very slow and RAM is cheap and fast.

4 years ago #138972

Hi, I think Marks reworked the way block code is implemented to have “plugin” feature available in the new version. I will check the performance with SQ4 when released

Viewing 4 posts - 1 through 4 (of 4 total)