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

2 min read

As a forex ​trader, mastering‌ the technical aspect of the craft ⁣ can be tricky. Utilizing technical indicators to maximize returns is a tried‍ and true method among experienced traders. One of the most powerful tools to‌ have in your trader’s toolkit is the 20⁤ day exponential moving⁤ average (EMA) crossover of the 50 day simple moving ‌average (SMA). This indicator can be used to identify key breakpoints for entries and exits, with‌ the ​potential⁣ to increase⁤ profits and minimize risk. The purpose of this article is to discuss‍ the ‍source code for a Metatrader⁤ 4 expert advisor (EA)⁣ based on ‍20 day EMA crossover of 50⁣ day SMA in the forex market.⁢ //————//

//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 buySignal = False;
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)
{⁤
buySignal = true;
}

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); }

You May Also Like

More From Author