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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXPERT ADVISOR PROGRAMMING<br />

we need to check the input or output of the function. Let's create a Print() statement and print the<br />

contents of all relevant variables to the log.<br />

We'll run the EA in the Strategy Tester, using Open prices only as our testing model. Make sure you're<br />

testing the EA over a long enough time period so that it will place enough trades for us to analyze. If<br />

you need to check prices on the chart, hit the Open Chart button to open a chart showing the<br />

simulated trades.<br />

Next, we'll go to the Journal tab and check for the information we need. If we need to view the log in<br />

it's entirety, or if there are trades that are not showing in the Journal tab, we can right-click and<br />

choose Open from the pop-up menu, and open the log file directly.<br />

This code is giving us error 130: "invalid stops" every time we place a buy order. We know that error<br />

130 means that either the stop loss or the take profit is incorrect. Can you identify the error?<br />

if(Close[0] > MA && BuyTicket == 0)<br />

{<br />

double OpenPrice = Ask;<br />

double BuyStopLoss = OpenPrice + (StopLoss * UsePoint);<br />

double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);<br />

BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,<br />

BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);<br />

}<br />

SellTicket = 0;<br />

We will use the Print() function to verify the parameters that are being passed to the OrderSend()<br />

function. We'll focus on the order opening price, the stop loss and the take profit.<br />

Print("Price:"+OpenPrice+" Stop:"+BuyStopLoss+" Profit:"+BuyTakeProfit);<br />

Here is the output when we run the EA in the strategy tester. A stop loss and take profit of 50 pips is<br />

assumed:<br />

11:52:12 2009.11.02 02:00 Example EURUSD,H1: OrderSend error 130<br />

11:52:12 2009.11.02 02:00 Example EURUSD,H1: Price:1.47340000 Stop:1.47840000<br />

Profit:1.47840000<br />

We know that the stop loss must be below the opening price for a buy order. Here, it is above the<br />

price. In fact, it's the same price as the take profit. A quick look at our code and we realize that we<br />

accidentally inserted a plus sign in the buy stop loss equation. Here is the correct code:<br />

142

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

Saved successfully!

Ooh no, something went wrong!