//+------------------------------------------------------------------+
//| NATHANIEL SMART TRADER EA v1.0 |
//| MetaTrader 5 - MQL5 |
//| EMA + RSI + ATR + Risk Management |
//+------------------------------------------------------------------+
#property copyright "Nathaniel"
#property version "1.00"
#property strict
#include <Trade/Trade.mqh>
CTrade trade;
//--- Trading settings
input double RiskPercent = 1.0;
input double RiskReward = 2.0;
input int FastEMA = 20;
input int SlowEMA = 50;
input int RSIPeriod = 14;
input int ATRPeriod = 14;
input double ATRMultiplier = 2.0;
input int MaxSpreadPoints = 30;
input ulong MagicNumber = 20261128;
input int SlippagePoints = 20;
//--- Indicator handles
int fastHandle, slowHandle, rsiHandle, atrHandle;
datetime lastBarTime = 0;
//+------------------------------------------------------------------+
//| Initialization |
//+------------------------------------------------------------------+
int OnInit()
{
trade.SetExpertMagicNumber(MagicNumber);
trade.SetDeviationInPoints(SlippagePoints);
fastHandle = iMA(_Symbol,_Period,FastEMA,0,MODE_EMA,PRICE_CLOSE);
slowHandle = iMA(_Symbol,_Period,SlowEMA,0,MODE_EMA,PRICE_CLOSE);
rsiHandle = iRSI(_Symbol,_Period,RSIPeriod,PRICE_CLOSE);
atrHandle = iATR(_Symbol,_Period,ATRPeriod);
if(fastHandle == INVALID_HANDLE ||
slowHandle == INVALID_HANDLE ||
rsiHandle == INVALID_HANDLE ||
atrHandle == INVALID_HANDLE)
{
Print("Indicator initialization failed.");
return INIT_FAILED;
}
Print("Nathaniel Smart Trader EA initialized.");
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Release indicators |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(fastHandle);
IndicatorRelease(slowHandle);
IndicatorRelease(rsiHandle);
IndicatorRelease(atrHandle);
}
//+------------------------------------------------------------------+
//| Calculate position size based on risk |
//+------------------------------------------------------------------+
double CalculateLot(double entry, double stopLoss,
ENUM_ORDER_TYPE orderType)
{
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
double riskMoney = balance * RiskPercent / 100.0;
double profit = 0;
if(!OrderCalcProfit(orderType,_Symbol,1.0,
entry,stopLoss,profit))
return 0;
double lossPerLot = MathAbs(profit);
if(lossPerLot <= 0)
return 0;
double lot = riskMoney / lossPerLot;
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;
lot = MathFloor(lot / step) * step;
lot = MathMin(lot,maxLot);
if(lot < minLot)
return 0;
int digits = 0;
double temp = step;
while(temp < 1.0 && digits < 8)
{
temp *= 10.0;
digits++;
}
return NormalizeDouble(lot,digits);
}
//+------------------------------------------------------------------+
//| Check if symbol already has an open position |
//+------------------------------------------------------------------+
bool HasOpenPosition()
{
return PositionSelect(_Symbol);
}
//+------------------------------------------------------------------+
//| Main trading function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check spread
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
if(ask <= 0 || bid <= 0)
return;
double spread = (ask-bid)/_Point;
if(spread > MaxSpreadPoints)
return;
// Only one open position per symbol
if(HasOpenPosition())
return;
// Trade once per new candle
datetime currentBar = iTime(_Symbol,_Period,0);
if(currentBar == 0 || currentBar == lastBarTime)
return;
lastBarTime = currentBar;
// Indicator buffers
double fast[], slow[], rsi[], atr[];
ArraySetAsSeries(fast,true);
ArraySetAsSeries(slow,true);
ArraySetAsSeries(rsi,true);
ArraySetAsSeries(atr,true);
if(CopyBuffer(fastHandle,0,0,3,fast) < 3 ||
CopyBuffer(slowHandle,0,0,3,slow) < 3 ||
CopyBuffer(rsiHandle,0,0,3,rsi) < 3 ||
CopyBuffer(atrHandle,0,0,3,atr) < 3)
return;
// Use completed candles
double fastPrevious = fast[2];
double fastCurrent = fast[1];
double slowPrevious = slow[2];
double slowCurrent = slow[1];
double currentRSI = rsi[1];
double currentATR = atr[1];
if(currentATR <= 0)
return;
int digits = (int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
// BUY condition: bullish EMA crossover + RSI confirmation
bool buySignal =
fastPrevious <= slowPrevious &&
fastCurrent > slowCurrent &&
currentRSI > 50;
// SELL condition: bearish EMA crossover + RSI confirmation
bool sellSignal =
fastPrevious >= slowPrevious &&
fastCurrent < slowCurrent &&
currentRSI < 50;
// BUY ORDER
if(buySignal)
{
double entry = ask;
double sl = NormalizeDouble(
entry - currentATR*ATRMultiplier,digits);
double tp = NormalizeDouble(
entry + (entry-sl)*RiskReward,digits);
double lot = CalculateLot(entry,sl,ORDER_TYPE_BUY);
if(lot > 0)
{
if(!trade.Buy(lot,_Symbol,0,sl,tp,
"Nathaniel Smart BUY"))
Print("BUY failed: ",trade.ResultRetcodeDescription());
}
}
// SELL ORDER
if(sellSignal)
{
double entry = bid;
double sl = NormalizeDouble(
entry + currentATR*ATRMultiplier,digits);
double tp = NormalizeDouble(
entry - (sl-entry)*RiskReward,digits);
double lot = CalculateLot(entry,sl,ORDER_TYPE_SELL);
if(lot > 0)
{
if(!trade.Sell(lot,_Symbol,0,sl,tp,
"Nathaniel Smart SELL"))
Print("SELL failed: ",trade.ResultRetcodeDescription());
}
}
}
//+------------------------------------------------------------------+