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.

Order Management<br />

Here is the external variable for our break even profit setting. The minimum profit is specified in pips.<br />

extern double BreakEvenProfit = 25;<br />

This code will modify the stop loss on all buy market orders to break even, once the order profit in<br />

pips is equal to or greater than BreakEvenProfit. We will not be creating a function for this, but you<br />

can do so if you feel it would be useful.<br />

for(int Counter = 0; Counter = MinProfit<br />

&& OrderOpenPrice() != OrderStopLoss())<br />

{<br />

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

OrderOpenPrice(),OrderTakeProfit(),0);<br />

if(BreakEven == false)<br />

{<br />

ErrorCode = GetLastError();<br />

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

string ErrAlert = StringConcatenate("Buy Break Even - Error ",<br />

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

Alert(ErrAlert);<br />

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

", Ticket: ",CloseTicket,", Stop: ",OrderStopLoss(),", Break: ",<br />

MinProfit);<br />

Print(ErrLog);<br />

}<br />

}<br />

}<br />

We subtract the order opening price from the current Bid price to calculate the current profit in pips,<br />

and store this in PipsProfit. We calculate the minimum profit in pips and store that in MinProfit.<br />

If PipsProfit is greater than or equal to MinProfit, then we will modify the stop loss to be equal<br />

to the order opening price.<br />

We also check to make sure that the stop loss is not already set at the break even price. If<br />

OrderOpenPrice() is not equal to OrderStopLoss(), then we can proceed.<br />

91

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

Saved successfully!

Ooh no, something went wrong!