Why am I getting a 0 Divide error?

 
if(PositionsTotal()==0&&Signal=="Buy"&&Longs==true&&HourAfter==true)
{
   
double  Balance = AccountInfoDouble(ACCOUNT_BALANCE);
 double RiskBuy = NormalizeDouble((MathAbs(Ask - PriceInformation[2].low)*Multiplier),2);         //stop loss distance
   double AmountRisk = Balance * (0.02); //2% default   
   double BuyLotSize = MathRound(AmountRisk/RiskBuy);
      trade.Buy(BuyLotSize,NULL,Ask,NULL,Ask+((Ask-PriceInformation[2].low)+(Ask-PriceInformation[2].low)+(Ask-PriceInformation[2].low)),NULL);
}//Buy Condition
Why am I getting a 0 Divide error? I just want to risk 2% of my account size...
 
Theo324:
Why am I getting a 0 Divide error? I just want to risk 2% of my account size...

Your code looks OK. Did you check the Journal for the error line?

 
Theo324:
Why am I getting a 0 Divide error? I just want to risk 2% of my account size...
if(PositionsTotal()==0&&Signal=="Buy"&&Longs==true&&HourAfter==true)
{
   
double  Balance = AccountInfoDouble(ACCOUNT_BALANCE);
 double RiskBuy = NormalizeDouble((MathAbs(Ask - PriceInformation[2].low)*Multiplier),2);         //stop loss distance
   double AmountRisk = Balance * (0.02); //2% default   
   double BuyLotSize = MathRound(AmountRisk/RiskBuy);
      trade.Buy(BuyLotSize,NULL,Ask,NULL,Ask+((Ask-PriceInformation[2].low)+(Ask-PriceInformation[2].low)+(Ask-PriceInformation[2].low)),NULL);
}//Buy Condition

code marked yellow cause it

there are numerous different topics about this issue if you just search for it...

this is just one of them https://www.mql5.com/en/forum/338114

zero divide causing errors in EA
zero divide causing errors in EA
  • 2020.04.21
  • www.mql5.com
This is the code thats returning the zero divide problem . Which occurs when I change the lot size from MODE_LOTSIZE to MODE_MINLOT...
 
Theo324:
Why am I getting a 0 Divide error? I just want to risk 2% of my account size...
 static int price2low=PriceInfo[2].low

You must initialize a static variable with a constant.

Also, BuyLotSize doesn't give you what you want. Check it again.

 
Abdurrahman Kaya:

You must initialize a static variable with a constant.

Also, BuyLotSize doesn't give you what you want. Check it again.

Could you elaborate? 

 

I did figure it out though, by replacing the Ask price with PriceInformation[1].close (Since calculating onTick seems impossible for MQL5 when using the Ask price XD) , I just wish I could have it the way I want it calculated on the Ask though. Any advice?


 double RiskBuy = NormalizeDouble((MathAbs(Ask - PriceInformation[2].low)*Multiplier),2);         >>     double RiskBuy = NormalizeDouble((MathAbs(PriceInformation[1].close- PriceInformation[2].low)*Multiplier),2);         //stop loss distance
 
Theo324: Could you elaborate?
 static int price2low=PriceInfo[2].low

That is not an assignment; it's initialization of a static with a constant. Global and static variables 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 server related functions in OnInit (or on load,) 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. 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