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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXPERT ADVISOR PROGRAMMING<br />

// Error Handling<br />

if(TicketMod == false)<br />

{<br />

int ErrorCode = GetLastError();<br />

string ErrDesc = ErrorDescription(ErrorCode);<br />

string ErrAlert = StringConcatenate("Add Stop/Profit - Error ",ErrorCode,": ",<br />

ErrDesc);<br />

Alert(ErrAlert);<br />

}<br />

string ErrLog = StringConcatenate("Bid: ",MarketInfo(OrderSymbol(),MODE_BID),<br />

" Ask: ",MarketInfo(OrderSymbol(),MODE_ASK)," Ticket: ",argTicket,<br />

" Stop: ",argStopLoss," Profit: ",argTakeProfit);<br />

Print(ErrLog);<br />

return(TicketMod);<br />

}<br />

We check first to see if either a stop loss or a take profit price has been supplied. If not, we will exit<br />

the function. Otherwise, we will modify the order using the stop loss and take profit that was passed<br />

to the function. The error handling function will run if the order modification was not successful. This<br />

function will work on all order types.<br />

Using Include Files<br />

To keep our functions organized for easy inclusion in our source code files, we'll place the functions<br />

into an include file. An include file can consist of function declarations, imported functions, and any<br />

global or external variables that you wish to include in an expert advisor.<br />

Include files require no special syntax. You declare the functions and variables in the include file just<br />

as you would in any source code file. Include files should not have an init(), start() or deinit()<br />

function. The file must have an .mqh extension and be located in the \experts\include folder.<br />

All of the functions we create in this book will be placed in an include file named<br />

IncludeExample.mqh. The contents of this file are listed in Appendix D.<br />

Using Libraries<br />

A library is a compiled collection of functions. Whereas an include file is a source code file whose<br />

contents are "included" in the executable file, a library is a separate executable that contains the<br />

imported functions. Therefore you must have both your expert advisor executable and the library<br />

executable to run your EA.<br />

74

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

Saved successfully!

Ooh no, something went wrong!