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

from M1 data. The trades that occur in live trading will not necessarily correspond to trade made in<br />

the Strategy Tester.<br />

But <strong>by</strong> placing our trades on the close on the bar and using "Open prices only" as the testing model,<br />

we can get testing results that more accurately reflect real-time trades. The disadvantage of trading<br />

once per bar is that trades may be executed late, especially if there is a lot of price movement over<br />

the course of the bar. It's basically a trade-off between responsiveness and reliability.<br />

To check the trade conditions once per bar, we must examine the time stamp of the current bar. We<br />

will save this time stamp to a global variable. Upon each execution of the expert advisor, we will<br />

compare the saved time stamp to the current time stamp. Once the time stamp of the current bar<br />

changes, indicating that a new bar has opened, we will then check the trading conditions.<br />

We must also adjust the shift parameter of our indicator functions, price functions and arrays to<br />

return the value of the previous bar. If an indicator function or price array is set to check the current<br />

bar, we will shift the bar index <strong>by</strong> 1 to check the previous bar instead. All indicators and price arrays<br />

must have their shift parameters incremented <strong>by</strong> 1.<br />

Technically, we are checking trading conditions on the first tick of a new bar, while examining the<br />

closing value of the previous bar. We do not check the currently opened bar when executing once per<br />

bar.<br />

Here is the code to check for the opening of a new bar. First, we declare an external variable named<br />

CheckOncePerBar to turn this feature on and off. Then we declare a datetime global variable to<br />

store the time stamp of the current bar – this will be CurrentTimeStamp.<br />

In the init() function, we will assign the time stamp of the current bar to CurrentTimeStamp. This<br />

will delay the trade condition check until the opening of the next bar:<br />

// External variables<br />

extern bool CheckOncePerBar = true;<br />

// Global variables<br />

datetime CurrentTimeStamp;<br />

// Init function<br />

int init()<br />

{<br />

CurrentTimeStamp = Time[0];<br />

}<br />

118

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

Saved successfully!

Ooh no, something went wrong!