Not enough money calculation

 

Just a quick question for an EA,

I know about the error "not enough money" that sometimes pops-up when trying to place a trade above certain limits.

My question would be, is it any (maybe easy) formula to calculate the trade parameters so it will avoid, or at least alert the user, that the trade will be rejected for "not enough money"?

Of course, when I ask about a formula, I am thinking about something that could potentially be implemented on an existing EA.

 
Pedro Sanchez:

Just a quick question for an EA,

I know about the error "not enough money" that sometimes pops-up when trying to place a trade above certain limits.

My question would be, is it any (maybe easy) formula to calculate the trade parameters so it will avoid, or at least alert the user, that the trade will be rejected for "not enough money"?

Of course, when I ask about a formula, I am thinking about something that could potentially be implemented on an existing EA.


It means that you are not calculating your margin requirements correctly, meaning your volume (lots), is too high for the available balance.

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 

ok, i see thanks for the link info!

Just one doubt I get when I see the function `CheckMoneyForTrade` that they propose there...maybe you can help me understand...

I see the signature of the function, and the calculations and so on, and all looks ok for me, is only that, shouldn't the stop loss be considered too somehow?

Maybe I am too wrong in my thinking here. 

 
Pedro Sanchez #:

ok, i see thanks for the link info!

Just one doubt I get when I see the function `CheckMoneyForTrade` that they propose there...maybe you can help me understand...

I see the signature of the function, and the calculations and so on, and all looks ok for me, is only that, shouldn't the stop loss be considered too somehow?

Maybe I am too wrong in my thinking here. 

No (and yes)! The stop-loss is part of your risk calculations, not your margin calculations, because stop-loss does not affect the margin requirements, but it does affect your risk or potencial exposure.

When placing an order you should consider both calculations, and use the volume (lots) that satisfies both conditions (margin and risk).

 

Hi

You can try this function – when you have your lot calculated just check if it will pass the “enoughmoney” requirement. You can add alert instead of only printing out the message. Hope this helps.

bool CheckMoneyForTrade(string symb,double lots,ENUM_ORDER_TYPE type)

  {

//--- Getting the opening price

   MqlTick mqltick;

   SymbolInfoTick(symb,mqltick);

   double price=mqltick.ask;

   if(type==ORDER_TYPE_SELL)

      price=mqltick.bid;

//--- values of the required and free margin

   double margin,free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);

   //--- call of the checking function

   if(!OrderCalcMargin(type,symb,lots,price,margin))

     {

      //--- something went wrong, report and return false

      Print("Error in ",__FUNCTION__," code=",GetLastError());

      return(false);

     }

   //--- if there are insufficient funds to perform the operation

   if(margin>free_margin)

     {

      //--- report the error and return false

      Print("Not enough money for ",EnumToString(type)," ",lots," ",symb," Error code=",GetLastError());

      return(false);

     }

//--- checking successful

   return(true);

  }

Have a nice day👍📊