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 />
First, we'll check to see if a valid stop loss level has been passed along with the function. If the<br />
argStopLoss argument is 0, then we return a value of 0 to the calling function, indicating that no<br />
stop loss was specified.<br />
Next, we calculate the stop loss <strong>by</strong> subtracting the stop loss in pips from the order opening price. We<br />
multiply argStopLoss <strong>by</strong> PipPoint() to calculate the fractional value, and subtract that from<br />
argOpenPrice. We will use either the Bid or Ask price (for market orders) or the intended pending<br />
order price.<br />
Note that we do not check the stop level or otherwise verify that the stop loss is valid. We will use<br />
another set of functions to verify or adjust the stop loss price as necessary. You could, of course,<br />
easily modify this function to verify the stop loss price, display an error message, or automatically<br />
adjust the price.<br />
Here is the function to calculate a buy take profit in pips:<br />
double CalcBuyTakeProfit(string argSymbol, int argTakeProfit, double argOpenPrice)<br />
{<br />
if(argTakeProfit == 0) return(0);<br />
double BuyTakeProfit = OpenPrice + (argTakeProfit * PipPoint(argSymbol));<br />
return(BuyTakeProfit);<br />
}<br />
The functions for calculating stop loss and take profit for sell orders are listed in Appendix D. Note<br />
that the function for calculating sell stop loss is nearly identical to the one above for calculating the<br />
buy take profit, and likewise for buy stop loss and sell take profit.<br />
Stop Level Verification<br />
We're going to create two sets of functions to calculate and verify stop levels. The first will simply<br />
calculate the stop level above or below a specified price, and return a boolean value indicating<br />
whether the indicated price is inside or outside the stop level. A second set of functions will<br />
automatically adjust a price so that it is outside the stop level, plus or minus a specified number of<br />
pips.<br />
The following function verifies whether a price is above the upper stop level (the order opening price<br />
plus the stop level). If so, the function returns true, otherwise false:<br />
71