70 lines
2.5 KiB
Text
70 lines
2.5 KiB
Text
//+------------------------------------------------------------------+
|
|
//| XAUUSD EMA20 Reversal Scalper |
|
|
//+------------------------------------------------------------------+
|
|
input int EMA_Period = 20;
|
|
input double TakeProfitPips = 40;
|
|
input double StopLossPips = 20;
|
|
input double LotSize = 0.1;
|
|
input ENUM_TIMEFRAMES TimeFrame = PERIOD_M5;
|
|
|
|
//--- Helper to get EMA
|
|
double GetEMA(int shift) {
|
|
return iMA(NULL, TimeFrame, EMA_Period, 0, MODE_EMA, PRICE_CLOSE, shift);
|
|
}
|
|
|
|
//--- Helper to calculate pips for XAUUSD (usually point = 0.01)
|
|
double PipValue() { return 0.10; }
|
|
|
|
//--- Check for simple reversal (bullish/bearish engulfing as sample)
|
|
bool IsReversal(int shift) {
|
|
double open1 = iOpen(NULL, TimeFrame, shift+1);
|
|
double close1 = iClose(NULL, TimeFrame, shift+1);
|
|
double open0 = iOpen(NULL, TimeFrame, shift);
|
|
double close0 = iClose(NULL, TimeFrame, shift);
|
|
|
|
// Bullish engulfing
|
|
if (close1 < open1 && close0 > open0 && close0 > open1 && open0 < close1)
|
|
return(true);
|
|
|
|
// Bearish engulfing
|
|
if (close1 > open1 && close0 < open0 && close0 < open1 && open0 > close1)
|
|
return(true);
|
|
|
|
return(false);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Main trading loop |
|
|
//+------------------------------------------------------------------+
|
|
void OnTick() {
|
|
// Only on new candles
|
|
static datetime lastCandle;
|
|
datetime currentCandle = iTime(NULL, TimeFrame, 0);
|
|
if (lastCandle == currentCandle) return;
|
|
lastCandle = currentCandle;
|
|
|
|
double ema = GetEMA(1);
|
|
double price = iClose(NULL, TimeFrame, 1);
|
|
|
|
// Price touch EMA
|
|
if (MathAbs(price - ema) <= PipValue()) {
|
|
if (IsReversal(1)) {
|
|
// Detect reversal direction
|
|
bool bullish = iClose(NULL, TimeFrame, 1) > iOpen(NULL, TimeFrame, 1);
|
|
bool bearish = iClose(NULL, TimeFrame, 1) < iOpen(NULL, TimeFrame, 1);
|
|
|
|
double sl, tp;
|
|
if (bullish) {
|
|
sl = price - StopLossPips * PipValue();
|
|
tp = price + TakeProfitPips * PipValue();
|
|
// Place Buy trade
|
|
trade.Buy(LotSize, NULL, price, sl, tp, NULL);
|
|
} else if (bearish) {
|
|
sl = price + StopLossPips * PipValue();
|
|
tp = price - TakeProfitPips * PipValue();
|
|
// Place Sell trade
|
|
trade.Sell(LotSize, NULL, price, sl, tp, NULL);
|
|
}
|
|
}
|
|
}
|
|
}
|