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.

Advanced Order Placement<br />

If you must use a large stop loss, or none at all, a fixed lot size would probably be more beneficial.<br />

We need to be able to choose between calculating the lot size or using a fixed lot size. Let's use an<br />

external boolean variable called DynamicLotSize to turn our lot size calculation on and off:<br />

// External variables<br />

extern bool DynamicLotSize = true;<br />

extern double EquityPercent = 2;<br />

extern double FixedLotSize = 0.1;<br />

// Start function<br />

if(DynamicLotSize == true)<br />

{<br />

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

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

if(Digits == 3 || Digits == 5) TickValue *= 10;<br />

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

double LotSize = CalcLots;<br />

}<br />

else LotSize = FixedLotSize;<br />

If DynamicLotSize is set to true, we will calculate the lot size based on the stop loss, and assign that<br />

value to the LotSize variable. If DynamicLotSize is false, we simply assign the value of<br />

FixedLotSize to LotSize. The LotSize variable will be passed to the OrderSend() function as the<br />

lot size for the order.<br />

Verifying Lot Size<br />

Just like the stop loss, take profit and pending order prices, the lot size should also be verified to<br />

make sure it is acceptable to your broker. This means that your lot size should not be too large or too<br />

small, and it should not be specified in micro lots (0.01) if your broker doesn't support those. You<br />

should also normalize your lot size to the appropriate decimal place.<br />

Let's check the minimum and maximum lot size first. The MarketInfo() function, using the<br />

MODE_MINLOT and MODE_MAXLOT parameters, will be used to compare the current lot size to the<br />

minimum and maximum lot size. If the lot size is not valid, it will automatically be resized to the<br />

minimum or maximum.<br />

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

{<br />

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

}<br />

51

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

Saved successfully!

Ooh no, something went wrong!