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.

Tips and Tricks<br />

The code will be very similar to the previous section where we added the minimum margin check. We<br />

will include the code from the previous section to show how these various checks work together:<br />

// External variables<br />

extern int MaximumSpread = 5;<br />

extern int MinimumEquity = 8000;<br />

if(AccountEquity() > MinimumEquity && MarketInfo(Symbol(),MODE_SPREAD) < MaximumSpread)<br />

{<br />

// Place order<br />

}<br />

else<br />

{<br />

if(AccountEquity() MaximumSpread) Alert("Current spread is<br />

greater than maximum spread! Order not placed.");<br />

Note that we perform both the minimum equity check and the spread check before placing the order.<br />

If an one of the conditions are false, we go to the else block and check to see which of the<br />

conditions caused the order to not be placed. We will display one or more alerts depending on which<br />

condition is true.<br />

Multiple Orders<br />

You may wish to place multiple orders per position with different stop loss and take profit levels, as<br />

well as lot sizes. There are several ways to accomplish this. One way is to simply use a different<br />

OrderSend() statement for each order you want to place. This is assuming that you plan on placing<br />

the same number of orders every time.<br />

Another way is to use a for loop to place the orders. This way, you can adjust the number of orders<br />

to place at one time. You can pre-load your stop loss and take profit prices into arrays, and increment<br />

through the arrays in the for loop.<br />

Let's start <strong>by</strong> defining external variables for three stop loss and take profit levels. Any additional<br />

orders above three will not have a stop loss or take profit placed. We'll also add an external variable<br />

to adjust the number of orders to place.<br />

extern int StopLoss1 = 20;<br />

extern int StopLoss2 = 40;<br />

extern int StopLoss3 = 60;<br />

133

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

Saved successfully!

Ooh no, something went wrong!