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

double PipsProfit = MarketInfo(argSymbol,MODE_BID) - OrderOpenPrice();<br />

double MinProfit = MinimumProfit * PipPoint(argSymbol));<br />

// Modify Stop<br />

if(OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol<br />

&& OrderType() == OP_BUY && CurrentStop < MaxStopLoss<br />

&& PipsProfit >= MinProfit)<br />

{<br />

bool Trailed = OrderModify(OrderTicket(),OrderOpenPrice(),MaxStopLoss,<br />

OrderTakeProfit(),0);<br />

// Error Handling<br />

if(Trailed == false)<br />

{<br />

ErrorCode = GetLastError();<br />

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

string ErrAlert = StringConcatenate("Buy Trailing Stop - Error ",<br />

ErrorCode,": ",ErrDesc);<br />

Alert(ErrAlert);<br />

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

MarketInfo(argSymbol,MODE_BID), " Ticket: ",CloseTicket,<br />

" Stop: ",OrderStopLoss()," Trail: ",MaxStopLoss);<br />

Print(ErrLog);<br />

}<br />

}<br />

}<br />

}<br />

We calculate the current order profit in pips <strong>by</strong> subtracting OrderOpenPrice() from the current Bid<br />

price, and storing that in the variable PipsProfit. We compare that to our minimum profit setting,<br />

which is multiplied <strong>by</strong> PipPoint() and stored in the variable MinProfit.<br />

If the current profit in pips (PipsProfit) is greater than or equal to our minimum profit<br />

(MinProfit), and all of the other conditions are true, the stop will be modified.<br />

The trailing stop with the minimum profit setting is much more flexible, so you'll probably want to use<br />

this function in your expert advisor. See Appendix D for the complete sell trailing stop code.<br />

Break Even Stop<br />

You can also use this method to apply a break even stop adjustment to your orders. A break even<br />

stop adjusts the stop loss to be equal to the order opening price, after a certain level of profit has<br />

been reached. The break even stop is independent from your initial stop loss and trailing stop<br />

functions.<br />

90

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

Saved successfully!

Ooh no, something went wrong!