Files
xMQL4/Docs/Samples/RSI Moving Average Cross EA.Tutorial.md
2024-01-25 04:03:36 +03:30

2.6 KiB

RSI MA Cross Strategy

EURUSD M15 Chart is Open

Strategy Used

  • 10 Period RSI:
    • ;
    • ;
  • 5 Period EMA;
  • 10 Period MA;

works by over solde and over baughts on rsi and then check mas;

Implementation

//
// Fast MA ...
input int fastMaPeriod = 20; // Fast MA Bars
input ENUM_MA_METHOD fastMaMethod = MODE_EMA; // Fast MA Method
input ENUM_APPLIED_PRICE fastMaAppliedPrice = PRICE_CLOSE; // Fast MA Applied Price

//
// Slow MA ...
input int slowMaPeriod = 50; // Slow MA Bars
input ENUM_MA_METHOD slowMaMethod = MODE_EMA; // Slow MA Method
input ENUM_APPLIED_PRICE slowMaAppliedPrice = PRICE_CLOSE; // Slow MA Applied Price

//
// SL / TP ...
input int stopLossPips = 50; // SL Pips
input int takeProfitPips = 50; // TP Pips

//
// Trade ...
intput double orderSize = 0.01; // Order size in Lots

//
// Buffers ...
double fastMaBuffer[];
double slowMaBuffer[];

const int buffersValuesRequired = 3;

//
// Global Variables ...
double stopLoss;
double takeProfit;

//
int OnInit() {
    //
    stopLoss = PipsToDouble(stopLossPips);
    takeProfit = PipsToDouble(takeProfitPips);

    //
    IsNewBar();
}

Calculating SL and TP Gaps on Trading Open

//
// Define Some Variables ...
double price;
double closePrice;
double tp = 0;
double sl = 0;
double slGap = PointsToDouble((int) SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL));

//
if (stopLoss > 0 && stopLoss < slGap) {
    return;
}

//
if (takeProfit > 0 && takeProfit < slGap) {
    return;
}

//
RefreshRates();

//
if (type == ORDER_TYPE_BUY) {
    //
    price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    closePrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    //
    if (stopLoss > 0) {
        sl = closePrice - stopLoss;
    }

    //
    if (takeProfit > 0) {
        tp = price + takeProfit;
    }
} else {
    //
    price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    closePrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

    //
    if (stopLoss > 0) {
        sl = closePrice + stopLoss;
    }

    //
    if (takeProfit > 0) {
        tp = price - takeProfit;
    }
}

//
price = NormalizeDouble(price, _Digits);
sl = NormalizeDouble(sl, _Digits);
tp = NormalizeDouble(tp, _Digits);

Usefull Library Methods

//
// Convert points to Actual Point ...
double PointsToDouble(int points) {
    //
    double result = points * _Point;
    return result;
}

//
// Converts Pips to Points ...
int PipsToPoints(int pips) {
    //
    int result = pips * ((_Digits == 3 || _Digits == 5) ? 10 : 1);
    return result;
}

//
// Converts Pips to Double ...
double PipsToDouble(int pips) {
    //
    double result = PointsToDouble(PipsToPoints(pips));
    return result;
}