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

- 2020.04.21
- www.mql5.com
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
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++.
- They are initialized once on program load.
- They don't update unless you assign to them.
- 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:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
- Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
external static variable - MQL4 programming forum

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use