Download MetaTrader 5
To add comments, please log in or register
Do you want to write a trading robot? Find solution in MQL5 Reference!
super_noob
38
super_noob 2014.03.14 04:40 

After recently joining and posting on the forums I was redirected to some excellent material which by and large helped me understand and make basic EA's. For the life of me though I can't understand why the close order portion of this EA isn't closing any positions. It is attached below.

//|---------close orders
   
   if(Hedge==false)
   {
      if((ReverseSystem==false&&HideSL&&StopLoss>0&&OrderType()==OP_BUY&&(OrderOpenPrice()-Ask)/Point>=StopLoss)||(ReverseSystem==false&&HideTP&&TakeProfit>0&&OrderType()==OP_BUY&&(Bid-OrderOpenPrice())/Point>=TakeProfit)||(ReverseSystem&&HideSL&&StopLoss>0&&OrderType()==OP_SELL&&(Bid-OrderOpenPrice())/Point>=StopLoss)||(ReverseSystem&&HideTP&&TakeProfit>0&&OrderType()==OP_SELL&&(OrderOpenPrice()-Ask)/Point>=TakeProfit))
      {
         if(ReverseSystem)CloseSellOrders(Magic);else CloseBuyOrders(Magic);
      }
      if((ReverseSystem==false&&HideSL&&StopLoss>0&&OrderType()==OP_SELL&&(Bid-OrderOpenPrice())/Point>=StopLoss)||(ReverseSystem==false&&HideTP&&TakeProfit>0&&OrderType()==OP_SELL&&(OrderOpenPrice()-Ask)/Point>=TakeProfit)||(ReverseSystem&&HideSL&&StopLoss>0&&OrderType()==OP_BUY&&(OrderOpenPrice()-Ask)/Point>=StopLoss)||(ReverseSystem&&HideTP&&TakeProfit>0&&OrderType()==OP_BUY&&(Bid-OrderOpenPrice())/Point>=TakeProfit))
      {
         if(ReverseSystem)CloseBuyOrders(Magic);else CloseSellOrders(Magic);
      }
   }
   else
   {
      if((HideSL&&StopLoss>0&&OrderType()==OP_BUY&&(OrderOpenPrice()-Ask)/Point>=StopLoss)||(HideTP&&TakeProfit>0&&OrderType()==OP_BUY&&(Bid-OrderOpenPrice())/Point>=TakeProfit))
      {
         CloseBuyOrders(Magic);
      }
      if((HideSL&&StopLoss>0&&OrderType()==OP_SELL&&(Bid-OrderOpenPrice())/Point>=StopLoss)||(HideTP&&TakeProfit>0&&OrderType()==OP_SELL&&(OrderOpenPrice()-Ask)/Point>=TakeProfit))
      {
         CloseSellOrders(Magic);
      }

Should I put something like this into it?

int start()
  {
//----
      string curr = Symbol();
      int ot = OrdersTotal();
      int ords[200], ordType[200], ordTicket[200]; double ordLots[200];
      string ordComments[200];
      int ix=0;
      for (int i=0; i<ot; i++)
      {
         int o = OrderSelect(i, SELECT_BY_POS);
         if (OrderSymbol() == Symbol())
            if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL))
            {
               ords[ix] = o; ordType[ix] = OrderType(); 
               ordLots[ix] = OrderLots(); ordTicket[ix] = OrderTicket();
               ordComments[ix] = OrderComment();
               ix++;
            }
      }
      
      for (i=0; i<ix; i++)
      {
         
         double ask = MarketInfo(Symbol(), MODE_ASK);
         double bid = MarketInfo(Symbol(), MODE_BID);
         double prc = 0;
         
         if (ordType[i] == OP_BUY) prc = bid; else prc=ask;
         OrderSelect(ords[i], SELECT_BY_POS);
         Print("Closing Order # ",ords[i]);
         OrderClose(ordTicket[i], ordLots[i],prc,5);
         Print("Error ",GetLastError());
      }
      
      for (i=0; i<ix; i++)
      {
          ask = MarketInfo(Symbol(), MODE_ASK);
          bid = MarketInfo(Symbol(), MODE_BID);
          prc = 0;
         
         int op = ordType[i]; if (op == OP_BUY) { op = OP_SELL; prc = bid;} else { op = OP_BUY; prc=ask;}
         OrderSend(Symbol(), op, ordLots[i], prc,5,0,0,ordComments[i]+"[Rev]"); 
      }
//----
   return(0);
  }

Any advice whether from someone or redirection to relevant material would be great.

qjol
3243
qjol 2014.03.14 04:47  

The order must be previously selected by the OrderSelect() function

The OrderSelect() function copies order data into program environment and all further calls of OrderClosePrice(), OrderCloseTime(), OrderComment(), OrderCommission(), OrderExpiration(), OrderLots(), OrderMagicNumber(), OrderOpenPrice(), OrderOpenTime(), OrderPrint(), OrderProfit(), OrderStopLoss(), OrderSwap(), OrderSymbol(), OrderTakeProfit(), OrderTicket(), OrderType() functions return the data, copied earlier. It means that in some cases the order details (open price, SL/TP levels or expiration date) may change and the data become non-actual. It is strongly recommended to call the OrderSelect() function before request the order data.

super_noob
38
super_noob 2014.03.14 05:14  

Thanks for your speedy response. Great advice. I see in the orderselect section I have a piece of code that looks like :

//|---------close buy orders

int CloseBuyOrders(int Magic)
{
  int result,total=OrdersTotal();

  for (int cnt=total-1;cnt>=0;cnt--)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol())
    {
      if(OrderType()==OP_BUY)
      {
        OrderClose(OrderTicket(),OrderLots(),Bid,3);
        switch(OrderType())
        {
          case OP_BUYLIMIT:
          case OP_BUYSTOP:
          result=OrderDelete(OrderTicket());
        }
      }
    }
  }
  return(0);

I'll delete the "result=OrderDelete" bit.

/
To add comments, please log in or register