Use the OrderClose() function.

OrderClose - Trade Functions - MQL4 Reference
- docs.mql4.com
OrderClose - Trade Functions - MQL4 Reference

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I want to write a generic MA crossover EA, it just buy when the Fast MA cross above the Slow MA , and sell when the Fast MA cross below Slow MA from .
I watched a couple of tutorials on Youtube on how to do this. And this how I found they usually do it:
My only problem with this code is that it only exit a trade if and only if a stop loss or take profit has been hit.
For example let's just say the fast MA crossed below the slow MA, the EA will open a sell position, then six candles later the fast MA crossed above the slow MA, the EA will not close the current trade and open a buy position, instead it will wait until either stop loss or take profit being hit and then wait for another crossover to happen to open a new position.
To sum up I want to know how to make the EA close the current position and open an opposite position when a reverse signal happen.