MinLot and LotStep problem during backtest: SOLVED

 

This is an answer, rather than a question. I searched for an anwser for my question and all I found were threads started by other people asking the same question, with no complete answer. I think I solved the problem I was having - so here is the answer for the next person. (I hope I am correct).

I was having trouble with Error 131 (invalid volume) during backtests when the terminal was disconnected, but not when the terminal was connected. Same EA code. Same terminal. Never noticed this problem before in hundreds of hours of backtesting.

I was using the latest Alpari MT4 installation (Win7), connected to a (micro+classic) demo account. The minimum lotsize for that is 0.10, and the lot step is 0.01. I adjusted the lotsize calculation to reflect that, and it backtested (M5) perfectly when the terminal was connected (apart from the history gaps).

Then I disconnected the terminal, deleted all history data from the installation directory, F2 imported M1 EURUSD data from here http://www.fxdd.com/us/en/forex-resources/forex-trading-tools/metatrader-1-minute-data/ and then created M5 history using the deault period converter, adjusted the spread using this http://www.jeremywhittaker.com/2011/04/09/how-mt4-calculates-spread-and-how-to-manually-change-it/ then restarted the terminal and ran the backtest again (same period, same symbol, same EA, same test dates, same settings). This time I got hundreds of Error 131 errors when lotsize was not rounded to the first decimal place.

It is intersting to note that MarketInfo() data for MINLOT and LOTSTEP are 0.1 and 0.01 respectively when connected, and both 0 when not connected - even though spread is set to last available when not connected.

I noticed that this article about backtesting limits https://www.mql5.com/en/articles/1512 mentions:

Lot sizes including initial size and increment step, commissions and swaps should be taken from the active account settings

Before testing, it is necessary to make sure that there is at least one activated account in the list in "Navigator" window of the terminal.

This explains the problem, since I usually disconnect the terminal by getting the firewall to block the terminal's access to the internet, whereas this time I disconnected it by simply deleting the user profile so that it did not automatically connect.

** HOWEVER!, the problem is not quite that simple. The problem persisted even after I put back "at least one activated account in the list in "Navigator" window of the terminal." It seems that just having an account up there is not sufficient. Apparently the "Save Account Information" in the login box ALSO needs to be ticked. I tried it first without the details saved, and removed the password from the box so that it would not be able to connect when it seems to randomly try to connect to the server every now and then. The Error 131 errors occurred under these conditions.

It was only after I put full login details in the boxes, ticked the "Save Account Information" box and disabled the terminal with the firewall that it began to work properly again. Perhaps it has to at least "try" to connect for the lotsize data to be loaded?


I don't know enough about the terminal to know why this might be the case. I just came across this by trial and error. I hope I am correct, and not just imagining it! I replicated it on another computer to check.

I hope this helps some people who are getting unexplained lotsize errors. You may be wasting your time pulling your hair out going over your code, when the problem seems to be with the terminal.

 
The way I disconnect from my Broker is to use an incorrect login ID, it fails and then you are disconnected. I have had the problem you describe but only when trying to set the spread manually.
 

Yeah, I noticed that some methods of trying to set the spread manually (using scripts which alter the symbols.sel file) also seem to reset the minlot and/or lotstep, which causes a problem. I gave up trying to understand the symbols.sel file, and now I just use the program above to alter the spread, which is free and doesn't seem to touch anything else apart from the spread.

Reason: