Avoid not enough money when working with pending orders

 

Hello everyone,

I'm wrinting an EA  which has a strategy which open a lot of  pending orders. Before sending the command to open, I'm checking if there is enough money.

The problem  is when some pending orders are triggered there is not enough money, so the strategy tester delete them and close all the trades.

bool PlaceOrder(const int index)
  {
   double sl=0.0;
   double tp=0.0;
   
   double check_lot=0.0;
   int check_order_type=-1;
   double check_price=0.0;

   switch(SOrder[index].order_type)
     {
      case  OP_BUY:
         check_order_type=OP_BUY;
         break;
      case OP_SELL:
         check_order_type=OP_SELL;
         break;
      case OP_BUYLIMIT:
         check_order_type=OP_BUY;
         break;
      case OP_SELLLIMIT:
         check_order_type=OP_SELL;
         break;
      case OP_BUYSTOP:
         check_order_type=OP_BUY;
         break;
      case OP_SELLSTOP:
         check_order_type=OP_SELL;
         break;
      default:
         return(false);
         break;
     }
//--- 
   if(check_order_type==OP_BUY)
      check_price=Ask;
   else
      check_price=Bid;
//--- volume calculation
   double check_lot=LotCheck(CalculateLotSize_Margin());
//---
   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_LIMIT);
   if(max_volume>0.0)
     {
      double volume_buys        = 0.0;
      double volume_sells       = 0.0;
      double volume_buy_limits  = 0.0;
      double volume_sell_limits = 0.0;
      double volume_buy_stops   = 0.0;
      double volume_sell_stops  = 0.0;
      CalculateAllVolumes(volume_buys,volume_sells,
                          volume_buy_limits,volume_sell_limits,
                          volume_buy_stops,volume_sell_stops);
      if(volume_buys+volume_sells+
         volume_buy_limits+volume_sell_limits+
         volume_buy_stops+volume_sell_stops+check_lot>max_volume)
         return(false);
     }    
//--- check maximal number of allowed positions
   int account_limit_orders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);
   if(account_limit_orders>0)
     if(OrdersTotal()+1>account_limit_orders)
         return(false);
//--- check volume before OrderSend to avoid "not enough money" error           
   if(CheckMoneyForTrade(_Symbol,check_lot,check_order_type))
     {
      int ticket=OrderSend(Symbol(),SOrder[index].order_type,check_lot,NormalizePrice(check_price),InpSlippage,sl,tp,InpComment,InpMagic);
      if(ticket>0) 
        return(true);
      else 
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: failed to open order: ",ErrorDescription(GetLastError()) );
         return(false);
        }
     }  
//---
   return(false);       
  }


//+------------------------------------------------------------------+
//| CheckMoneyForTrade                                               |
//+------------------------------------------------------------------+
bool CheckMoneyForTrade(string symb, double lots,int type)
  {
   double free_margin=AccountFreeMarginCheck(symb,type, lots);
   //-- if there is not enough money
   if(free_margin<0)
     {
      string oper=(type==OP_BUY)? "Buy":"Sell";
      Print("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());
      return(false);
     }
   //--- checking successful
   return(true);
  }  


These are the kinds of message I've in logs:

2022.08.06 08:08:17.349 2022.01.19 12:29:28  Tester: PrevBalance: 500.00, PrevPL: -426.46, PrevEquity 73.54, PrevMargin: 288.54, NewMargin: 289, FreeMargin: -215.57
2022.08.06 08:08:17.349 2022.01.19 12:29:28  Tester: not enough money for buy 0.28 XAUUSD at 1818.29 sl: 0.00 tp: 0.00 [2022.01.19 12:29]

What others tests/checks I should do before opening a order or maybe I should perform other checkings ?

Can someone guide my thinking ?
 

Hi,

I had the same problem (many pending orders) and I solved it this way:

after a pending order is opened, I check the free margin per minute and if it falls below a limit, I close
all pending orders. If there is enough free margin again, the pending order could then be reopened.

You should also think about reducing the lot size so that the low margin situation is very rare.

Best regards

 
  1. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

  2. Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
Werner Klehr #:

Hi,

I had the same problem (many pending orders) and I solved it this way:

after a pending order is opened, I check the free margin per minute and if it falls below a limit, I close
all pending orders. If there is enough free margin again, the pending order could then be reopened.

You should also think about reducing the lot size so that the low margin situation is very rare.

Best regards

Hi,

Thank for your replys. It's a good idea.

You said "...  if it falls below a limit...". I think maybe if it's negative should be enough to delete the pending orders, right ?

 
William Roeder #:
  1. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

  2. Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

Thank you William

Yes you're right. Maybe I should use direct market orders since the begining, now doing this requires a lot of code rewrite. I  will test previous answer and take some time to rewrite the code.

Thank for your help

 
michel picard #:

Hi,

Thank for your replys. It's a good idea.

You said "...  if it falls below a limit...". I think maybe if it's negative should be enough to delete the pending orders, right ?

Hi michel,

Normally, when the EA runs on an account and the free margin becomes negative, your broker will close all your open positions.

Best regards

 
Your code
   switch(SOrder[index].order_type)
     {
      case  OP_BUY:
         check_order_type=OP_BUY;
         break;
      case OP_SELL:
         check_order_type=OP_SELL;
         break;
      case OP_BUYLIMIT:
         check_order_type=OP_BUY;
         break;
      case OP_SELLLIMIT:
         check_order_type=OP_SELL;
         break;
      case OP_BUYSTOP:
         check_order_type=OP_BUY;
         break;
      case OP_SELLSTOP:
         check_order_type=OP_SELL;
         break;
      default:
         return(false);
         break;
     }
Simplified
check_order_type=SOrder[index].order_type % 2;
Reason: