[MQL4 CODING] Function to close Losing trade when it reached X distance

shanmugapradeep

Active Trader
Dec 18, 2020
119
5
34
39
Hello,

I am making a code,

If Buy Order reach above 30 Point from Entry price, close Sell Order
If Sell Order reach below 30 Point from Entry Price, Close Buy Order.

MQL4:
int gRangeClosing_Automatic = 30;
 
if((LastOrderPrice(OP_SELL, gSellMagic) - Ask) / Point > gRangeClosing_Automatic)
     {
      CloseOpenAndPendingTrades(gBuyMagic);
      Print("Range Closing => Buy Order Closed");
     }
   if((Bid - LastOrderPrice(OP_BUY, gBuyMagic)) / Point > gRangeClosing_Automatic)
     {
      CloseOpenAndPendingTrades(gSellMagic);
      Print("Range Closing => Sell Order Closed");
     }
 
double LastOrderPrice(int LastOrderPrice_OrderType, int LastOrderPrice_magic)
  {
   double LastOrderPrice_Price = 0; // Initialize to some default value
   for(int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++)
     {
      OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != LastOrderPrice_magic)
         continue;
      if(OrderType() == LastOrderPrice_OrderType)
         LastOrderPrice_Price = OrderOpenPrice();
     }
   return LastOrderPrice_Price;
  }

Problem :

Both condition some how getting true and it closing both Buy and Sell order at same time
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,098
1,481
144
Odesa
www.earnforex.com
It's best to print everything along the way - order's ticket numbers, price difference, and so on. It will be easier to spot the issue.
One issue that I see is that LastOrderPrice() will return 0 in case it doesn't find any Buy order with the given Magic number. And this condition will automatically become true:
MQL4:
if((Bid - LastOrderPrice(OP_BUY, gBuyMagic)) / Point > gRangeClosing_Automatic)
This means that when you have your Sell condition triggering, it will close your Buy order. It will then automatically trigger the condition to close the Sell order because you no longer have a Buy and LastOrderPrice() will return 0.
 

shanmugapradeep

Active Trader
Dec 18, 2020
119
5
34
39
will return 0 in case it doesn't find any Buy order with the given Magic number.
With this old code, if Market move up from Buy entry price, it perfectly close the trade but when Market move down from Sell entry price, it is closing both the trade.

I made some changes and currently testing (New Code) :

MQL4:
  if(LastOrderPrice(OP_BUY, gBuyMagic) > 0 && trade_count_ordertype(OP_SELL, gSellMagic) == 1 && Bid - LastOrderPrice(OP_BUY, gBuyMagic) > gRangeClosing_Automatic * Point)
     {
      CloseOpenAndPendingTrades(gSellMagic);
      Print("Range Closing => Sell Order Closed");
     }
   if(LastOrderPrice(OP_SELL, gSellMagic) > 0 && trade_count_ordertype(OP_BUY, gBuyMagic) == 1 && LastOrderPrice(OP_SELL, gSellMagic) - Ask > gRangeClosing_Automatic * Point)
     {
      CloseOpenAndPendingTrades(gBuyMagic);
      Print("Range Closing => Buy Order Closed");
     }
 
double LastOrderPrice(int LastOrderPrice_OrderType, int LastOrderPrice_magic)
  {
   double LastOrderPrice_Price = 0; // Initialize to some default value
   for(int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++)
     {
      OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != LastOrderPrice_magic)
         continue;
      if(OrderType() == LastOrderPrice_OrderType)
         LastOrderPrice_Price = OrderOpenPrice();
     }
   return LastOrderPrice_Price;
  }
 
int trade_count_ordertype(int trade_count_ordertype_value, int trade_count_ordertype_magic)
  {
   int count_4 = 0;
   for(int pos_8 = 0; pos_8 < OrdersTotal(); pos_8++)
     {
      OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != trade_count_ordertype_magic)
         continue;
      if(trade_count_ordertype_value == OrderType())
         count_4++;
     }
   return count_4;
  }

What you suggest?