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 />

// Lot size calculation<br />

if(DynamicLotSize == true)<br />

{<br />

double RiskAmount = AccountEquity() * (EquityPercent / 100);<br />

double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);<br />

if(Point == 0.001 || Point == 0.00001) TickValue *= 10;<br />

double CalcLots = (RiskAmount / StopLoss) / TickValue;<br />

double LotSize = CalcLots;<br />

}<br />

else LotSize = FixedLotSize;<br />

// Lot size verification<br />

if(LotSize < MarketInfo(Symbol(),MODE_MINLOT))<br />

{<br />

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

}<br />

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

{<br />

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

}<br />

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

{<br />

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

}<br />

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

The lot size calculation and verification code from page 51 is added to the beginning of our start<br />

function. Since our stop loss level is known beforehand, this is a good a place as any to put it. The<br />

remaining code is our modified buy market order routine:<br />

// Buy Order<br />

if(FastMA > SlowMA && BuyTicket == 0)<br />

{<br />

// Close Order<br />

OrderSelect(SellTicket,SELECT_BY_TICKET);<br />

if(OrderCloseTime() == 0 && SellTicket > 0)<br />

{<br />

double CloseLots = OrderLots();<br />

while(IsTradeContextBusy()) Sleep(10);<br />

RefreshRates();<br />

double ClosePrice = Ask;<br />

bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);<br />

58

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

Saved successfully!

Ooh no, something went wrong!