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.
Order Management<br />
Trailing Stops<br />
We can also use our order loop to modify multiple orders. A common example of this is the trailing<br />
stop. A trailing stop moves the stop loss up or down with the order price as the order gains in profit.<br />
This "locks in" profit and provides excellent loss protection.<br />
The trailing stop is expressed as a maximum number of pips. For example, if your trailing stop is 50<br />
pips, the stop loss will never be more than 50 pips away from your price. If the price reverses and<br />
the profit declines, the stop loss will stay where it is. The stop only moves in the direction of profit –<br />
never in reverse.<br />
When modifying a trailing stop, we must check to see if the distance in pips between the current<br />
price and the current stop loss is greater that the trailing stop. If so, the stop loss will be modified so<br />
that the distance from the current price in pips is equal to the number of pips in the trailing stop<br />
setting.<br />
The trailing stop is calculated relative to the closing price, which is the Bid for buy orders, and the<br />
Ask for sell orders. Note that this is the opposite of the opening price. Let's examine the code for<br />
modifying a trailing stop. First, we declare the external variable for our trailing stop setting:<br />
extern double TrailingStop = 50;<br />
This code checks all buy market orders and modifies the stop loss as necessary:<br />
for(int Counter = 0; Counter