Expert Advisor Programming by Andrew R. Young
Expert Advisor Programming by Andrew R. Young
Expert Advisor Programming by Andrew R. Young
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Working with Functions<br />
The arguments to our function are highlighted in bold. Other than the fact that we're using<br />
arguments now, the code is identical to the lot size calculation code from earlier. We've added a<br />
return statement at the end of the function – this will return the value of LotSize to our calling<br />
function.<br />
The function itself will be placed somewhere in our program file, outside of the start() and init()<br />
functions, or it will be located in an external include file. In the latter case, an #include statement at<br />
the top of the program would include the file for use in our program.<br />
Here's how we would use this function in code. First, let's list the external variables we'll use for our<br />
lot size settings:<br />
extern bool DynamicLotSize = true;<br />
extern double EquityPercent = 2;<br />
extern double FixedLotSize = 0.1;<br />
extern double StopLoss = 50;<br />
And here's how we call the function. This line of code would be located inside the start() function:<br />
double LotSize = CalcLotSize(DynamicStopLoss,EquityPercent,StopLoss,FixedLotSize);<br />
Our external variables are passed to the function as arguments. The function will calculate our lot<br />
size, and the value will be saved in the variable LotSize. Note that this variable is different from the<br />
LotSize variable that is inside the CalcLotSize() function. Both variables are local to their<br />
functions, so even though they have the same name, they are not the same variable.<br />
Lot Verification Function<br />
Let's continue with the lot verification code from page 51. This will be a separate function, in case<br />
you decide to use an alternate method of calculating lot size. Regardless of the method of<br />
determining lot size, you'll want to verify it before using passing it to an order placement function:<br />
double VerifyLotSize(double argLotSize)<br />
{<br />
if(argLotSize < MarketInfo(Symbol(),MODE_MINLOT))<br />
{<br />
argLotSize = MarketInfo(Symbol(),MODE_MINLOT);<br />
}<br />
else if(argLotSize > MarketInfo(Symbol(),MODE_MAXLOT))<br />
{<br />
argLotSize = MarketInfo(Symbol(),MODE_MAXLOT);<br />
}<br />
65