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.
EXPERT ADVISOR PROGRAMMING<br />
Margin Check<br />
MetaTrader comes with functions that allow you to check the current free margin or stop out level<br />
before placing an order. The stop out level is the percentage or amount of free margin below which<br />
you will not be able to place orders. Manually checking the free margin or stop out level before<br />
placing an order is not really necessary however, as an error will occur if you try to place an order<br />
with too little margin.<br />
A more useful idea would be to determine your own stop out level, and halt trading if the current<br />
equity goes below that level. Let's start <strong>by</strong> declaring an external variable called MinimumEquity,<br />
which is the minimum amount of equity required in our account before we can place an order.<br />
We'll compare MinimumEquity to our current account equity. If the current equity is less than our<br />
minimum equity, the order will not be placed, and an alert message will inform the user of the<br />
condition. Let's assume we have an account balance of $10,000. If we lose more than 20% of that<br />
equity, we do not want to place the order. Here is the code to check the minimum equity:<br />
// External variables<br />
extern int MinimumEquity = 8000;<br />
// Order placement<br />
if(AccountEquity() > MinimumEquity)<br />
{<br />
// Place order<br />
}<br />
else if(AccountEquity()