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);
}
//---------//
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);
}
//---------//
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.
That calculation is done BEFORE the result is passed to ValNonZero()
Just so you know, you may still get divide by zero problems if Eqty is 0.
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.
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...
- 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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
double Margin = AccountInfoDouble(ACCOUNT_MARGIN);
double MarginCheck = MarginFree / Margin;
When I use this I get a division by zero error.