Hello guys, I’m new to coding EA. I wanna create an EA that can buy when all three moving averages are crossed over to the upside and the distance between the FastMA (21 EMA) and SlowMA(50EMA) is at least 50 pips. The opposite is the same for selling condition. Another problem is that how do I code so that the price will buy at the FastMA(Whenever the price touches the FastMA) when all the conditions are met?
extern int TakeProfit=30;
extern int StopLoss=20;
extern double LotSize=0.01;
extern int FastMA=21;
extern int FastMaShift=0;
extern int FastMaMethod=1;
extern int FastMaAppliedTo=0;
extern int SlowMA=50;
extern int SlowMaShift=0;
extern int SlowMaMethod=1;
extern int SlowMaAppliedTo=0;
extern int SlowMA2=200;
extern int SlowMa2Shift=0;
extern int SlowMa2Method=1;
extern int SlowMa2AppliedTo=0;
extern int MagicNumber=1234;
int pipsMABuy,pipsMASell;
double pips;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
if(ticksize==0.00001 || ticksize==0.001)
pips=ticksize*10;
else pips=ticksize;
return (0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(IsNewCandle())CheckForMaTrade();
return(0);
}
//+------------------------------------------------------------------+
//insuring its a new candle function
//+------------------------------------------------------------------+
bool IsNewCandle()
{
static int BarsOnChart=0;
if(Bars==BarsOnChart)
return (false);
BarsOnChart=Bars;
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CheckForMaTrade()
{
double PreviousFast= iMA(NULL,0,FastMA,FastMaShift,FastMaMethod,FastMaAppliedTo,2);
double CurrentFast = iMA(NULL,0,FastMA,FastMaShift,FastMaMethod,FastMaAppliedTo,1);
double PreviousSlow= iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,2);
double CurrentSlow = iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);
double PreviousSlow2= iMA(NULL,0,SlowMA2,SlowMa2Shift,SlowMa2Method,SlowMa2AppliedTo,2);
double CurrentSlow2 = iMA(NULL,0,SlowMA2,SlowMa2Shift,SlowMa2Method,SlowMa2AppliedTo,1);
pipsMABuy=(int)MathFloor(MathAbs(CurrentFast-CurrentSlow)/(_Point));
pipsMASell=(int)MathFloor(MathAbs(CurrentSlow-CurrentFast)/(_Point));
if(PreviousFast<PreviousSlow && PreviousFast<PreviousSlow2 && PreviousSlow<PreviousSlow2)
if(CurrentFast>CurrentSlow && CurrentFast>CurrentSlow2 && CurrentSlow>CurrentSlow2)
if(pipsMABuy>=50) OrderEntry(0);
if(PreviousFast>PreviousSlow && PreviousFast>PreviousSlow2 && PreviousSlow>PreviousSlow2)
if(CurrentFast<CurrentSlow && CurrentFast<CurrentSlow2 && CurrentSlow<CurrentSlow2)
if(pipsMABuy>=50) OrderEntry(1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OrderEntry(int direction)
{
if(direction==0)
{
if(StopLoss== 0) double bsl =0;
else bsl = Ask-(StopLoss*pips);
if(TakeProfit==0) double btp=0;
else btp=Ask+(TakeProfit*pips);
if(OpenOrdersThisPair(Symbol())==0) int buyticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,20,0,0,NULL,MagicNumber,0,clrGreen);
if(buyticket>0) OrderModify(buyticket,OrderOpenPrice(),bsl,btp,0,clrNONE);
}
if(direction==1)
{
if(StopLoss== 0) double ssl =0;
else ssl = Bid+(StopLoss*pips);
if(TakeProfit==0) double stp=0;
else stp=Bid-(TakeProfit*pips);
if(OpenOrdersThisPair(Symbol())==0) int sellticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,20,0,0,NULL,MagicNumber,0,clrRed);
if(sellticket>0) OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,clrNONE);
}
}
//+------------------------------------------------------------------+
//checks to see if any orders open on this currency pair.
//+------------------------------------------------------------------+
int OpenOrdersThisPair(string pair)
{
int total=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==pair) total++;
}
return (total);
}
//+------------------------------------------------------------------+