Download MetaTrader 5

Preventing a trade from closing because it is in negative equity

To add comments, please log in or register
mop0
197
mop0  

So I have an active trade which was opened fine through the following code (ignore a, b, and c, because those conditions have been met)

int ticket;
double LongOpen, ShortOpen;
  
if (a == true && b == true && c == true)
   {
      ticket = OrderSend(Symbol(),OP_BUY,lots,Ask,500,Ask-SL,0,"Lcomment!",111,0,Green);
      LongOpen = OrderOpenPrice();
         if (ticket > 0)
            {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0);
    }  

and it is now time to close the trade according to the conditions d and e, except I don't want to close because the trade is in negative equity, so I added in

&& (iClose(NULL,0,0) > LongOpen)

to the 2nd line here of the closing part

int cnt; 
if (d == true && e == true && (iClose(NULL,0,0) > LongOpen) )
   {
      for (cnt = 0 ; cnt < OrdersTotal() ; cnt++)
         {
            OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
            if (OrderType() == OP_BUY)
               {
                  OrderClose(OrderTicket(),OrderLots(),Bid,30,Blue);
               }
         }
    }

Except it doesn't work, trades are still closing according to d and e, while ignoring the iClose bit. Can anybody see where I went wrong?

mop0
197
mop0  

I think I see now that I did not select that trade which just opened to assign a value to LongOpen...
Not sure how to select the last opened trade though...

darksideofthemoon
57
darksideofthemoon  

To select order, also assign ticket, i.e. LongTicket and LongOpen, then, select by ticket.

Also, when closing, count orders down vs up.

mop0
197
mop0  
2cent:
I'd say that your "iClose bit" is true for... losing order, i.e. try " ". To select order, assign ticket instead of price, i.e. LongTicket vs LongOpen, then, select by ticket.
Cheers. I tried the magic number as well ticket integer thing but eventually I managed it by putting the iClose bit into line 6 after the order was selected for closure.
if ( d == true && e == true )
   {
      for (cnt = 0 ; cnt < OrdersTotal() ; cnt++)
         {
            OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
            if (OrderType() == OP_BUY && iClose(NULL,0,0) > OrderOpenPrice())
               {
                  OrderClose(OrderTicket(),OrderLots(),Bid,30,Blue);
               }
         }
    }
darksideofthemoon
57
darksideofthemoon  

Yes, that's the way it works.

In the first example, once it entered the loop, it closed all orders regardless of profit. (And I made a mistake in my suggestion, I apologise.)

mop0
197
mop0  
2cent:


Also, when closing, count orders down vs up.


You mean like this?

for (cnt = OrdersTotal() ; cnt = 0 ; cnt--)
darksideofthemoon
57
darksideofthemoon  

Like so:

for(cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
mop0
197
mop0  
2cent:


Like so:


Cheers, I guess it does make sense to count down since it would always begin with the latest open order.
darksideofthemoon
57
darksideofthemoon  

Yes, otherwise it will skip orders.

To add comments, please log in or register