CTradePreflight checks a trade request before OrderSend(), so an EA finds an off-step volume, a wrong-side stop, missing margin or a closed session instead of getting a server rejection. The class repairs only safe cases, reads the OrderCheck() retcode, and counts every refusal by reason. Unit tests and a random-delay stress EA show that levels placed very close to the price can still fail, and an optional delay buffer stops those rejections at the cost of blocking some valid requests.
Any trade request an Expert Advisor sends can come back refused. In the Strategy Tester the refusal is a "failed" line in the journal with a retcode: 10014 for an invalid volume, 10016 for invalid stops, 10019 when there isn't enough money. On a live account the same lines go to the Journal tab, where they are easy to miss.
I learned how common these refusals are when a tick scalper of mine needed 5 submissions to pass the automatic CodeBase validation. The first 4 came back with 21 errors each. I still have one of those reports, and the errors I can still read in it are trade requests that my code had already checked and passed. I wrote up those failures and their fixes in an earlier article. This one is about the checks that could run before each request.
MetaQuotes has already published the standard list of checks in The checks a trading robot must pass before publication in the Market. It covers free margin, volume limits, the stops level, the freeze level and modifications that change nothing. I treat that article as the rulebook and won't repeat its explanations here. Artyom Trishkin's DoEasy series extends these checks in parts XXII, XXIII and XXIV. Its trading classes validate limits and parameters before a request is sent and can correct invalid values. However, that implementation is part of a large library. I wanted a component that can be dropped into a single EA. So my goal was one object that every supported request passes through before OrderSend(). I wanted it to work for any symbol and to be testable without a broker. It also had to fix small mistakes without raising the volume or widening a stop loss on its own, and keep count of what it refused.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Check out the new article: A Trade Request Preflight Class in MQL5: Checking Volume, Stops, Margin and Sessions Before OrderSend.
Any trade request an Expert Advisor sends can come back refused. In the Strategy Tester the refusal is a "failed" line in the journal with a retcode: 10014 for an invalid volume, 10016 for invalid stops, 10019 when there isn't enough money. On a live account the same lines go to the Journal tab, where they are easy to miss.
I learned how common these refusals are when a tick scalper of mine needed 5 submissions to pass the automatic CodeBase validation. The first 4 came back with 21 errors each. I still have one of those reports, and the errors I can still read in it are trade requests that my code had already checked and passed. I wrote up those failures and their fixes in an earlier article. This one is about the checks that could run before each request.
MetaQuotes has already published the standard list of checks in The checks a trading robot must pass before publication in the Market. It covers free margin, volume limits, the stops level, the freeze level and modifications that change nothing. I treat that article as the rulebook and won't repeat its explanations here. Artyom Trishkin's DoEasy series extends these checks in parts XXII, XXIII and XXIV. Its trading classes validate limits and parameters before a request is sent and can correct invalid values. However, that implementation is part of a large library. I wanted a component that can be dropped into a single EA. So my goal was one object that every supported request passes through before OrderSend(). I wanted it to work for any symbol and to be testable without a broker. It also had to fix small mistakes without raising the volume or widening a stop loss on its own, and keep count of what it refused.
Author: Ali Akbar