My open orders gets automatically closed, although no where in my code I call OrderClose(). Moreover, most of the time the transactions are unsuccessful giving error code 130.

 

extern int UPPER_LIMIT_ON_COUNTLOWHIGH=5,TOTAL_ORDERS=3;
extern double SL=200,VOLUME_LOT=0.01;
extern int SLIPPAGE=3,TP=2;

int countlowhigh=0;
double prevprice=0.0;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+

int start()
{
int ordercount=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
{
if(OrderType()<2) //if Market Order then
{
ordercount++;
}
}
}
Print("Countlowhigh = ",countlowhigh," Previous Price = ",prevprice," Bid Price = ", Bid," Ask Price ",Ask);
if(ordercount>TOTAL_ORDERS)
{
Alert("I cannot return from here, error is here ");
return (0);
}
if(NormalizeDouble(prevprice,Digits)<NormalizeDouble(Bid,Digits))
{
if(countlowhigh<0)countlowhigh=0;
countlowhigh++;
}
else if(NormalizeDouble(prevprice,Digits)>NormalizeDouble(Bid,Digits))
{
if(countlowhigh>0)countlowhigh=0;
countlowhigh--;
}
prevprice=Bid; //updating previous price value to the current one.
if(countlowhigh>=UPPER_LIMIT_ON_COUNTLOWHIGH)
{
countlowhigh=0;
Alert(" I was about to Buy ",Symbol()," ",OP_BUY," ",VOLUME_LOT," ",Ask," ",SLIPPAGE," ",Bid-SL*Point," ",Ask+(Ask-Bid)*TP);
int buyResponse=OrderSend(Symbol(),OP_BUY,VOLUME_LOT,Ask,SLIPPAGE,Bid-SL*Point,Bid+(Ask-Bid)*TP);
Alert("Order Reponse ",buyResponse," get last error ",GetLastError());
Sleep(3000);
}
else if(countlowhigh<=UPPER_LIMIT_ON_COUNTLOWHIGH*(-1))
{
countlowhigh=0;
Alert(" I was about to Sell ",Symbol()," ",OP_SELL," ",VOLUME_LOT," ",Bid," ",SLIPPAGE," ",Ask+SL*Point," ",Bid-(Ask-Bid)*TP);
int sellResponse=OrderSend(Symbol(),OP_SELL,VOLUME_LOT,Bid,SLIPPAGE,Ask+SL*Point,Ask-(Ask-Bid)*TP);
Alert("Order Reponse ",sellResponse," get last error ",GetLastError());
Sleep(3000);
}
return(0);
}
 
Here. Read the links on the first page. This question is asked a million times.
 

I just figured out something :

My open order closes automatically only when the price value reaches to one of the pending orders' open price value.

But how can my open order close automatically when it happens.

Moreover, my pending order even doesn't become market order, instead it just gets removed from pending orders list.

 
In the code you posted you don't have any Pending orders being placed . . .
 

  1. EA's must adjust TP, SL, AND slippage for 4/5 digit brokers. On ECN brokers you must open first and THEN set stops
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
         if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                    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(...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_TICKET))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket()...)
           Alert("OrderModify failed: ", GetLastError());
         */
    

  2. if(NormalizeDouble(prevprice,Digits)<NormalizeDouble(Bid,Digits))
    Bid, OpenOrderPrice() etc are always normal. It is NEVER necessary to use NormalizeDouble - EVER.