//+------------------------------------------------------------------+
//| GoldScalpingEA.mq5 |
//| Gold Scalping Trading EA |
//+------------------------------------------------------------------+
#property copyright "Claude Assistant"
#property version "1.00"
#property description "Scalping trading EA specifically for XAUUSD, fixed lot size high-frequency trading"
#include <Trade\Trade.mqh>
CTrade trade;
//--- Input parameters
input double LotSize = 0.02; // Fixed lot size
input int StopLoss = 50; // Stop loss points
input int TakeProfit = 30; // Take profit points
input int StartHour = 21; // Start trading time (hours)
input int StartMinute = 30; // Start trading time (minutes)
input int EndHour = 23; // End trading time (hours)
input int EndMinute = 20; // End trading time (minutes)
input int RSI_Period = 14; // RSI period
input int MA_Fast = 5; // Fast moving average line
input int MA_Slow = 20; // Slow moving average line
input double RSI_Oversold = 30; // RSI oversold level
input double RSI_Overbought = 70; // RSI overbought level
input int MinutesAfterTrade = 2; // Wait minutes after trade
//--- Global variables
int rsi_handle;
int ma_fast_handle;
int ma_slow_handle;
datetime last_trade_time = 0;
bool can_trade = true;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Check trading variety
if(Symbol() != "XAUUSD")
{
Print("Warning: This EA is specifically designed for XAUUSD");
}
// Initialize technical indicators
rsi_handle = iRSI(Symbol(), PERIOD_M1, RSI_Period, PRICE_CLOSE);
ma_fast_handle = iMA(Symbol(), PERIOD_M1, MA_Fast, 0, MODE_SMA, PRICE_CLOSE);
ma_slow_handle = iMA(Symbol(), PERIOD_M1, MA_Slow, 0, MODE_SMA, PRICE_CLOSE);
if(rsi_handle == INVALID_HANDLE || ma_fast_handle == INVALID_HANDLE || ma_slow_handle == INVALID_HANDLE)
{
Print("Technical indicator initialization failed");
return INIT_FAILED;
}
Print("Gold scalping EA started successfully");
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(rsi_handle);
IndicatorRelease(ma_fast_handle);
IndicatorRelease(ma_slow_handle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check trading time
if(!IsTradeTime())
{
can_trade = true; // Reset trading permission
return;
}
// Check if trading is allowed
if(!can_trade)
return;
// Check if enough time has passed since the last trade
if(TimeCurrent() - last_trade_time < MinutesAfterTrade * 60)
return;
// Get technical indicator data
double rsi_value = GetRSIValue();
double ma_fast = GetMAValue(ma_fast_handle);
double ma_slow = GetMAValue(ma_slow_handle);
if(rsi_value == 0 || ma_fast == 0 || ma_slow == 0)
return;
// Get current price
double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
// Check current positions
if(PositionsTotal() == 0)
{
// Buy signal: RSI oversold + fast line crosses above slow line
if(rsi_value < RSI_Oversold && ma_fast > ma_slow)
{
OpenBuyOrder(ask);
}
// Sell signal: RSI overbought + fast line crosses below slow line
else if(rsi_value > RSI_Overbought && ma_fast < ma_slow)
{
OpenSellOrder(bid);
}
}
else
{
// Check if need to close positions
CheckForClose();
}
}
//+------------------------------------------------------------------+
//| Check if within trading time |
//+------------------------------------------------------------------+
bool IsTradeTime()
{
MqlDateTime dt;
TimeCurrent(dt);
int current_time = dt.hour * 60 + dt.min;
int start_time = StartHour * 60 + StartMinute;
int end_time = EndHour * 60 + EndMinute;
return (current_time >= start_time && current_time <= end_time);
}
//+------------------------------------------------------------------+
//| Get RSI value |
//+------------------------------------------------------------------+
double GetRSIValue()
{
double rsi[];
if(CopyBuffer(rsi_handle, 0, 0, 1, rsi) <= 0)
return 0;
return rsi[0];
}
//+------------------------------------------------------------------+
//| Get moving average value |
//+------------------------------------------------------------------+
double GetMAValue(int handle)
{
double ma[];
if(CopyBuffer(handle, 0, 0, 1, ma) <= 0)
return 0;
return ma[0];
}
//+------------------------------------------------------------------+
//| Open long order |
//+------------------------------------------------------------------+
void OpenBuyOrder(double price)
{
double sl = price - StopLoss Point() 10;
double tp = price + TakeProfit Point() 10;
if(trade.Buy(LotSize, Symbol(), price, sl, tp, "Scalping Buy"))
{
Print("Buy order executed successfully Price:", price, " Stop Loss:", sl, " Take Profit:", tp);
last_trade_time = TimeCurrent();
can_trade = false; // Temporarily prohibit trading
}
}
//+------------------------------------------------------------------+
//| Open short order |
//+------------------------------------------------------------------+
void OpenSellOrder(double price)
{
double sl = price + StopLoss Point() 10;
double tp = price - TakeProfit Point() 10;
if(trade.Sell(LotSize, Symbol(), price, sl, tp, "Scalping Sell"))
{
Print("Sell order executed successfully Price:", price, " Stop Loss:", sl, " Take Profit:", tp);
last_trade_time = TimeCurrent();
can_trade = false; // Temporarily prohibit trading
}
}
//+------------------------------------------------------------------+
//| Check closing conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
if(PositionSelectByTicket(PositionGetTicket(i)))
{
if(PositionGetString(POSITION_SYMBOL) == Symbol())
{
double profit = PositionGetDouble(POSITION_PROFIT);
ENUM_POSITION_TYPE pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
// Quick take profit logic
if(profit > 5.0) // If profit exceeds 5 dollars, close immediately
{
trade.PositionClose(PositionGetTicket(i));
Print("Quick take profit closing, profit:", profit);
can_trade = true; // Allow new trades
return;
}
// RSI reversal signal closing
double rsi_current = GetRSIValue();
if(pos_type == POSITION_TYPE_BUY && rsi_current > RSI_Overbought)
{
trade.PositionClose(PositionGetTicket(i));
Print("Buy order RSI reversal closing");
can_trade = true;
}
else if(pos_type == POSITION_TYPE_SELL && rsi_current < RSI_Oversold)
{
trade.PositionClose(PositionGetTicket(i));
Print("Sell order RSI reversal closing");
can_trade = true;
}
}
}
}
}
//+------------------------------------------------------------------+
//| Trade event handling |
//+------------------------------------------------------------------+
void OnTrade()
{
// After trade execution, re-allow trading
if(PositionsTotal() == 0)
{
can_trade = true;
}
}