MQL4 Help

Hi Everyone,

I appreciate any advice you can offer me.

  1. When coding an EA that will open a buy/sell order when FastMA crosses SlowMA, is there a way to program it so it will only open the order if the FastMA [U]closes[/U] above SlowMA? With my current program I have multiple buy and sell orders during the same minute. I’m guessing it’s opening/closing orders every time FastMA ticks across SlowMA.

  2. With the previously mentioned EA, how would you code it so the order is only opened if, say, the RSI is greater than 50 at the time the cross happens? With my current setup (if(FastMA > SlowMA && BuyTicket == 0 && iRSI > 50)) the order will be opened as soon as RSI hits greater than 50, even if that’s long after the FastMA actually crosses the SlowMA. I would like it to only open the order if RSI is greater than 50 when the actual cross occurs and then not check again until the next cross occurs.

I’m new and still have a lot to learn, but I was wondering if these conditions are possible and how advanced they would be.

Thank you for your time.

There is a small trick to doing what you asked about. Usually I wait for the opening of the new candle and check to see if the entry conditions are valid. If so, open an order. In the following code, barStart is declared as a static DateTime variable initialized to 0;

if (barStart < Time[0]) //start of new bar
{
// check for MA crossing and RSI > 50
barStart = Time[0];
}

Thank you CodeMeister. That was exactly what I was looking for.