Get account info

 
     double MarginFree =  AccountInfoDouble(ACCOUNT_MARGIN_FREE);
     double Margin =  AccountInfoDouble(ACCOUNT_MARGIN);

     double MarginCheck = MarginFree / Margin;


When I use this I get a division by zero error.

 
Tony Chavez:
double MarginFree =  AccountInfoDouble(ACCOUNT_MARGIN_FREE);
     double Margin =  AccountInfoDouble(ACCOUNT_MARGIN);

     double MarginCheck = MarginFree / Margin;


When I use this I get a division by zero error.


   double MarginFree  =  AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   double Margin      =  AccountInfoDouble(ACCOUNT_MARGIN);
   double MarginCheck =  ValNotZero(MarginFree/Margin);
   //--
   double ValNotZero(double not_zero) // function to return value not zero
    {
      if(not_zero<=0) return(NormalizeDouble(1/100000,_Digits));
      else return(not_zero);
    }
   //---------//
 
Roberto Jacobs:

   double MarginFree  =  AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   double Margin      =  AccountInfoDouble(ACCOUNT_MARGIN);
   double MarginCheck =  ValNotZero(MarginFree/Margin);
   //--
   double ValNotZero(double not_zero) // function to return value not zero
    {
      if(not_zero<=0) return(NormalizeDouble(1/100000,_Digits));
      else return(not_zero);
    }
   //---------//
I still get error
 
The purpose if this is so I can test if the margin free is below 40% and pause making trades until is goes above 40%
 
   double MarginFree =  AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   double Margin =  AccountInfoDouble(ACCOUNT_MARGIN);
   double MarginCheck = (Margin == 0) ? 0 : MarginFree / Margin;
 

I ended up doing:


double MarginFree =  AccountInfoDouble(ACCOUNT_MARGIN_FREE);
double Eqty =  AccountInfoDouble(ACCOUNT_EQUITY);
double MarginCheck = ValNonZero(MarginFree/Eqty);

 

Just so you know, you may still get divide by zero problems if Eqty is 0.

double MarginCheck = ValNonZero(MarginFree/Eqty);

That calculation is done BEFORE the result is passed to ValNonZero() 

 
honest_knave:

Just so you know, you may still get divide by zero problems if Eqty is 0.

double MarginCheck = ValNonZero(MarginFree/Eqty);

That calculation is done BEFORE the result is passed to ValNonZero() 

That is fine because for Eqty to be at zero you would have to bankrupt the account.
 
Tony Chavez:
That is fine because for Eqty to be at zero you would have to bankrupt the account.

Well, not really.

Have you checked what AccountInfoDouble() returns when you aren't connected to an account? It returns 0

So if you aren't connected to your account before this code runs, you will get a zero divide error. Like when you restart the terminal.

This is probably what was happening with your initial ACCOUNT_MARGIN check giving zero divide errors. 

Also, there is no point in ValNonZero() if you are already dividing by 0... 

 
No need for any of that, just avoid stop out
  • 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.
  • Account Balance * 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.)
  • Do NOT use TickValue by itself - DeltaPerLot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out