How to handle reverse signal in MA crossover EA

To add comments, please log in or register
wilsonwilson
12
wilsonwilson  

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:

double PreviousFast=iMA(NULL,0,14,0,MODE_SMA,PRICE_CLOSE,2);
double CurrentFast=iMA(NULL,0,14,0,MODE_SMA,PRICE_CLOSE,1);
double PreviousSlow=iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,2);
double CurrentSlow=iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,1);

if (PreviousFast<PreviousSlow && CurrentFast>CurrentSlow)
	if (OrdersTotal()==0)
         	OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*Point),Ask+(TakeProfit*Point),NULL,0,0,Green);

if (PreviousFast>PreviousSlow && CurrentFast<CurrentSlow)
	if (OrdersTotal()==0)
		OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*Point),Bid-(TakeProfit*Point),NULL,0,0,Red);


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.

Basic Principles - Trading Operations - MetaTrader 5
Basic Principles - Trading Operations - MetaTrader 5
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
Seng Joo Thio
1155
Seng Joo Thio  
Use the OrderClose() function.
OrderClose - Trade Functions - MQL4 Reference
OrderClose - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderClose - Trade Functions - MQL4 Reference
To add comments, please log in or register