MT5 hedge and pyramiding failed



I have mt5 setting for my trading robot but it doesn't follow the inputs!

so I put hedging to true and pyramiding to off = only 1 position open at a time and reverse position will close the open one.

for example trading xauusd let say buy then during this trade a sell position triggered so it should close buy before open sell but what actually happened kept buy open and open sell of same symbol at same time. any idea? what went wrong and why is this happening? and how to solve it?

thanks ahead