A Simple Forex Robot Based On EMA Crossover - GoTrade4me


Forex Robot Rules:

Long Entry: When Fast EMA crosses Above the Slow EMA.

Long Exit: Exit happens with Short Entry.

Short Entry: When Fast EMA Crosses Below the Slow EMA.

Short Exit: Exit happens with Long Entry.

#include <StdLib.mqh>

input int FastEMA = 8;
input int SlowEMA = 21;
input double StopLoss = 50;
input double TakeProfit = 100;

double lots = 0.01;
int buyOrder = -1;
int sellOrder = -1;

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

void deinit()
{
// Deinitialize the robot
}

int start()
{
// Calculate the fast and slow exponential moving average values
double fastEMA = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, 0);
double slowEMA = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, 0);

// Check if the fast EMA crosses above the slow EMA
if (fastEMA > slowEMA && sellOrder < 0)
{
// Place a long trade with the calculated lot size and specified stop loss and take profit values
buyOrder = OrderSend(Symbol(), OP_BUY, lots, Bid, 3, 0, StopLoss, TakeProfit, “Buy”, MagicNumber, 0, Blue);
}

// Check if the fast EMA crosses below the slow EMA
if (fastEMA < slowEMA && buyOrder < 0)
{
// Place a short trade with the calculated lot size and specified stop loss and take profit values
sellOrder = OrderSend(Symbol(), OP_SELL, lots, Ask, 3, 0, StopLoss, TakeProfit, “Sell”, MagicNumber, 0, Red);
}

// Check if the buy order is still open
if (buyOrder >= 0)
{
// Check if the fast EMA crosses below the slow EMA
if (fastEMA < slowEMA)
{
// Close the buy order
OrderClose(buyOrder, lots, Bid, 3, Red);
buyOrder = -1;
}
}

// Check if the sell order is still open
if (sellOrder >= 0)
{
// Check if the fast EMA crosses above the slow EMA
if (fastEMA > slowEMA)
{
// Close the sell order
OrderClose(sellOrder, lots, Ask, 3, Blue);
sellOrder = -1;
}
}

return(0);
}

This robot uses the iMA function to calculate the fast and slow exponential moving average values for the specified symbol, period, and settings. The FastEMA and SlowEMA inputs are used to specify the periods for the fast and slow EMAs. The StopLoss and TakeProfit inputs are used to specify the stop loss and take profit values for the trades.

The robot checks if the fast EMA crosses above or below the slow EMA, and if so, it places a long or short trade using the OrderSend function. The MagicNumber variable is used to identify the trades placed by this robot, so that it can manage them correctly.

Leave a Reply