# A Slightly Complex EMA Crossover Forex Robot For MT4 - GoTrade4me

Forex Robot Strategy Rules:

Long Entry: When candle crosses Above the EMA(Standalone) and subsequent candle closes above previous candle which crossed above EMA(Standalone). Then buy at the opening of 3rd candle.

Long Exit: Exit happens with Short Entry.

Short Entry: When candle crosses Below the EMA(Standalone) and subsequent candle closes below previous candle which crossed below EMA(Standalone). Then sell at the opening of 3rd candle.

Short Exit: Exit happens with Long Entry.

#include <StdLib.mqh>

input int Period = 14;
input double StopLoss = 50;
input double TakeProfit = 100;

int init()
{
// Initialize the robot
return(0);
}

void deinit()
{
// Deinitialize the robot
}

int start()
{
// Calculate the EMA value for the current symbol and period
double ema = iMA(NULL, 0, Period, 0, MODE_EMA, PRICE_CLOSE, 0);

// Get the current open, high, low, and close prices
double open = iOpen(NULL, 0, 1);
double high = iHigh(NULL, 0, 1);
double low = iLow(NULL, 0, 1);
double close = iClose(NULL, 0, 1);

// Check if the current candle crosses above the EMA
if (open < ema && close > ema)
{
// Check if the subsequent candle closes above the previous candle
if (iClose(NULL, 0, 2) > iClose(NULL, 0, 3))
{
// Place a buy trade at the opening of the 3rd candle
OrderSend(Symbol(), OP_BUY, 0.01, iOpen(NULL, 0, 3), 3, 0, StopLoss, TakeProfit, “Buy”, MagicNumber, 0, Blue);
}
}

// Check if the current candle crosses below the EMA
if (open > ema && close < ema)
{
// Check if the subsequent candle closes below the previous candle
if (iClose(NULL, 0, 2) < iClose(NULL, 0, 3))
{
// Place a sell trade at the opening of the 3rd candle
OrderSend(Symbol(), OP_SELL, 0.01, iOpen(NULL, 0, 3), 3, 0, StopLoss, TakeProfit, “Sell”, MagicNumber, 0, Red);
}
}

return(0);
}