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

You should recognize these examples from before. Instead of checking the current bar, we will check<br />

the bar that just closed, i.e. the previous bar. If you need to reference a bar previous to the last<br />

closed bar, simply add the current shift parameter to BarShift:<br />

double LastFastMA = iMA(NULL,0,FastMAPeriod,0,0,0,BarShift+1);<br />

If you don't anticipate ever needing to run your expert advisor once per bar, you won't need to add<br />

this code. But for many indicator-based trading systems, this can make your trading and back testing<br />

results more reliable.<br />

To control the execution of trades, we need to check the value of NewBar before the order placement<br />

routines. We can do this using the if block we placed earlier for the timer:<br />

// Begin trade block<br />

if(TradeAllowed == true && NewBar == true)<br />

{<br />

// Buy Order<br />

if(FastMA > SlowMA && BuyTicket == 0 && BuyOrderCount(Symbol(),MagicNumber) == 0)<br />

{<br />

// Buy order code omitted for brevity<br />

}<br />

// Sell Order<br />

if(FastMA < SlowMA && SellTicket == 0 && SellOrderCount(Symbol(),MagicNumber) == 0)<br />

{<br />

// Sell order code omitted for brevity<br />

}<br />

} // End trade block<br />

120

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

Saved successfully!

Ooh no, something went wrong!