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.

Order Management<br />

Note the condition (CurrentStop > MaxStopLoss || CurrentStop == 0). If there is no stop loss<br />

placed with the order, then the condition CurrentStop > MaxStopLoss will never be true, because<br />

MaxStopLoss will never be less than zero. Thus, we add an OR condition, CurrentStop == 0.<br />

If the current order's stop loss is 0 (no stop loss), then as long as the remaining conditions are true,<br />

the trailing stop will be placed.<br />

Minimum Profit<br />

Let's enhance our trailing stop <strong>by</strong> adding a minimum profit level. In the above example, the trailing<br />

stop will kick in right away. If you set an initial stop loss of 100 pips, and your trailing stop is 50 pips,<br />

the stop loss would be set to 50 pips immediately, invalidating your initial 100 pip stop loss.<br />

Adding a minimum profit level will allow you to set an initial stop loss, while delaying the trailing stop<br />

until a specified amount of profit is reached. In this example, let's assume an initial stop loss of 100<br />

pips is set when the order is placed. We're using a trailing stop of 50 pips, with a minimum profit<br />

level of 50 pips. When the profit for the order reaches 50 pips, the stop loss will be adjusted to break<br />

even.<br />

Let's add an external variable for our minimum profit setting:<br />

extern int TrailingStop = 50;<br />

extern int MinimumProfit = 50;<br />

The following function modifies the stop loss for all buy market orders, checking the minimum profit<br />

before doing so:<br />

void BuyTrailingStop(string argSymbol, int argTrailingStop, int argMinProfit,<br />

int argMagicNumber)<br />

{<br />

for(int Counter = 0; Counter

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

Saved successfully!

Ooh no, something went wrong!