# Getting an error on ticksize

33

I'm getting an error, can't divide by 0 when i try to run my EA on AUDCAD.    I'm pretty sure i've narrowed it down to:  MarketInfo(Symbol(), MODE_TICKSIZE).  If i print that. It returns 1e-5

```   double LotSize = MarketInfo(Symbol(), MODE_LOTSTEP);

double riskAmount = AccountFreeMargin() * (Risk / 100);

double Stop_Loss = (ATR * 1.5) / Pip;

/////////////////////////////////////////////////////////////////////Im pretty sure this is where the error is.  Tick is printing as 0////////////////////

double Tick = (MarketInfo(Symbol(), MODE_TICKVALUE) * Point) / MarketInfo(Symbol(), MODE_TICKSIZE);

if (riskAmount > 0 && Stop_Loss > 0 && Tick > 0)
{
LotSize =(riskAmount / ((Stop_Loss + Spread) / Tick)) / 10;
}

if (LotSize > MarketInfo(Symbol(), MODE_MAXLOT))
LotSize = MarketInfo(Symbol(), MODE_MAXLOT);
//Make sure lot size is not too small
else if (LotSize <= MarketInfo(Symbol(), MODE_MINLOT))
LotSize = MarketInfo(Symbol(), MODE_MINLOT);

LotSize = MathRound(LotSize / MarketInfo(Symbol(), MODE_LOTSTEP)) * MarketInfo(Symbol(), MODE_LOTSTEP);
Print("Rounded LotSize: ", LotSize);```
20070

1. J4str: If i print that. It returns 1e-5

1E-5 = 0.00001 = Point. Nothing to do with your divide by zero.

2. J4str: I'm pretty sure i've narrowed it down to:
What do you mean your "pretty sure" Only you know what line and column your error message says. Do you want us to guess? Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.

3. ```   double LotSize = MarketInfo(Symbol(), MODE_LOTSTEP);

double riskAmount = AccountFreeMargin() * (Risk / 100);

double Stop_Loss = (ATR * 1.5) / Pip;

/////////////////////////////////////////////////////////////////////Im pretty sure this is where the error is.  Tick is printing as 0////////////////////

double Tick = (MarketInfo(Symbol(), MODE_TICKVALUE) * Point) / MarketInfo(Symbol(), MODE_TICKSIZE);
```
There is no context here — don't make us guess. Are these global variables?
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 - Inflation - MQL4 programming forum

4. `LotSize =(riskAmount / ((Stop_Loss + Spread) / Tick)) / 10;`
What is this hard coded 10 you divided by? It depends on your account currency and the symbol.
In code (MT4): Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage.
1. 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.
2. AccountBalance * 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.)
3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
4. You must normalize lots properly and check against min and max.
5. You must also check FreeMargin to avoid stop out

Most pairs are worth about \$10 per PIP. A \$5 risk with a (very small) 5 PIP SL is \$5/\$10/5 or 0.1 Lots maximum.

33

i had someone help me with writing the that part.  He said the dividing by 10 was because it was always off by a factor of 10 and instead of figuring it out, he just added that line.     I connected to the internet and the error went away. So that must be what it was.     But, you are right, the calculations are off, because i set up my chart to start right before a stop loss was hit.  It should be a 2% loss and it was a 1.3% loss.
883

If you get this error again, copy the lines from the log and show us all of your code. You can blank out parts of it if you feel it compromises your strategy.
To add comments, please log in or register