Trader’s Tech – Writing Your Own EA Part 10 – Program Structure

Forex RobotIn Part 9 we got a little of our code written and had some discussion about syntax and commenting and such. This time we’re going to talk about how to keep from fire too many trades on a single trade signal.

So far, we’ve tested to see if there is a currently open trade, then we checked for a moving average crossover to happen. For our next stunt, before we actually open a trade, we have to be sure that the current signal we’re seeing has not already been acted upon.

For starters, we have to discuss how we will determine that we have a moving average crossover. If you observe an active chart, you will notice that the moving averages can cross-uncross-cross-uncross several times during the course of a single candle. Since we most often use moving averages calculated on the closing price of a candle and because of the crisscross action we described, it makes sense to wait for the candle close to determine if a cross has actually occurred.  The functions provided by MT4 for determining the price location of a moving average (and most other indicator functions as well) allow us to pick the historical candle upon which we received the price of the moving average. In other words, if we tell it to look back 0 (zero) candles, it will give us the moving average based upon the current price. If we tell it to look back 1 (one) candle, it will give us the moving average based upon the last candle close and so on.

The way we will be determining whether a crossover has occurred is to look at the position of the moving averages on the close of the candle two candles back  (the current candle is the one that is still open, the prior candle – or one candle back – is the last candle that closed, the one we will look at is the candle before that one ) and compare it to the moving averages at the close of the prior candle. This picture should help that make sense:

Trader's Tech - MA CrossoverHere is an example of a moving average crossover that has just occurred. Note the current candle is still active (this happens to be a 5 minute chart and we’re about 30 seconds into the current candle.) When the Prior candle closed, it is clear that it closed with the purple MA crossing down over the longer green MA. To test this using the iMA() function of MQL4, you would have to look at relative positions of the closing moving averages on candle (2) and compare to the relative positions of the closing moving averages on candle (1.) If they reverse positions, then you have a crossover. We’ll be writing the function that does this comparison in a future episode. For now, we needed to discuss it so I could explain the issue at hand: how to determine if we have a new crossover signal or one that’s already been acted upon.

If you are looking for this crossover by comparing the MA levels at the number 2 and number 1 lookback candles, then there’s always the possibility – especially on longer term charts – that the trade was entered at the beginning of the current (0) candle and hits its SL or TP before the end of the same candle upon which the trade was taken. If that were to occur, on the next market tick the program would see 1) there is now no trade on and 2) a crossover has occurred on the prior close. That would once again trigger another trade on the exact same signal upon which the prior trade was taken. We’ll discuss one of the several ways this situation can be handled. We will cause the EA to remember on which candle the last trade was taken and be sure that we don’t take another trade on that same candle.

Once again, we’re out of time and this explanation will take just a little bit too much for now. So, tomorrow we’ll discuss the code that will keep us from firing off a hundred trades on the same signal. Yes, I’ve actually done that.  There’s no teacher like experience. 🙂

Thanks for your attention. As always, please ask me questions or tell me what I’m doing wrong. I’m tough, I can handle it. Have a great day and follow me on Twitter.


The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news