problem with grid trading

 

Hi I create an EA using grid technic. the EA will open the first order according to a specific logic and then the grid technic will run after it. I have 2 problem that I faced:

first one is that the EA failed to safe last open order if price is below last open order for buy order or if price is above last open order for sell order,

second, it failed to open third order if there's 2 open order, the EA will stop at break statement even though there's still 2 open order.

I don't know if this 2 problem is connected or not but I can't find the problem myself. can anyone tell me where's the problem?

void gridTrading()
  {
   double   lastOPBuy   = 0,
            lastOPSell  = 0,
            lastLots    = 0;
   bool     lastIsBuy   = FALSE;

   RefreshRates();
  
   //find last open order for order buy and order sell
   for(int iCount = OrdersTotal()-1; iCount<=0; iCount--)
     {
      //Print("1");
      //if there's no open order, break loop. it is used to prevent EA open order without logic.
      if(!OrderSelect(iCount,SELECT_BY_POS,MODE_TRADES))
         break;
      //Print("2"); failed to print this statement for the second problem
      if(OrderSelect(iCount,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() &&
            OrderMagicNumber() == MagicNumber)
           {
            if(OrderType() == OP_BUY)
              {
               if(lastOPBuy==0)
                  lastOPBuy=OrderOpenPrice();
               if(lastOPBuy!=OrderOpenPrice())
                  lastOPBuy=OrderOpenPrice();
               lastIsBuy = TRUE;
               Print("last op buy: ",lastOPBuy);
              }

            if(OrderType() == OP_SELL)
              {
               if(lastOPSell==0)
                  lastOPSell=OrderOpenPrice();
               if(lastOPSell!=OrderOpenPrice())
                  lastOPSell=OrderOpenPrice();
               Print("last op sell: ",lastOPSell);
              }
           }
        }
      if(lastIsBuy)
        {
         if(Ask>=lastOPBuy+stepInPoint)
           {
            if(Ask!=OrderOpenPrice()) //open when there's no open order on that grid
               ticketBuy   = OrderSend(Symbol(),OP_BUY,FixedLotSize,Ask,slippage,0,Bid+spread+stepInPoint,"GRID -",MagicNumber);
           }
         if(Ask<=lastOPBuy-stepInPoint)
           {
            if(Ask!=OrderOpenPrice())//do not open when there's open order on that grid
               ticketBuy   = OrderSend(Symbol(),OP_BUY,FixedLotSize,Ask,slippage,0,Bid+spread+stepInPoint,"GRID -",MagicNumber);
           }
        }
      if(!lastIsBuy)
        {
         if(Bid<=lastOPSell-stepInPoint)
           {
            if(Bid!=OrderOpenPrice())//do not open when there's open order on that grid
               ticketSell  = OrderSend(Symbol(),OP_SELL,FixedLotSize,Bid,slippage,0,Bid-stepInPoint,"GRID -",MagicNumber);
           }
         if(Bid>=lastOPSell+stepInPoint)
           {
            if(Bid!=OrderOpenPrice())//do not open when there's open order on that grid
               ticketSell  = OrderSend(Symbol(),OP_SELL,FixedLotSize,Bid,slippage,0,Bid-stepInPoint,"GRID -",MagicNumber);
           }
        }
     }
  }

failed to open thi

 
  1. for(int iCount = OrdersTotal()-1; iCount<=0; iCount--)

    If you have two or more orders, your loop does nothing.

    If you have one or zero, you have an infinite loop.


  2.                lastIsBuy = TRUE;

    This will be set if you have any open buy orders.

  3.               if(lastOPBuy!=OrderOpenPrice())
                      lastOPBuy=OrderOpenPrice();
    Since it is unlikely that you will have two buy orders opened at the same exact price, lastOPBuy will be the open price of the earliest order, not the last.
 
William Roeder #If you have two or more orders, your loop does nothing.

Yes I fix this problem by changing the operation and addd break statement so that it will stop at the first buy or sell order.


A quick question, if the grid is 5 pips and I use Ask+5 pips for buy order, the price doesn't always filled at the exact price, the only way for it to be exact is using stop order. is this correct?

 
Luandre Ezra #: . is this correct?

Stop orders become market orders when hit. Never assume an exact price fill.

Reason: