XAUUSD_MACD_RSI_Bot/XAUUSD_MACD_RSI_EA.mq5
2025-11-11 17:00:48 +00:00

179 lines
No EOL
5.3 KiB
MQL5

//+------------------------------------------------------------------+
//| XAUUSD MACD RSI Bot EA |
//| Algorithmic Trading Strategy (MQL5) |
//| For Gold (XAUUSD) |
//+------------------------------------------------------------------+
#property strict
#property description "XAUUSD Trading Bot with MACD and RSI indicators"
#property version "1.0"
// Trading Parameters
input double FixedLotSize = 0.1; // Fixed lot size
input int StopLossPips = 50; // Stop loss in pips
input int TakeProfitPips = 100; // Take profit in pips
input int MagicNumber = 201125; // EA magic number
// MACD Parameters
input int FastMA = 12; // Fast moving average
input int SlowMA = 26; // Slow moving average
input int SignalMA = 9; // Signal line MA
input ENUM_APPLIED_PRICE MacdPrice = PRICE_CLOSE; // MACD applied price
// RSI Parameters
input int RSIPeriod = 14; // RSI period
input ENUM_APPLIED_PRICE RsiPrice = PRICE_CLOSE; // RSI applied price
// Indicator handles
int macdHandle;
int rsiHandle;
//+------------------------------------------------------------------+
void OnInit()
{
// Create MACD indicator
macdHandle = iMACD(Symbol(), Period(), FastMA, SlowMA, SignalMA, MacdPrice);
if(macdHandle == INVALID_HANDLE)
{
Print("Failed to create MACD handle");
return;
}
// Create RSI indicator
rsiHandle = iRSI(Symbol(), Period(), RSIPeriod, RsiPrice);
if(rsiHandle == INVALID_HANDLE)
{
Print("Failed to create RSI handle");
return;
}
Print("XAUUSD MACD RSI Bot Started Successfully");
}
//+------------------------------------------------------------------+
void OnTick()
{
// Get MACD values
double macdMain[2];
double macdSignal[2];
if(CopyBuffer(macdHandle, 0, 0, 2, macdMain) < 0 ||
CopyBuffer(macdHandle, 1, 0, 2, macdSignal) < 0)
{
Print("Error copying MACD data");
return;
}
// Get RSI value
double rsiValue[2];
if(CopyBuffer(rsiHandle, 0, 0, 2, rsiValue) < 0)
{
Print("Error copying RSI data");
return;
}
// Get current price information
double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
// Only one trade at a time
if(CountOrders() > 0) return;
// BUY SIGNAL: MACD crosses above signal line
if(macdMain[1] <= macdSignal[1] && macdMain[0] > macdSignal[0] && rsiValue[0] < 50)
{
OpenBuyOrder(ask, bid, FixedLotSize);
}
// SELL SIGNAL: MACD crosses below signal line
if(macdMain[1] >= macdSignal[1] && macdMain[0] < macdSignal[0] && rsiValue[0] > 50)
{
OpenSellOrder(ask, bid, FixedLotSize);
}
}
//+------------------------------------------------------------------+
int CountOrders()
{
int count = 0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
if(PositionSelectByTicket(PositionGetTicket(i)))
{
if(PositionGetInteger(POSITION_MAGIC) == MagicNumber &&
PositionGetString(POSITION_SYMBOL) == Symbol())
count++;
}
}
return count;
}
//+------------------------------------------------------------------+
void OpenBuyOrder(double ask, double bid, double volume)
{
double sl = bid - (StopLossPips * Point());
double tp = ask + (TakeProfitPips * Point());
MqlTradeRequest request = {};
request.action = TRADE_ACTION_DEAL;
request.symbol = Symbol();
request.volume = volume;
request.type = ORDER_TYPE_BUY;
request.price = ask;
request.sl = sl;
request.tp = tp;
request.magic = MagicNumber;
request.comment = "XAUUSD MACD RSI Buy";
MqlTradeResult result = {};
if(OrderSend(request, result))
{
Print("Buy order opened. Ticket: ", result.order, " Price: ", ask);
}
else
{
Print("Buy order failed. Error: ", GetLastError());
}
}
//+------------------------------------------------------------------+
void OpenSellOrder(double ask, double bid, double volume)
{
double sl = ask + (StopLossPips * Point());
double tp = bid - (TakeProfitPips * Point());
MqlTradeRequest request = {};
request.action = TRADE_ACTION_DEAL;
request.symbol = Symbol();
request.volume = volume;
request.type = ORDER_TYPE_SELL;
request.price = bid;
request.sl = sl;
request.tp = tp;
request.magic = MagicNumber;
request.comment = "XAUUSD MACD RSI Sell";
MqlTradeResult result = {};
if(OrderSend(request, result))
{
Print("Sell order opened. Ticket: ", result.order, " Price: ", bid);
}
else
{
Print("Sell order failed. Error: ", GetLastError());
}
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Release indicator handles
if(macdHandle != INVALID_HANDLE)
IndicatorRelease(macdHandle);
if(rsiHandle != INVALID_HANDLE)
IndicatorRelease(rsiHandle);
Print("XAUUSD MACD RSI Bot Stopped");
}
//+------------------------------------------------------------------+