Reply

Possible Coding To Allow Open of Multiple Trades to Go Beyond Max Lot Size

0 replies

jbrunet

Subscriber, bbp_participant, community, 13 replies.

Visit profile

10 years ago #111607

Most brokers have a limit to the number of lots they will allow you to open. In some cases, it is desirable to open up multiple trades to achieve a number of open lots that is beyond the broker’s max for any one trade ticket. I have done some experimenting, and have found that the following modification to the sqOpenOrderWithErrorHandling() function will allow this to happen. I am offering this as a suggestion to be included in future versions, if it is deemed desirable by the developers and other users to do so. If anyone wishes to try this, you are free to give it a try, but be sure to test it thoroughly to be sure it does not break anything else. My own personal testing has found (so far) that it does not break other functions and so I have been using it with good success.

 

Please note that instead of LotsDecimals, which is defined as double by EA Wizard, I usually use an int variable, decimals, which is a global int variable and is defined as the number of decimal places allowed for lot sizing by that particular broker. In most cases, it is 2, for brokers that allow micro lots. I have used LotsDecimals in my custom coding below, since that is the norm for EA Wizard, but in my own practice, I use the int variable decimals instead.

 

int sqOpenOrderWithErrorHandling(string symbol, int orderType, double orderLots, double price, double realSL, double realPT, string comment, int orderMagicNumber) {
   int ticket, error;
 
// *****  Custom Added Code
  double mmaxlots=MarketInfo(Symbol(), MODE_MAXLOT) ;
  double mminlots=MarketInfo(Symbol(), MODE_MINLOT) ;
 
  double jlots=orderLots ;     
  double ilots=0.0 ;
  while(jlots>0.0) 
    {
        ilots=jlots ;
        if(ilots>mmaxlots) ilots=mmaxlots ;
        if(ilots<mminlots) ilots=mminlots ;
        ilots=NormalizeDouble(ilots,LotsDecimals) ;
        jlots=jlots-ilots ;
      
 
 
   if(symbol == “NULL”) {
      ticket = OrderSend(Symbol(), orderType, ilots, price, MaxSlippage, 0, 0, comment, orderMagicNumber);
   } else {
      ticket = OrderSend(symbol, orderType, ilots, price, MaxSlippage, 0, 0, comment, orderMagicNumber);
   }
// **** End of Custom Added Code for this section…See additional code at end of function.
 

 

   if(ticket < 0) {
      // order failed, write error to log
      return(-1*GetLastError());
   }
 
   OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
   Log(“Order opened: “, OrderTicket(), ” at price:”, OrderOpenPrice());
 
   // set up stop loss and profit target
   // it has to be done separately to support ECN brokers
   if((realSL > 0 || realPT > 0) && (OrderStopLoss() != realSL || OrderTakeProfit() != realPT)) {
      if(OrderModify(ticket, OrderOpenPrice(), realSL, realPT, 0, 0)) {
         Log(“Order modified, StopLoss: “, realSL,”, Profit Target: “, realPT);
      } else {
         error = GetLastError();
         Log(“Error modifying order: “,error, ” : “, ErrorDescription(error));
 
         RefreshRates();
         if(orderType == OP_BUY) price = Ask;
         if(orderType == OP_SELL) price = Bid;
         if(OrderClose(ticket, orderLots, price, 6) != true) {
            error = GetLastError();
            Log(“CANNOT CLOSE ORDER: “, OrderTicket(), “, Error opening order : “,error, ” : “, ErrorDescription(error));
            return(-1*error);
         }
      }
   }
 
// ***** Additional Custom Added Code

 

  if(jlots>0.0) continue ;
 }  
 
// **** End of Custom Added Code for this section

 

 
  return(ticket);
}

0