Close previous orders conditionally on 1H and M5 charts

 

Hello Community,

What I need is basically the following:
1) If current ask goes above previous high, close all SELL orders. Only allowed to close on upcoming bars.
2) If current bid falls below previous low, close all BUY orders. Only allowed to close on upcoming bars.

I have tried closing these orders using the Hour() function as follows (on 1H chart) :

if(Hour() >= firstOrderHour + 1 || Hour() <= MathAbs(firstOrderHour - 23)){
        if(curAsk >= High[1])
           CloseAllType(OP_SELL);
        if(curBid <= Low[1])
           CloseAllType(OP_BUY);
}

Where curAsk is the current Ask price, curBid the current bid, firstOrderHour a static integer that marks the opening hour of my order, and CloseAllType() is a custom function that closes all orders given as parameter.

I've written a similar scrap for the M5 period. The problem is that sometimes, the order doesn't close unless a whole DAY passes. It turns out that if the succeeding bars don't fulfill the condition and the order was started at like 20:00 or so, it shall wait a whole day to test it again... ( if 21:00, 22:00, 23:00, 00:00  don't fulfill, then condition won't be true anymore for a whole day)
This is silly, I always struggle with the little things... What is the best way to fix this on both charts 1H and M5. Any help would be appreciated.

Thanks in advance,

Best Regards.

 
Lord Odin: What I need is basically the following:

1) If current ask goes above previous high, close all SELL orders. Only allowed to close on upcoming bars.
2) If current bid falls below previous low, close all BUY orders. Only allowed to close on upcoming bars.

Stop checking the time.

  1. Select the order
  2. Get the bar number of the OrderOpenTime (iBarShift)
  3. If it's not zero, it's the "upcoming bars." Now do your check.
 
William Roeder:

Stop checking the time.

  1. Select the order
  2. Get the bar number of the OrderOpenTime (iBarShift)
  3. If it's not zero, it's the "upcoming bars." Now do your check.

Yup. This is far better than the whole time thing. I will try it first thing tomorrow but it seems it will work out just fine. Thanks mate, cheers.

Best Regards.

Reason: