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.

EXPERT ADVISOR PROGRAMMING<br />

else if(LotSize > MarketInfo(Symbol(),MODE_MAXLOT))<br />

{<br />

LotSize = MarketInfo(Symbol(),MODE_MAXLOT);<br />

}<br />

We simply compare the value of LotSize, our calculated or fixed lot size from above, to the minimum<br />

and maximum lot size. If LotSize is less than the minimum lot size, or greater than the maximum lot<br />

size, it will be assigned the appropriate minimum or maximum value.<br />

Next, we need to compare our lot size to the step value. The step value indicates whether the broker<br />

allows micro lots (0.01) or mini lots (0.1). If you attempt to use a micro lot size on a broker that only<br />

allows mini lots, you will get an error and the trade will not be placed. Here's the code to check the<br />

step value:<br />

if(MarketInfo(Symbol(),MODE_LOTSTEP) == 0.1)<br />

{<br />

LotSize = NormalizeDouble(LotSize,1);<br />

}<br />

else LotSize = NormalizeDouble(LotSize,2);<br />

The NormalizeDouble() function rounds the value of LotSize to the number of digits specified in<br />

the second argument. In the first line, if the step size is 0.1, indicating the the broker only uses mini<br />

lots, LotSize will be rounded to one decimal place. Otherwise, LotSize will be rounded to 2 decimal<br />

places.<br />

If in the future you happen to come across a broker that allows lot sizes up to three decimal places,<br />

then you could easily modify the above code to check that as well. But at moment, virtually every<br />

MetaTrader broker uses either one or two decimal places for lot sizing.<br />

Other Considerations<br />

Trade Context<br />

MetaTrader has a single trade execution thread for expert advisors. This means that only one expert<br />

advisor can trade at any one time, regardless of how many expert advisors are running in the<br />

terminal. Before commencing with any trade operations, we must check to see whether the trade<br />

execution thread is currently being used.<br />

The function IsTradeContextBusy() will return true if the trade execution thread is occupied,<br />

otherwise false. We will call this function just before calling any trading functions, including<br />

OrderSend(), OrderClose(), OrderDelete() or OrderModify().<br />

52

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

Saved successfully!

Ooh no, something went wrong!