How do I code a "market spent 30 minutes (time) at a price level buy now " alert ?

 

Good Day All

Please assist on the following, how do i write a code that will alert that " The Ask > 1.25654 and it has spent 30minutes trading at the level or not ".

I want to receive an alert if the market (ASK / BID)  is above a certain price level and has spent so much time at the level.

Thank you for your assistance.

 
Masa Nethononda:

Good Day All

Please assist on the following, how do i write a code that will alert that " The Ask > 1.25654 and it has spent 30minutes trading at the level or not ".

I want to receive an alert if the market (ASK / BID)  is above a certain price level and has spent so much time at the level.

Thank you for your assistance.


Look back or loop through the latest 30 1 minutes bar and if all closes of the latest 30 candles were above the specified price then return true . That's the easy way .
Hard way to store the bid and ask price for every tick in external file or DB and make the EA or indicator to read it at fixed intervals
 

Don't double post! You already had this thread open.
          General rules and best pratices of the Forum. - General - MQL5 programming forum 2017.07.19

 
William Roeder:

Don't double post! You already had this thread open.
          General rules and best pratices of the Forum. - General - MQL5 programming forum 2017.07.19


Apologies It wont happen again

 
Kareem Abdelhakim:

Look back or loop through the latest 30 1 minutes bar and if all closes of the latest 30 candles were above the specified price then return true . That's the easy way .
Hard way to store the bid and ask price for every tick in external file or DB and make the EA or indicator to read it at fixed intervals

Thank a lot. This is how I manage to do it. I am open for corrections and advise.

Files:
SNAP_3.JPG  34 kb
 
Masa Eudy Nethononda:

Thank a lot. This is how I manage to do it. I am open for corrections and advise.

Hello 

Actually this is not the most accurate way for making it to work . for many reasons

1- You didn't check check the closing price of each bar in the body of your loop instead you just made it to check the same bar 30 times :)  becuase you declared the PREVIOUS_CLOSE outside of the bool functions . So technically the code just compate the PREVIOUS_CLOSE against the aupPrice 30 times . which of course is not what you want . the correct way id to set the price inside of the loop body so it can change . this way

double prev_price= iClose(Symbol(),PERIOD_M1,
i );

2- you need to not return true from the loop once single bar closes above the specified level . because this way if only one candle closed above the the specified level the loop would break without continue to check the remaining bars . I suggest you to use while loop in this case . so the code will be like that


       double price = iClose(Symbol(),PERIOD_M1,31) ; // Arbitrary level 
      int i=30 ; // First candle index for last 30 minutes 
      bool isValid=true // Start with true
      while(isValid && i>=1) // Loop until this is not valid
        {
          isValid=iClose(Symbol(),PERIOD_M1,i)>=price ;
          i-- ;
        }
        
      if(isValid)
       {
         /// Do Something
       }
       else
        {
         // Do somethinf else
        }
 
Kareem Abdelhakim:

Hello 

Actually this is not the most accurate way for making it to work . for many reasons

1- You didn't check check the closing price of each bar in the body of your loop instead you just made it to check the same bar 30 times :)  becuase you declared the PREVIOUS_CLOSE outside of the bool functions . So technically the code just compate the PREVIOUS_CLOSE against the aupPrice 30 times . which of course is not what you want . the correct way id to set the price inside of the loop body so it can change . this way

2- you need to not return true from the loop once single bar closes above the specified level . because this way if only one candle closed above the the specified level the loop would break without continue to check the remaining bars . I suggest you to use while loop in this case . so the code will be like that


WOW !!!, Thank You Kareem, It is working Perfectly. Appreciate Your Help.

Reason: