# Analyzing 20 day EMA/50 day SMA Source Code in MQL4

//This is a MQL4 EA for the‍ MT4 platform that will open trades when the 20​ day exponential moving average (EMA)
crosses the⁣ 50 day ⁣simple moving average (SMA).

//This EA will​ only open‍ trades when the moving averages cross.

//Variables for the EA
extern int movingAveragePeriod1 = 20;
extern int movingAveragePeriod2 =⁣ 50;
extern double Stop Loss = 50;
extern double ‍Take Profit‍ = 100;

//Here we define the⁣ expert advisor’s main logic
int start()
{⁤
//Define the timeframe to use
ResetLastError();
int timeframe⁣ =⁣ PERIOD_H1;

//Get a handle‌ for ⁢the moving averages
‌ ⁢int maHandle1 = iMA(NULL, timeframe, movingAveragePeriod1, 0, MODE_EMA, PRICE_CLOSE);
⁢ int maHandle2 = ⁤iMA(NULL, timeframe, movingAveragePeriod2, 0, MODE_SMA, PRICE_CLOSE);

//Declare⁢ some variable for the EA
double ma1 = 0;
double ma2 = 0;
bool sellSignal = False;

//Now get⁢ the moving ‌average values
ma1 = iMA(NULL, timeframe, movingAveragePeriod1,⁢ 0, MODE_EMA, PRICE_CLOSE, maHandle1);
ma2 = iMA(NULL, timeframe,‌ movingAveragePeriod2, 0, MODE_SMA, PRICE_CLOSE, maHandle2);

//Now we check the moving averages against each other
if(ma1 ⁢> ma2)
{⁤
}

if‌ (ma1 < ma2) { sellSignal = true; }//Once ​a signals ⁤has been ‌triggered, it will open ⁣position //in the desired direction, with the preset Stop Loss and //Take Profit⁣ levels if ​(buySignal == true) { ⁢ OrderSend(Symbol(), ⁤OP_BUY, 0.1,‍ Bid, 3, Stop Loss, Take Profit, "EA Triggered Entry", 0, Blue); }if (sellSignal == true) { ⁤OrderSend(Symbol(), OP_SELL, 0.1, Ask, 3, Stop Loss, Take Profit, "EA⁣ Triggered Entry", 0, Red); }//Finally, we return that⁢ all is ok return(0); }