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

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

{<br />

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

}<br />

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

}<br />

return(argLotSize);<br />

For this function, we'll pass the variable with the lot size we calculated using CalcLotSize() as the<br />

argument. The argument variable argLotSize is then processed and returned back to the calling<br />

function.<br />

Order Placement Function<br />

Now it's time to assemble our buy market order placement function. There will be a few differences<br />

between our order placement function and the code we reviewed earlier. For one, we will not be<br />

closing orders in our order placement functions. We will handle the closing of orders separately. We'll<br />

create a function to close orders in the next chapter.<br />

We will also be calculating and modifying our stop loss and take profit prices outside of the order<br />

placement function. Because there are multiple ways of calculating stops, we need to keep our order<br />

placement function as flexible as possible, and not tie it to a predetermined method of calculating<br />

stops. The order modification code has been moved to a separate function.<br />

We'll place our buy order at the current market price using OrderSend(), and if the order was not<br />

placed, we'll run the error handling code from page 54. In any case, we'll return the ticket number to<br />

the calling function, or -1 if the order was not placed.<br />

We are specifying the order symbol using the argSymbol argument, instead of simply using the<br />

current chart symbol. This way, if you decide to place an order on another symbol, you can do so<br />

easily. Instead of using the predefined Bid and Ask variables, we'll need to use the MarketInfo()<br />

function with the MODE_ASK and MODE_BID parameters to retrieve the Bid and Ask price for that<br />

particular symbol.<br />

We have also specified a default value for the order comment. The argument argComment has a<br />

default value, "Buy Order". If no value is specified for this argument, then the default is used. We'll<br />

assume that the lot size and slippage have been calculated and verified prior to calling this function:<br />

66

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

Saved successfully!

Ooh no, something went wrong!