How To Close All Open Orders at the touch of the MA Not after the candle close?

 

Hello Fellow Traders,

It's have been a week since i started learning how to program MQL4 

And i Have A problem , The EA I am working on opens a lot of orders and i want all these orders to be closed at the touch of the ma

let's say i have opened 7 buy orders above 21 MA and i want to close them if the price "touched " the MA not after the Candle close under the MA, if that makes sense.


that is the code i am using to close all orders after the touch but it closes all the orders after the candle closes below the MA

double currentSma = iMA(NULL,0,21,0,MODE_SMA,PRICE_CLOSE,0);
if(MarketInfo(Symbol(),MODE_BID) <= currentSma){
   
   for (int i=cnt-1; i>=0; i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
 
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
 
    if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage);
    
  }
   
  }

thanks traders

 
AhmedMorra: i want to close them if the price "touched " the MA not after the Candle close under the MA,
  1. if(MarketInfo(Symbol(),MODE_BID) <= sma)
    Why use a function call instead of just Bid?
  2. That tests if the market is below sma. Nothing to do with touched.

  3. bool hasTouched = High[0] > sma && sma > Low[0];
  4. You don't have to use MI in the close. You can use OrderClosePrice if you RefreshRates after each close.
  5. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 
William Roeder:
  1. Why use a function call instead of just Bid?
  2. That tests if the market is below sma. Nothing to do with touched.

  3. You don't have to use MI in the close. You can use OrderClosePrice if you RefreshRates after each close.
  4. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

Thanks. your suggestions helped me find out what was wrong with my code.

When you say "Touch" the price,strategy tester should be on "Every tick", and I learned it the hard way.