Zero divide on restart due to LotStep being changed to zero . .
-
Hello,
I had a similar issue 3 years ago but unable to solve on a new EA.
If EA has a live trade and MT4 restarts for whatever reason it throws a Zero Divide due to "LotStep" being set to "0" on restart.
a) It doesn't happen if there is no trade.
b) Manual solution I have found is to add "if(LotStep == 0) LotStep = 0.01;" (my broker step) within the "AlignLots" function manually.Question: Any ideas why this is happening and is there a way for me to add a custom block to insert the fix automatically or would it be possible for you to add this line into the system code please because it does seem like a bug.
Many thanks
MantaDiver
-
@mantadiver If LotStep is your variable, (MarketInfo(Symbol(),MODE_LOTSTEP)); as code input in a modify variables block will ensure this value will be in variable and change for each broker.
-
@JSTAP LotStep is created as part of the "System and Custom functions used in the program" area of the standard code. I have tried recreating / writing to / overwriting but I just get errors.
This is where it first appears:
double AlignLots(string symbol, double lots, double lowerlots = 0.0, double upperlots = 0.0)
{
double LotStep = SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);
...........Populates fine on initial startup but changes to a zero value on restart with a live trade.
-
@mantadiver Is this MT5? Code can be different, although most code can be used in FX via custom code I often find it works better only single lines are uses to compliment other blocks. Put a shared link on here with an explanation of what you are trying to achieve and, I'll have a look and see what can be done.
-
@JSTAP It is MT4 code.
The reality is I am trying to cover off a situation that is unlikely to occur . . . i.e. Server / MT4 / EA restarts when the EA has a live trade and the EA fails to start due to Zero Divide ewrror and leaves an orphaned trade.
I have had the same issue with a prior EA build with fxDreema and I would be interested to know if anyone else gets the error when they restart an EA with a trade running.
Maybe I have done something that has a knock on effect.
Anyway, given that I have noticed the issue I thought it prudent to put in a fix. My logic says that as it is proven that the system variable LotStep works on startup but changes to zero on restart that either fxdreema might provide a "bug fix" to prevent it happening or someone might offer a fix I can apply.
From my attemps so far I ahve been unable to interact with and change the value of LotStep.
Appreciate your time and interest, but at the end of the day it may never happen in live trading !
-
@mantadiver is double LotStep a variable in FX that you can change? If is put value in a comment and see how you can get it to change.
-
I haven't managed to change it. Whenever I reference it I get "LotStep' - undeclared identifier".
-
@mantadiver I have tested this code, (MarketInfo(Symbol(),MODE_LOTSTEP)); if I have this in a modify variables block then it throws no error and in a comment block it has the lot step value. Put a shared link on here and what is happening can be assessed, are you sure you have a variable created in FX with the correct name/type? These errors are normally something simple to fix.
-
Working on it
-
-
It all works fine on TIME charts but not on OFFLINE charts. I am using Ovo charts but same issue exists when using the MT4 standard PeriodConverter.
-
There are actually two issues involved:-
a) LotStep returns zero after mt4 retart when using offline chart and there is an existing trade.
b) EA returns a zero divide error on restart (again offline chart & existing trade) in the fxdreema system code AlignLots.
I have discovered the reason for the zero divide. On restart I am using the "If Trade" block to detect if there is already a trade with the EA magic number. From what I can tell the "If Trade" block does not detect trades on restart on offline charts. I have tried various blocks and methods but none seem to work.
This is a simple project as an example:-
It prints the LotStep, Magic Nuymber and trade status to a comment.
Simple rule: If no trade then Buy.
If the EA is run, no trade is detetcted so it Buys. Restart MT4 and on restart the LotStep returns zero, it still says there is no trade, (even though there is) and in the Experts tab there is a zero divide error relating to LotStep in the AlignLots line [size=MathRound(size/LotStep)*LotStep;]
Therefore my take is that the zero divide is caused by the EA trying to take another trade & being met with LotStep zero BUT it should not be trying to take another trade because the "If Trade" should have detected a trade is already present.
The solution therefore is to find a way to get fxdreema to detect existing trades on restart when usiung offline charts . . . .
-
-
@mantadiver The problem is likely because you used text value, instead of text(code input) value.

-
That's weird, when I click on it, it says Numeric.

Anyway it was only getting the data to display, the issue remains.
Are you able to get fxdreema to recognise existing trades from offline charts ?
-
@mantadiver The problem is your variable is in as a double not string, do like this and all will work.

-
OK, I think I have solved it.
The problems being experienced were effects not the cause.
The cause is all down to the code running at startup on offline charts BEFORE it has had a chance to read data from the broker.
Therefore it was returning no trades when therre were trades and LotStep returned as zero which led to it trying to take another trade which in turn led to a zero divide error.
The solution I have used is to add a 3 second delay & change a flag to "true" at the end of initialisation and then check that the flag is "true" when executing code in the On Tick tab.
Maybe offline charts need to wait for an additional tick or something but this definitely allows it to report the active trades correctly, reports LotStep correctly and prevents the zero divide.
Many thanks for your interest and help @jstap.
-
@mantadiver I'm glad you sorted it, the problem was in the variable being the wrong type, LotStep is a broker number, doesn't rely on an open trade .
-
@mantadiver I am fairly new to this but we are having issues with zero divide errors. Please see attached. Is your solution related to this?
.
The line of code is to do withe money management where we are calculating lot size related to account balance and SL.
Any help woould be much appreciated. -
@tulefi Sharing the link to your project would help to review it in depth, is possible.