"Zero divide error" with the new broker.

 

Hello,

For some time now, I've been using an EA with a trusted broker, and it's been working without any issues. Recently, I opened an account with a new broker, and today the EA was running on both accounts. On the previous broker's account, it opened a position, but on the new broker's account, an error occurred: "zero divide error." I'd like to add that everything works correctly on the strategy tester of the new broker.

The "Experts" tab points to an error in the line: 

double LOT = NormalizeDouble((BALANCE * RISK) / (stopLossDiff / getPipsValue() * nTickValue), 2);

where:

double BALANCE = 10000;

double RISK = 0.02;

double stopLossDiff = Ask - iLow(_Symbol, PERIOD_M15, 1);

double nTickValue = MarketInfo(Symbol(), MODE_TICKVALUE);

if ((Digits == 3) || (Digits == 5) || (Digits == 2))
{ 
	nTickValue = nTickValue * 10; 
}


double getPipsValue() { 
	if (Digits < 3) { return 0.1; } 
	else if (Digits == 3) { return 0.01; } 
	else if (Digits >= 4) { return 0.0001; } 
	else { return 0; } 
}


How to solve this problem?

 
Print or Comment() the parameters of stopLossDiff , getPipsValue(), nTickValue right before that line or use the debugger: https://www.metatrader5.com/en/metaeditor/help/development/debug.
Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
double stopLossDiff = Ask - iLow(_Symbol, PERIOD_M15, 1);

double nTickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
  1. Your posted code is without context. Are those lines in a function or global?

    Always post all relevant code (using Code button) or attach the source file.

  2. If global, those are not assignments; they are initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)