Please help!! Can't close orders

 

I am new to MQL4 and have been trying to solve the following problem all day. Currently the code below will buy when the indicator raises up above -40 from below. However, I cannot get it to close that same order when it raises above +40. (The same is true of the sell signal in the opposite direction)


extern int StopLoss=30;
extern int Upper = 40;
extern int Lower = -40; 
extern double LotSize = 0.01;
extern int MagicNumber = 1234;
bool Work=true;

int start()
{
 double CurrentInd=iCustom("EURUSD",1,"Indicator",1,0);
 double PreviousInd=iCustom("EURUSD",1,"Indicator",1,1);
  
   if (PreviousInd<Lower && CurrentInd>Lower && OrdersTotal () == 0)
           {
         OrderSend (Symbol (), OP_BUY, LotSize, Ask, 3, Ask - (StopLoss*0.00001), NULL, NULL, MagicNumber);
           };

   if (OrdersTotal () == 1 && OrderType ()==OP_BUY && PreviousInd<Upper && CurrentInd>Upper)
{
OrderClose(MagicNumber, LotSize, Bid,10);
};

 

  
   if (PreviousInd>Upper && CurrentInd<Upper && OrdersTotal () == 0)
         {
       OrderSend (Symbol (), OP_SELL, LotSize, Bid, 3, Bid + (StopLoss*0.00001), NULL, NULL, MagicNumber);
          };

  if (OrdersTotal () ==1 && OrderType ()==OP_SELL && PreviousInd>Lower && CurrentInd<Lower)
{
OrderClose(MagicNumber, LotSize, Ask,10);
}; 
}


 
Look up the help file for OrderClose(), you cannot close an order with the magic number.
 
GumRai:
Look up the help file for OrderClose(), you cannot close an order with the magic number.

 Thanks GumRai. I've changed it to the below but still having problems

 if (PreviousInd<Lower && CurrentInd>Lower && OrdersTotal () == 0)
           {
        int ticket;
         ticket = OrderSend (Symbol (), OP_BUY, LotSize, Ask, 3, Ask - (StopLoss*0.00001), NULL);
           };

if (OrdersTotal () == 1 && OrderType ()==OP_BUY && PreviousInd<Upper && CurrentInd>Upper)
   {
OrderClose(ticket, LotSize, Bid,10);
};
 
if (OrdersTotal () == 1 && OrderType ()==OP_BUY && PreviousInd<Upper && CurrentInd>Upper)

You have to select an order before you can access OrderType()