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 />

bool VerifyUpperStopLevel(string argSymbol, double argVerifyPrice,<br />

double argOpenPrice = 0)<br />

{<br />

double StopLevel = MarketInfo(argSymbol,MODE_STOPLEVEL) * Point;<br />

if(argOpenPrice == 0) double OpenPrice = MarketInfo(argSymbol,MODE_ASK);<br />

else OpenPrice = argOpenPrice;<br />

double UpperStopLevel = OpenPrice + StopLevel;<br />

if(argVerifyPrice > UpperStopLevel) bool StopVerify = true;<br />

else StopVerify = false;<br />

}<br />

return(StopVerify);<br />

We pass the currency symbol, the price to verify, and the order opening price (optional) as<br />

arguments. By default, the stop level is calculated relative to the Ask price. If argOpenPrice is<br />

specified, the stop level will be calculated relative to that price instead. (Use this when verifying stop<br />

loss and take profit prices for pending orders).<br />

The function will check to see whether argVerifyPrice is greater than the UpperStopLevel. If it is,<br />

the return value will be true. Otherwise, false. You can use this function to check for a valid stop loss,<br />

take profit or pending order price, without modifying the original price. Here's an example where we<br />

check a stop loss price and show an error message if the price is not valid:<br />

bool Verified = VerifyUpperStopLevel(Symbol(),SellStopLoss);<br />

if(Verified == false) Alert("Sell stop loss is invalid!");<br />

The code to check the stop level below the current or pending price is in Appendix D. Our second set<br />

of functions is similar, except that they will automatically adjust the invalid stop loss, take profit or<br />

pending order price to a valid one:<br />

double AdjustAboveStopLevel(string argSymbol, double argAdjustPrice, int argAddPips = 0,<br />

double argOpenPrice = 0)<br />

{<br />

double StopLevel = MarketInfo(argSymbol,MODE_STOPLEVEL) * Point;<br />

if(argOpenPrice == 0) double OpenPrice = MarketInfo(argSymbol,MODE_ASK);<br />

else OpenPrice = argOpenPrice;<br />

double UpperStopLevel = OpenPrice + StopLevel;<br />

72

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

Saved successfully!

Ooh no, something went wrong!