Restrict number of open trades by type

jamrdavi
22
jamrdavi  
I am trying to get this code to work in a way that it will allow only one buy and one sell open at any time. I can not get it to acknowledge the script to limit the numbers. I was able to get a small code to restrict to one open trade in total to be allowed. But I am wanting to allow one sell trade be placed if there is already one buy trade open, and the vice versa.


This is the orders to place the sell or buy orders:

int sellOrder(ENUM_ORDER_TYPE orderType,double stopLoss, double takeProfit)
{
   double ATR = iATR(Symbol(),Period(),ATRPeriod,1);
         
   double SL  = ATR * ATRMultiplier;
   double TP;
   
   double priceAsk = MarketInfo(Symbol(), MODE_ASK);

   int    total = OrdersTotal();
   int    ticket;
   double openPrice;
   
   //Take Profit and Stop Loss
   if(!OrderExist())
   {
      if(orderType == ORDER_TYPE_SELL)
      {
         openPrice   = NormalizeDouble(Bid, Digits());
         stopLoss    = Ask + SL;
         TP          = (stopLoss - openPrice) * RiskFactor;
         takeProfit  = openPrice - TP;
      }
      else
      {
         GetLastError();
         return(0);
      }
      ticket = OrderSend(Symbol(), orderType, LotSize, openPrice, 0, stopLoss, takeProfit, TradeComment, MagicNumber, 0, clrNONE);
//      if(ticket < 0){}
      return(ticket > 0);
   
   }
      return(0);
}

int buyOrder(ENUM_ORDER_TYPE orderType,double stopLoss, double takeProfit)
{
   if(OrderExist() > 0) return(1);
   double ATR = iATR(Symbol(),Period(),ATRPeriod,1);
         
   double SL  = ATR * ATRMultiplier;
   double TP;
   
   double priceBid = MarketInfo(Symbol(), MODE_BID);   

   int    total = OrdersTotal();
   int    ticket;
   double openPrice;
   
   //Take Profit and Stop Loss

   if(!OrderExist())
   {
      if(orderType  == ORDER_TYPE_BUY)
      {
         openPrice   = NormalizeDouble( Ask, Digits());
         stopLoss    = Bid - SL;
         TP          = (openPrice - stopLoss) * RiskFactor;
         takeProfit  = TP + openPrice;

      }
      else
      {
         GetLastError();
         return(0);
      }
      ticket = OrderSend(Symbol(), orderType, LotSize, openPrice, 0, stopLoss, takeProfit, TradeComment, MagicNumber, 0, clrNONE);
//      if(ticket < 0){}
      return(ticket > 0);
   
   }
      return(0);
}

This is the code I am trying to use to limit the number orders by type:

int OrderExist()
{
   for(int openOrder = 0; openOrder < OrdersTotal(); openOrder++)
   {
      if(OrderSelect(openOrder, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            if(OrderType() > OP_BUY)
            {
               return(1);
            }
            else
            if(OrderType() > OP_SELL)
            {
               return(1);
            }
         }      
      }
   }
   GetLastError();
   return(0);
}


Please HELP!! I have been unable to determine the problem.

Keith Watford
Moderator
31752
Keith Watford  
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
jamrdavi
22
jamrdavi  
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
My apologies, thank you.
Keith Watford
Moderator
31752
Keith Watford  
int OrderExist()
{
   for(int openOrder = 0; openOrder < OrdersTotal(); openOrder++)
   {
      if(OrderSelect(openOrder, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            if(OrderType() > OP_BUY)
            {
               return(1);
            }
            else
            if(OrderType() > OP_SELL)
            {
               return(1);
            }
         }      
      }
   }
   GetLastError();
   return(0);
}
if(OrderType() > OP_BUY)
if(OrderType() > OP_SELL)

Why are you using > ?

Why are you calling GetLastError()?

Why is your function an int when it is being called as a bool?

if(!OrderExist())
Keith Watford
Moderator
31752
Keith Watford  
int sellOrder(ENUM_ORDER_TYPE orderType,double stopLoss, double takeProfit)
if(orderType == ORDER_TYPE_SELL)

Why are you using MQL5 enums in MQL4?

return(ticket > 0);

Why are you returning a bool from an integer function?

jamrdavi
22
jamrdavi  
Keith Watford:

Why are you using MQL5 enums in MQL4?

Why are you returning a bool from an integer function?

Because they show to be usable in MQL4 reference documentation.
jamrdavi
22
jamrdavi  
Keith Watford:

Why are you using > ?

Why are you calling GetLastError()?

Why is your function an int when it is being called as a bool?

It was the last way I tried to make it work. I have tried it as a bool with no luck. I am checking to see if there is a current open order of that type, being greater than 0. I used GetLastError() as a filler basically. Without, it kept saying something was needing to be checked.