//+------------------------------------------------------------------+
//| BTCUSD & XAUUSD SIMPLE SCALPING EA |
//| Strategy: EMA 9/21 + RSI 14 + ATR 14 |
//| Platform: MetaTrader 5 |
//+------------------------------------------------------------------+
#property strict
#property version "1.01"
#include <Trade/Trade.mqh>
CTrade trade;
//--- Trading settings
input ENUM_TIMEFRAMES Timeframe = PERIOD_M5;
input double RiskPercent = 1.0;
input int FastEMA = 9;
input int SlowEMA = 21;
input int RSIPeriod = 14;
input int ATRPeriod = 14;
input double SL_ATR_Multiplier = 1.5;
input double TP_RR = 1.5;
input double BreakEvenRR = 1.0;
input int MaxSpreadPoints = 0;
input ulong MagicNumber = 20261205;
//--- Indicator handles
int fastHandle;
int slowHandle;
int rsiHandle;
int atrHandle;
datetime lastBar = 0;
//+------------------------------------------------------------------+
//| Initialization |
//+------------------------------------------------------------------+
int OnInit()
{
trade.SetExpertMagicNumber(MagicNumber);
trade.SetTypeFillingBySymbol(_Symbol);
fastHandle = iMA(_Symbol, Timeframe, FastEMA, 0,
MODE_EMA, PRICE_CLOSE);
slowHandle = iMA(_Symbol, Timeframe, SlowEMA, 0,
MODE_EMA, PRICE_CLOSE);
rsiHandle = iRSI(_Symbol, Timeframe, RSIPeriod,
PRICE_CLOSE);
atrHandle = iATR(_Symbol, Timeframe, ATRPeriod);
if(fastHandle == INVALID_HANDLE ||
slowHandle == INVALID_HANDLE ||
rsiHandle == INVALID_HANDLE ||
atrHandle == INVALID_HANDLE)
{
Print("Indicator initialization failed.");
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Release indicators |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(fastHandle);
IndicatorRelease(slowHandle);
IndicatorRelease(rsiHandle);
IndicatorRelease(atrHandle);
}
//+------------------------------------------------------------------+
//| Detect new candle |
//+------------------------------------------------------------------+
bool NewBar()
{
datetime currentBar = iTime(_Symbol, Timeframe, 0);
if(currentBar == 0)
return false;
if(currentBar != lastBar)
{
lastBar = currentBar;
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Check existing positions |
//+------------------------------------------------------------------+
bool HasPosition()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket > 0 &&
PositionGetString(POSITION_SYMBOL) == _Symbol &&
PositionGetInteger(POSITION_MAGIC) == (long)MagicNumber)
{
return true;
}
}
return false;
}
//+------------------------------------------------------------------+
//| Normalize lot size |
//+------------------------------------------------------------------+
double NormalizeVolume(double lots)
{
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
if(step <= 0)
return 0;
lots = MathFloor(lots / step) * step;
lots = MathMin(lots, maxLot);
if(lots < minLot)
return 0;
return NormalizeDouble(lots, 8);
}
//+------------------------------------------------------------------+
//| Calculate risk-based lot size |
//+------------------------------------------------------------------+
double CalculateLot(double stopDistance)
{
double equity = AccountInfoDouble(ACCOUNT_EQUITY);
double riskMoney = equity * RiskPercent / 100.0;
double tickSize =
SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double tickValue =
SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE_LOSS);
if(tickValue <= 0)
tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
if(tickSize <= 0 || tickValue <= 0 || stopDistance <= 0)
return 0;
double lossPerLot = (stopDistance / tickSize) * tickValue;
if(lossPerLot <= 0)
return 0;
return NormalizeVolume(riskMoney / lossPerLot);
}
//+------------------------------------------------------------------+
//| Break-even management |
//+------------------------------------------------------------------+
void ManageBreakEven()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0)
continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol ||
PositionGetInteger(POSITION_MAGIC) != (long)MagicNumber)
continue;
ENUM_POSITION_TYPE type =
(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
double openPrice =
PositionGetDouble(POSITION_PRICE_OPEN);
double sl = PositionGetDouble(POSITION_SL);
double tp = PositionGetDouble(POSITION_TP);
if(sl <= 0)
continue;
double currentPrice;
if(type == POSITION_TYPE_BUY)
currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
else
currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double initialRisk = MathAbs(openPrice - sl);
if(initialRisk <= 0)
continue;
double profitDistance;
if(type == POSITION_TYPE_BUY)
profitDistance = currentPrice - openPrice;
else
profitDistance = openPrice - currentPrice;
if(profitDistance >= initialRisk * BreakEvenRR)
{
double newSL = NormalizeDouble(
openPrice,
(int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS)
);
if(type == POSITION_TYPE_BUY && sl < newSL)
trade.PositionModify(ticket, newSL, tp);
if(type == POSITION_TYPE_SELL && sl > newSL)
trade.PositionModify(ticket, newSL, tp);
}
}
}
//+------------------------------------------------------------------+
//| Main trading function |
//+------------------------------------------------------------------+
void OnTick()
{
ManageBreakEven();
if(!NewBar())
return;
if(HasPosition())
return;
// Dynamic arrays - corrected
double fast[];
double slow[];
double rsi[];
double atr[];
ArrayResize(fast, 3);
ArrayResize(slow, 3);
ArrayResize(rsi, 3);
ArrayResize(atr, 3);
ArraySetAsSeries(fast, true);
ArraySetAsSeries(slow, true);
ArraySetAsSeries(rsi, true);
ArraySetAsSeries(atr, true);
// Copy indicator values
if(CopyBuffer(fastHandle, 0, 0, 3, fast) < 3)
return;
if(CopyBuffer(slowHandle, 0, 0, 3, slow) < 3)
return;
if(CopyBuffer(rsiHandle, 0, 0, 3, rsi) < 3)
return;
if(CopyBuffer(atrHandle, 0, 0, 3, atr) < 3)
return;
MqlTick tick;
if(!SymbolInfoTick(_Symbol, tick))
return;
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
if(point <= 0)
return;
// Spread filter
if(MaxSpreadPoints > 0)
{
double spread = (tick.ask - tick.bid) / point;
if(spread > MaxSpreadPoints)
return;
}
// Confirmed candle signals
bool buySignal =
fast[2] <= slow[2] &&
fast[1] > slow[1] &&
rsi[1] > 50;
bool sellSignal =
fast[2] >= slow[2] &&
fast[1] < slow[1] &&
rsi[1] < 50;
double slDistance = atr[1] * SL_ATR_Multiplier;
if(slDistance <= 0)
return;
double lots = CalculateLot(slDistance);
if(lots <= 0)
return;
int digits =
(int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
// BUY
if(buySignal)
{
double sl = NormalizeDouble(
tick.ask - slDistance, digits);
double tp = NormalizeDouble(
tick.ask + slDistance * TP_RR, digits);
bool result = trade.Buy(
lots,
_Symbol,
0,
sl,
tp,
"EMA RSI Scalping BUY"
);
if(!result)
Print("BUY failed: ", trade.ResultRetcodeDescription());
}
// SELL
if(sellSignal)
{
double sl = NormalizeDouble(
tick.bid + slDistance, digits);
double tp = NormalizeDouble(
tick.bid - slDistance * TP_RR, digits);
bool result = trade.Sell(
lots,
_Symbol,
0,
sl,
tp,
"EMA RSI Scalping SELL"
);
if(!result)
Print("SELL failed: ", trade.ResultRetcodeDescription());
}
}
//+------------------------------------------------------------------+