RSI+BB+MA Scalping

 

Hello, 

In a trending market, I try to take trades during small counter-trend moves. For example, in an uptrend, the market moves down, then goes back up strongly, then pulls back slightly, and then goes up again. During these smaller pullbacks (counter-trend moves), I take buy trades in the direction of the larger trend. The same logic applies in a downtrend for sell trades.

I have created an MQL EA based on this logic. I need your help to verify and validate whether my code is correct. If there are any issues, please suggest how to fix them. Here is my EA code:

 Here is basic diagram. 



image


Code : 


   double RSI_Value = iRSI(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, 0);
   double BB_Upper_Resistance = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_UPPER, 0);
   double BB_Lower_Support = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_LOWER, 0);
   double BB_MiddleBand = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_MAIN, 0);
   double iMovingAverage = iMA(NULL, PERIOD_CURRENT, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
   //double iMovingAverage = iMA(NULL, PERIOD_CURRENT, 50, 0, MODE_EMA, PRICE_CLOSE, 0); - Ignore
   bool CandleGreen_S0 = iClose(NULL, PERIOD_CURRENT, 0) > iOpen(NULL, PERIOD_CURRENT, 0);

//Buy Trade
   if((RSI_Value < 30) &&
      Bid < BB_Lower_Support && Bid < BB_MiddleBand &&
      Bid > iMovingAverage && (iMovingAverage < BB_Lower_Support || iMovingAverage > BB_Upper_Resistance) &&
      CandleGreen_S0)
     {
      //Buy Order
     }

//Sell Trade
   if((RSI_Value > 70) &&
      Bid > BB_Upper_Resistance && Bid > BB_MiddleBand &&
      Bid < iMovingAverage && (iMovingAverage < BB_Lower_Support || iMovingAverage > BB_Upper_Resistance) &&
      !CandleGreen_S0)
     {
      //Sell Order
     }
 
anuj71:
Code : 

But this is only part of the code.😁

Since you've decided to share your developments, I suggest you attach an .mq4 file with the full code so that anyone (even non-programmers) can download and test it.

 
Vladislav Boyko #:

But this is only part of the code.😁

Since you've decided to share your developments, I suggest you attach an .mq4 file with the full code so that anyone (even non-programmers) can download and test it.

This is the main part of the code (the “Strategy”) that I explained in the image “Capturing smaller countertrend in a bigger trending market.” The EA is not completed and is still under development. I can only continue working on the EA when I know I am doing it correctly. I backtested this code and I feel something is missing — it’s not taking trades the way I want, as I described in my MS Paint diagram.

Can you help me fix this based on the given logic? I will share the .mq4 file once it is ready and I have some backtest results.

 
anuj71 #:
This is the main part of the code (the “Strategy”) that I explained in the image “Capturing smaller countertrend in a bigger trending market.” The EA is not completed and is still under development. I can only continue working on the EA when I know I am doing it correctly. I backtested this code and I feel something is missing — it’s not taking trades the way I want, as I described in my MS Paint diagram.

Ok, as you wish, I don’t insist. I just thought the people most enthusiastic about testing your code might be the ones who can't develop it themselves.

anuj71 #:
Can you help me fix this based on the given logic?

Unfortunately, I don't have time right now. But if you provide the rules for closing orders, then perhaps I will share my own implementation in a few days (although I make no promises).

 
Vladislav Boyko #:
if you provide the rules for closing orders

Closing with ATR (or) BollingerBand middle/opposite band (or) RSI Opposite Closing. - Any one is true, it will close the position (3 Ways to close)

ATR Code : 

extern string _ATR_TradeExit_ = "***ATR TP/SL/Trailing***";
extern int ATR_Period = 14;
extern int RiskToReward_TP = 1;
extern int RiskToReward_SL = 1;

fATRValue = iATR(NULL, PERIOD_CURRENT, ATR_Period, 0);
TakeProfit = fATRValue * RiskToReward_TP;
StopLoss = fATRValue * RiskToReward_SL;

RSI Closing (Opp direction): In counter-trend Buy generally open when RSI is Oversold, so it will close buy position when RSI is Overbought. 

for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol()))
           {
            string RSIClosingLevel = (OrderType() == OP_BUY) ? 70 : 30;

            double RSI_Value = iRSI(NULL, CandlePeriod, 14, PRICE_CLOSE, 0);

            if((OrderType() == OP_BUY && RSI_Value >= RSIClosingLevel) ||
               (OrderType() == OP_SELL && RSI_Value <= RSIClosingLevel))
              {
               ResetLastError();
               if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                  Print(__FUNCTION__, " => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
               else
                  Print(__FUNCTION__, " => Order ", OrderTicket(), " Successfully Closed");
              }
           }
        }
     }


BB Middle Band or Opposite band: When middle band is touched or Opposite band


enum BBClosing_enum {MiddleBand, BBBands};
extern BBClosing_enum BollingerBands_Closing = BBBands;

double BB_Upper_Resistance, BB_Lower_Support, BB_Middle;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol()))
           {

            if(BollingerBands_Closing == BBBands)
              {
               BB_Upper_Resistance = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_UPPER, 0);
               BB_Lower_Support = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_LOWER, 0);
              }
            if(BollingerBands_Closing == MiddleBand)
               BB_Middle = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_MAIN, 0);

            if((OrderType() == OP_BUY && ((BollingerBands_Closing == BBBands && Bid >= BB_Upper_Resistance) ||
                                          (BollingerBands_Closing == MiddleBand && Bid >= BB_Middle))) ||
               (OrderType() == OP_SELL && ((BollingerBands_Closing == BBBands && Bid <= BB_Lower_Support) ||
                                           (BollingerBands_Closing == MiddleBand && Bid <= BB_Middle))))
              {
               ResetLastError();
               if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                  Print(__FUNCTION__, " => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
               else
                  Print(__FUNCTION__, " => Order ", OrderTicket(), " Successfully Closed");
              }
           }
        }
     }



Vladislav Boyko #:
Ok, as you wish, I don’t insist. I just thought the people most enthusiastic about testing your code might be the ones who can't develop it themselves.

As the code is not fully completed and can have many logical error/bugs. i will share when at-least i have some development. and peoples are free to test my code. Maybe it takes more few more days to complete it like adding many option like trailing, signal strength, etc. 


Vladislav Boyko #:
I will share my own implementation in a few days (although I make no promises).


Thanks, i just need some guidance. 

 
anuj71 #:
Thanks, i just need some guidance. 

I'm afraid my code is unlikely to be useful to anyone (for study). Because I am sick with OOP; once I started writing in this paradigm, I can no longer write any other way.

So my implementation would be code that just works, but that few people can understand.

I have some developments for MT4 EA, but none of my code is publicly available. Considering that MT4 is going to die soon (I hope), I would like to share some example of my code just as a keepsake. I myself am not going to develop for MT4 anymore.