25.10.2016 Views

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EXPERT ADVISOR PROGRAMMING<br />

Fig. 3.2 – Alert message<br />

Here are the log contents. The first line is the output from the Alert() function. The second line is<br />

the output of the Print() function. Notice the error, "invalid trade volume", and the fact that the lot<br />

size reported in the log is 0. In this case, the problem is that the lot size is invalid.<br />

16:47:54 Profit Buster EURUSD,H1: Alert: Open Buy Stop Order - Error 131:<br />

invalid trade volume<br />

16:47:54 Profit Buster EURUSD,H1: Bid: 1.5046, Ask: 1.5048, Lots: 0<br />

You can create similar error handling routines for other functions as well, especially for the<br />

OrderModify() and OrderClose() functions. You can also create more sophisticated error handling<br />

routines that provide custom error messages based on the error code, or perform other actions.<br />

For example, if you receive error 130: "invalid stops", you could display a message such as "The stop<br />

loss or take profit price is invalid." Here's an example of how you can do this:<br />

ErrorCode = GetLastError();<br />

string ErrDesc;<br />

if(ErrorCode == 129) ErrDesc = "Order opening price is invalid!";<br />

if(ErrorCode == 130) ErrDesc = "Stop loss or take profit is invalid!";<br />

if(ErrorCode == 131) ErrDesc = "Lot size is invalid!";<br />

string ErrAlert = StringConcatenate("Open Buy Order - Error ",ErrorCode,": ",ErrDesc);<br />

Alert(ErrAlert);<br />

56

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!