Using three moving averages in EA

Hi,

I have this EA that simply buys or sell when the fastest EMA is on top of the 2 others EMA and vice versa for sell. However, does anyone know how I can make it so it only sells then buys and not buy buy or sell sell.

When the price is in between the 2 slowest EMAS, it can sometimes retrace and recreate a sell signal before a buy signal has occurred. Here is an example:

So the first sell signal is the orange circle, after that it takes two other sells on the black circles, but I would like to only buy after an initial sell signal has occurred.

Thanks for your help

when you detect a signal, before sending any Buy/Sell order, check if the same-direction position is open or not. send order if no same direction position is open already.

you can either track the record of your sent orders, or open positions. OR you can check the very current open positions at the moment of detecting a signal.
( also consider : sometimes(depending on your code and your exit strategies) a new signal should close an already open position with the opposite direction)

tip : to check the open positions, loop through all open positions, filter by symbol and magic number.

if(BUY_SIGNAL_DETECTED())
{
        // check if there's free room for new trade.
        bool FreeRoom = true;
        int N = PositionsTotal();
        ENUM_POSITION_TYPE direction;
        if(N>0)
        {
                for(int i=0; i<N; i++)
                {
                        if(PositionGetSymbol(i)==_Symbol && PositionGetInteger(POSITION_MAGIC)==EA_magic && ...)
                        {
                                FreeRoom = false;
                                direction = PositionGetInteger(POSITION_TYPE);  // buy/sell?
                                break;  // assumes you're gonna have one position per symbol
                        }
                }
        }
        if(FreeRoom) // <<<<<< open new long position
        else if(direction==POSITION_TYPE_BUY) // <<<<< already long is open, maybe adjust its TP to a more gain/trade
        else // <<<<< Close the sell. or ignore the long signal or whatever....
}
else if(SELL_SIGNAL_DETECTED())
{
        // check if there's free room for new trade.
        bool FreeRoom = true;
        int N = PositionsTotal();
        ENUM_POSITION_TYPE direction;
        if(N>0)
        {
                for(int i=0; i<N; i++)
                {
                        if(PositionGetSymbol(i)==_Symbol && PositionGetInteger(POSITION_MAGIC)==EA_magic && ...)
                        {
                                FreeRoom = false;
                                direction = PositionGetInteger(POSITION_TYPE);  // buy/sell?
                                break;  // assumes you're gonna have one position per symbol
                        }
                }
        }
        if(FreeRoom) // <<<<<< open new short position
        else if(direction==POSITION_TYPE_SELL) // <<<<< already short is open, maybe adjust its TP to a more gain/trade
        else // <<<<< Close the long. or ignore the short signal or whatever....
}

Thank you very much!