Opening 1 position, Confirming and not opening another in an EA

 
Position opening question for programming..

ALL I want is code for opening a single position ONE Time when conditions are true for entering. And NOT open any more positions in that direction.

Example
1. MA's cross
2. a BUY order is sent
3. CONFIRM Buy is opened (with magic number)
4. Don't open ANYMORE buys, even if above buy hits TP.
5. Do opposite when MA's cross in opposite direction.

I've tried my best to code something to do the above but I get NO positions when there should be one, then i get 1 sometimes, then I get 4 Sometimes.
So my code is Krap obviously.

I ONLY need the code for opening, confirming 1 position