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.

Advanced Order Placement<br />

}<br />

string ErrLog = StringConcatenate("Bid: ",Bid," Ask: ",Ask," Price: ",<br />

PendingPrice," Lots: ",LotSize);<br />

Print(ErrLog);<br />

At the top, we include the stdlib.mqh file. We add the ErrorCode global variable to store our error<br />

code. The OrderSend() places a buy stop order. If the function is not successful, our error handling<br />

code is run.<br />

First, we store the value of GetLastError() in ErrorCode. Then we call the ErrorDescription()<br />

function, using ErrorCode as the argument. Next, we use the StringConcatenate() function to<br />

create our alert message, which is stored in the string variable ErrAlert.<br />

StringConcatenate() is an MQL function that allows you to create complex strings using variables<br />

and constants. Each string element to be joined (or "concatenated") together is separated <strong>by</strong> a<br />

comma. Try typing the examples above into MetaEditor to view it with syntax highlighting.<br />

You can also concatenate strings <strong>by</strong> combining them with a plus sign (+). Using<br />

StringConcatenate() is clearer and more efficient, but if you want to simply concatenate a short<br />

string, use the plus sign to combine string constants and variables:<br />

string PlusCat = "The current Ask price is "+Ask;<br />

// Sample output: The current Ask price is 1.4320<br />

The Alert() function displays a pop-up on the user's desktop, containing the contents of the<br />

ErrAlert variable. Figure 3.2 displays the output of the Alert() function.<br />

We construct another string with our price and trade parameters, and store it in the ErrLog variable,<br />

which we pass to the Print() function. Print() prints the contents of the function argument to the<br />

experts log. The experts log can be viewed from the <strong>Expert</strong>s tab inside the Terminal window, or from<br />

the Journal tab in the Tester window if you're using the Strategy Tester.<br />

55

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

Saved successfully!

Ooh no, something went wrong!