ERROR 4105

 

Hi

i'm trying to build an hedging EA (to learn mql4).

Here's the code

#define MAGICMA  20141405
//--- Inputs
input double Lots     = 1;
input int    sl       = 5;
input int    tp       = 10;



float NetLots(string symbol)
  {
      if (OrdersTotal() > 0) {
         double shorts = 0; double longs = 0;
         int i = 0;
         for(i = 0; i <= OrdersTotal(); i++) {
            OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
            if(OrderType()==OP_BUY) longs += OrderLots();
            if(OrderType()==OP_SELL) shorts += OrderLots();
         }
         return(longs - shorts);
      }
  }
float NetProfit(string symbol)
  {
      if (OrdersTotal() > 0) {
         double profit = 0;
         int i = 0;
         for(i = 0; i <= OrdersTotal(); i++) {
            OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
            profit += OrderProfit();
            
         }
         return(profit);
      }
  }
 void CloseAll(){
   for(int i=OrdersTotal()-1;i>=0;i--) {
      OrderSelect(i, SELECT_BY_POS);
      int type   = OrderType();
      bool result = false;
      switch(type) {
         //Close opened long positions
         case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
      
         //Close opened short positions
         case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                          break;

         //Close pending orders
         case OP_BUYLIMIT  :
         case OP_BUYSTOP   :
         case OP_SELLLIMIT :
         case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
      }
   }
} 

void OnTick() {
   //--- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false)
      return;
   //--- Comments
   Comment("NetLots: ", NetLots(Symbol()), "\n", 
           "NetProfit: ", NetProfit(Symbol()), "\n", 
           "NetProfit < sl?: ", NetProfit(Symbol()) <= 0-sl, "\n", 
           "LastError: ", GetLastError(), "\n",
           "OrderSelect:", OrderSelect(0, SELECT_BY_POS, MODE_TRADES)==true);
   // -- First order always Long
   if (OrdersTotal() == 0) OrderSend(Symbol(), OP_BUY, Lots, Ask, 0, 0, 0, "", MAGICMA, 0, Red);
   // -- If NetProfits < SL then hedge
   if (NetProfit(Symbol()) <= 0-sl) {
      if(NetLots(Symbol()) < 0) OrderSend(Symbol(),OP_BUY, (0-NetLots(Symbol()))*2, Ask, 0, 0, 0,"",MAGICMA,0,Red);
      if(NetLots(Symbol()) > 0) OrderSend(Symbol(),OP_SELL, (0-NetLots(Symbol()))*2, Bid, 0, 0,0,"",MAGICMA,0,Red);
   } 
   // -- If NetProfits > tp then closeAll
   if (NetProfit(Symbol()) >= tp) CloseAll();
 }
//+------------------------------------------------------------------+

When it come to hedge he doesn't open the reverse order, and I receive the error 4105 (ERR_NO_ORDER_SELECTED).

Any idea on how to resolve this problem?

Thanks

 
(MathAbs(0-NetLots(Symbol()))*2)

you can't send negative numbers !!!

one more thing

according to NFA rules: you can't hedge if you're an american citizen or using a USA broker

Reason: