Grid EA problem

 

Hello,

I am trying to code a very simple EA.

The EA should do the follwing:

The variable BasicGridLine is the Basic Grid line ;)
Variable grid_separation is the distance to the next grid line, again and again....

If the ask price match a grid line, a tradeshould be open with X Pips Take Profit of the Variable TP. No SL will be set.


I tried already a Grid EA, but this one opens not always a trade. Sometimes it opens, a trade, sometimes not.

For example, if the prioce is 1.0000 and evey 10 pips distance it shopuld open a trade. So 1.0010 1.0020 1.0030 it should open a new trade. If at 1.0030 it opens a trade and it goes back to 1.0020 is should take profit and if the proice turn again, at 1.0030 should be a new trade with take profit 1.0020. And if at 1.0030 it opens a trade with tp 1.0020 but the price goes only to 1.0025 and then to 1.0030 again, it should not open again a 2nd trade on that price. So it should be be only one trade at a specific price be open.


I attached an EA which is doing what I like, but if a trade is closed, it will not open again. I wonder why?

If I trade this EA by Hand, it is very successfull. But there is no time to even to drink a coffee, because you will miss the next entry.

Files:
gridnea.mq4  5 kb
 

u should find in the history if a trade as been closed

for (int x = 0; x <= OrdersHistoryTotal(); x++)
   {
   if (you found a closed order)
      {
      ................
      }
   }
 
  1.    if(OrdersTotal()==0)
    
    This makes the EA incompatable with every other including itself on other charts.
    int count = 0;
        else for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if( // Open orders
            OrderSelect(iPos, SELECT_BY_POS)                // Only my orders w/
        &&  OrderMagicNumber()  == Magic.Number             // my magic number
        &&  OrderSymbol()       == chart.symbol             // and my pair.
        ){ count++; }
    if (count ...
    

  2.       for (int i = 1; i<max_count_of_all_gridLines; i++)
           {
           if (Ask == BasicGridLine - grid_separation*i)
           {
           enter_trade_SELL=1 ;
          }
         if (Bid == BasicGridLine + grid_separation*i)
         {
          enter_trade_SELL=1 ; 
          Print(i);
         }
        }
    
    Doubles will rairly compare equal. so your IFs will usually fail. There is one grid, you should be comparing Bid for both directions. No enter_trade_BUY. Try:
          for (int i = max_count_of_all_gridLines; i >= 1; i++){
            if (Bid <= BasicGridLine - grid_separation*i){
               enter_trade_SELL=1; break;
            }
            if (Bid >= BasicGridLine + grid_separation*i){
               enter_trade_BUY=1;  break;
            }
          }
    

  3.    double Ticket1 = OrderSend(Symbol(), OP_BUY, Lots, Ask, slip, 0, Ask+TP*Point, 
                                         "Buy(#" + 1 + ")", 1, 0, DodgerBlue) ; // Trade oeffnen
    
    TP=11. EA will fail on a 5 digit broker. Fails on a ECN broker. Always test return codes.
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
         if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    //---- These are adjusted for 5 digit brokers.
        /* On ECN brokers you must open first and THEN set stops
        int ticket = OrderSend(..., 0,0,...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_TICKET))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
           Alert("OrderModify failed: ", GetLastError());
         */
    

  4. Check out snowballs and the anti-grid - 7bit