Reversal Strategy - How to define StopLoss

shanmugapradeep

Master Trader
Dec 18, 2020
180
16
54
40
Hello,





I am using simple RSI + BollingerBands to find reversal and take a trade. i am not using any Take Profit or StopLoss defined directly in Order or defined in Point/Pips instead i am using Indicator to calculate When to close trade in profit (Take Profit similar) but the problem is i am confused about when to close the trade in loss (Stop Loss Similar).


Entry :


MQL4:
//fRSI, fBands is seperate function
double RSI_Value = fRSI(rSymbol, SelectedTimeFrame, RSI_Period, PRICE_CLOSE, 1);
double BB_UpperBand_ResistanceLine = fBands(rSymbol, SelectedTimeFrame, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_UPPER, 1);
double BB_LowerBand_SupportLine = fBands(rSymbol, SelectedTimeFrame, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_LOWER, 1);
double HighPrice = iHigh(rSymbol, SelectedTimeFrame, 1);
double LowPrice = iLow(rSymbol, SelectedTimeFrame, 1);
 
if(RSI_Value < RSI_BelowLevel && LowPrice < BB_LowerBand_SupportLine)
      NewOrderSend();
else
if(RSI_Value > RSI_AboveLevel && HighPrice > BB_UpperBand_ResistanceLine)
NewOrderSend();

Take Profit (With Indicator) :


MQL4:
void TPClosing()
  {
   double BB_Upper_Resistance=0, BB_Lower_Support=0, BB_Middle=0;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderMagicNumber() == Magic)
           {
            ENUM_TIMEFRAMES CandlePeriod = GetTimeframeFromComment(OrderComment());
            double fBID = SymbolInfoDouble(OrderSymbol(),SYMBOL_BID);
 
            //RSI Closing (Take Profit)
            if(RSI_Closing)
              {
               double RSI_Value = fRSI(OrderSymbol(), CandlePeriod, RSI_Period, PRICE_CLOSE, 1);
               if(
                  (OrderType() == OP_BUY && fBID > OrderOpenPrice() && RSI_Value >= RSI_AboveLevel) ||
                  (OrderType() == OP_SELL && fBID < OrderOpenPrice() && RSI_Value <= RSI_BelowLevel)
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => RSI => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => RSI => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //BB Closing (Take Profit)
            if(BollingerBands_Closing != BB_Deactivated)
              {
               if(BollingerBands_Closing == BBBands)
                 {
                  BB_Upper_Resistance = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_UPPER, 1);
                  BB_Lower_Support = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_LOWER, 1);
                 }
               if(BollingerBands_Closing == MiddleBand)
                  BB_Middle = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_MAIN, 1);
 
               if(
                  (OrderType() == OP_BUY && fBID > OrderOpenPrice() && ((BollingerBands_Closing == BBBands && fBID >= BB_Upper_Resistance) ||
                        (BollingerBands_Closing == MiddleBand && fBID >= BB_Middle))) ||
 
                  (OrderType() == OP_SELL && fBID < OrderOpenPrice() && ((BollingerBands_Closing == BBBands && fBID <= BB_Lower_Support) ||
                        (BollingerBands_Closing == MiddleBand && fBID <= BB_Middle)))
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => BB => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => BB => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
              }

Take profit working amazing.


StopLoss (With Indicator) : ??? - I am unable to understand this part. when should i close the trade at loss?. Need suggestion.
 
Last edited by a moderator:
Just remove the profit condition on those indicator checks and it will be closing your trades based on the indicator values irrespective of whether it is to take profit or to stop a loss.
I have already tried but it disaster and failed my EA.

For example,

RSI > 70 (Take Sell Trade), RSI went 69 and again went to 70, it will close the Sell Trade. Total Disaster.

I also tried, RSI > 70 (Take Sell Trade) and RSI > 80 (Loss Sell trade as SL) but several time RSI Does not touch 80 even with bigger drag-down and sometime RSI Touch 80 with just very minimal draw-down
 
I have already tried but it disaster and failed my EA.

For example,

RSI > 70 (Take Sell Trade), RSI went 69 and again went to 70, it will close the Sell Trade. Total Disaster.

I also tried, RSI > 70 (Take Sell Trade) and RSI > 80 (Loss Sell trade as SL) but several time RSI Does not touch 80 even with bigger drag-down and sometime RSI Touch 80 with just very minimal draw-down
I don't think that's how it is supposed to work. If you take a Sell trade when RSI > 70, you close it when RSI gets into a Buy territory (RSI < 30), not when it goes back up to 70.
 
I don't think that's how it is supposed to work. If you take a Sell trade when RSI > 70, you close it when RSI gets into a Buy territory (RSI < 30), not when it goes back up to 70.
This is exactly what i am doing for Take Profit but what about StopLoss. i am confused about how to setup Stop Loss
 
You set to to the RSI < 30 condition, but without the required profit condition - that will be your stop-loss.
I already tried this but result was not expected. I have tried adding SL. here is my full code. Any suggestion or correction

MQL4:
void TPSLClosing()
  {
   double BB_Upper_Resistance = 0, BB_Lower_Support = 0, BB_Middle = 0, iCloseValue, RSI_Value = 0;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderMagicNumber() == Magic)
           {
            ENUM_TIMEFRAMES CandlePeriod = GetTimeframeFromComment(OrderComment());
            double fBID = SymbolInfoDouble(OrderSymbol(),SYMBOL_BID);
 
            //RSI Calculation
            if(RSI_TP || RSI_SL)
               RSI_Value = fRSI(OrderSymbol(), CandlePeriod, RSI_Period, PRICE_CLOSE, 1);
 
            //RSI Closing (Take Profit)
            if(RSI_TP)
              {
               if(
                  (OrderType() == OP_BUY && fBID > OrderOpenPrice() && RSI_Value >= RSI_AboveLevel) ||
                  (OrderType() == OP_SELL && fBID < OrderOpenPrice() && RSI_Value <= RSI_BelowLevel)
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => RSI (TP) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => RSI (TP) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //RSI Closing (Stop Loss)
            if(RSI_SL)
              {
               if(
                  (OrderType() == OP_BUY && fBID < OrderOpenPrice() && RSI_Value < RSI_BelowLevel+5) ||
                  (OrderType() == OP_SELL && fBID > OrderOpenPrice() && RSI_Value > RSI_AboveLevel+5)
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => RSI (SL) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => RSI (SL) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //BB Calculation
            if(BollingerBandsSL || BollingerBands_Closing == BBBands)
              {
               BB_Upper_Resistance = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_UPPER, 1);
               BB_Lower_Support = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_LOWER, 1);
              }
 
            //BB Closing (Take Profit)
            if(BollingerBands_Closing != BB_Deactivated)
              {
               if(BollingerBands_Closing == MiddleBand)
                  BB_Middle = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_MAIN, 1);
 
               if(
                  (OrderType() == OP_BUY && fBID > OrderOpenPrice() && ((BollingerBands_Closing == BBBands && fBID >= BB_Upper_Resistance) ||
                        (BollingerBands_Closing == MiddleBand && fBID >= BB_Middle))) ||
 
                  (OrderType() == OP_SELL && fBID < OrderOpenPrice() && ((BollingerBands_Closing == BBBands && fBID <= BB_Lower_Support) ||
                        (BollingerBands_Closing == MiddleBand && fBID <= BB_Middle)))
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => BB (TP) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => BB (TP) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //BB Closing (Stop Loss)
            if(BollingerBandsSL)
              {
               iCloseValue = iClose(OrderSymbol(), CandlePeriod, 1);
               if(
                  (OrderType() == OP_BUY && fBID < OrderOpenPrice() && iCloseValue < BB_Lower_Support) ||
                  (OrderType() == OP_SELL && fBID > OrderOpenPrice() && iCloseValue > BB_Upper_Resistance)
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => BB (SL) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => BB (SL) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //Candle Exit (Take Profit)
            if(CandleExitTP > 0)
              {
               int openCandleIndex = iBarShift(OrderSymbol(), CandlePeriod, OrderOpenTime(), true);
 
               // Check if it's time to close the order
               if(openCandleIndex >= CandleExitTP && ((OrderType() == OP_BUY && fBID > OrderOpenPrice()) || (OrderType() == OP_SELL && fBID < OrderOpenPrice())))
                 {
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => CandleExit (TP) ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => CandleExit (TP) ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //Candle Exit (Stop Loss)
            if(CandleExitSL > 0)
              {
               // find bar index of order open
               int openBar = iBarShift(OrderSymbol(), CandlePeriod, OrderOpenTime(), true);
               if(openBar >= CandleExitSL)
                 {
                  bool GreenCandle = true;
                  bool RedCandle   = true;
 
                  // check candles AFTER entry
                  for(int c = openBar - 1; c >= openBar - CandleExitSL; c--)
                    {
                     double OpenS  = iOpen(rSymbol, CandlePeriod, c);
                     double CloseS = iClose(rSymbol, CandlePeriod, c);
 
                     if(CloseS <= OpenS)
                        GreenCandle = false; // not bullish
                     if(CloseS >= OpenS)
                        RedCandle   = false; // not bearish
 
                     if(!GreenCandle && !RedCandle)
                        break;
                    }
 
                  // Closing
                  if((OrderType() == OP_BUY && fBID < OrderOpenPrice() && RedCandle) || (OrderType() == OP_SELL && fBID > OrderOpenPrice() && GreenCandle))
                    {
                     ResetLastError();
                     if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                        Print(__FUNCTION__, " => CandleExit (SL) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                     else
                       {
                        Print(__FUNCTION__, " => CandleExit (SL) => Order ", OrderTicket(), " Successfully Closed");
                        continue;
                       }
                    }
                 }
              }
 
            //Moving Average (SL)
            if(MovingAverage_Closing)
              {
               double MovingAverageValue = fMA(OrderSymbol(), CandlePeriod, MovingAverage_Period, 0, MovingAverage_Method, MovingAverage_AppliedPrice, 1);
               if((OrderType() == OP_BUY && fBID < OrderOpenPrice() && fBID < MovingAverageValue) || (OrderType() == OP_SELL && fBID > OrderOpenPrice() && fBID > MovingAverageValue))
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => Moving Average (SL) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => Moving Average (SL) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
           }
        }
     }
  }


All the Take Profit code working like amazing but StopLoss still not giving reasonable result.
 
Last edited:
Not sure why you are using +5 on both RSI levels for stop-loss. If you wanted to SL trigger a little bit earlier, then it makes sense to change it to -5 for one of them. I'm a little confused. What are your RSI_AboveLevel and RSI_BelowLevel values?
 
Not sure why you are using +5 on both RSI levels for stop-loss. If you wanted to SL trigger a little bit earlier, then it makes sense to change it to -5 for one of them. I'm a little confused. What are your RSI_AboveLevel and RSI_BelowLevel values?
Yes, it my mistake. I noticed it yesterday and already fixed it. for RSI level values are default for AboveLevel 70 and Below Level 30. I have also made several changes since yesterday and i made some good StopLoss system but still it have several flaw.

Here is updated code :

MQL4:
void TPSLClosing()
  {
   double BB_Upper_Resistance = 0, BB_Lower_Support = 0, BB_Middle = 0, iCloseValue, fBID, RSI_Value = 0;
   int iBarShiftCandle;
   bool inProfit, inLoss;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderMagicNumber() == Magic)
           {
            ENUM_TIMEFRAMES CandlePeriod = GetTimeframeFromComment(OrderComment());
            fBID = SymbolInfoDouble(OrderSymbol(),SYMBOL_BID);
            iCloseValue = iClose(OrderSymbol(), CandlePeriod, 1);
            iBarShiftCandle = iBarShift(OrderSymbol(), CandlePeriod, OrderOpenTime(), true);
            inProfit = OrderProfit() > 0;
            inLoss = OrderProfit() < 0;
 
            //RSI Calculation
            if(RSI_TP || RSI_SL)
               RSI_Value = fRSI(OrderSymbol(), CandlePeriod, RSI_Period, PRICE_CLOSE, 1);
 
            //RSI Closing (Take Profit)
            if(RSI_TP && inProfit)
              {
               if(
                  (OrderType() == OP_BUY && RSI_Value >= RSI_AboveLevel) ||
                  (OrderType() == OP_SELL && RSI_Value <= RSI_BelowLevel)
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => RSI (TP) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => RSI (TP) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //RSI Closing (Stop Loss)
            if(RSI_SL && CoolDownCandle > 0 && iBarShiftCandle >= CoolDownCandle && inLoss)
              {
               if(
                  (OrderType() == OP_BUY && RSI_Value < RSI_BelowLevel-2) ||
                  (OrderType() == OP_SELL && RSI_Value > RSI_AboveLevel+2)
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => RSI (SL) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => RSI (SL) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //BB Calculation
            if(BollingerBandsSL || BollingerBands_Closing == BBBands)
              {
               BB_Upper_Resistance = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_UPPER, 1);
               BB_Lower_Support = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_LOWER, 1);
              }
 
            //BB Closing (Take Profit)
            if(BollingerBands_Closing != BB_Deactivated && inProfit)
              {
               if(BollingerBands_Closing == MiddleBand)
                  BB_Middle = fBands(OrderSymbol(), CandlePeriod, BollingerBands_Period, BollingerBands_deviation, 0, PRICE_CLOSE, MODE_MAIN, 1);
 
               if(
                  (OrderType() == OP_BUY && ((BollingerBands_Closing == BBBands && fBID >= BB_Upper_Resistance) ||
                                             (BollingerBands_Closing == MiddleBand && fBID >= BB_Middle))) ||
 
                  (OrderType() == OP_SELL && ((BollingerBands_Closing == BBBands && fBID <= BB_Lower_Support) ||
                                              (BollingerBands_Closing == MiddleBand && fBID <= BB_Middle)))
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => BB (TP) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => BB (TP) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //BB Closing (Stop Loss)
            if(BollingerBandsSL && CoolDownCandle > 0 && iBarShiftCandle >= CoolDownCandle && inLoss)
              {
               if(
                  (OrderType() == OP_BUY && iCloseValue < BB_Lower_Support) ||
                  (OrderType() == OP_SELL && iCloseValue > BB_Upper_Resistance)
               )
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => BB (SL) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => BB (SL) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
 
            //Candle Exit (Take Profit)
            if(CandleExitTP > 0 && iBarShiftCandle >= CandleExitTP && inProfit && (OrderType() == OP_BUY  || OrderType() == OP_SELL))
              {
               if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                  Print(__FUNCTION__, " => CandleExit (TP) ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
               else
                 {
                  Print(__FUNCTION__, " => CandleExit (TP) ", OrderTicket(), " Successfully Closed");
                  continue;
                 }
              }
 
 
            //Candle Exit (Stop Loss)
            if(CandleExitSL > 0 && iBarShiftCandle >= CandleExitSL && inLoss)
              {
               // find bar index of order open
               bool GreenCandle = true;
               bool RedCandle   = true;
 
               // check candles AFTER entry
               for(int c = iBarShiftCandle - 1; c >= iBarShiftCandle - CandleExitSL; c--)
                 {
                  double OpenS  = iOpen(OrderSymbol(), CandlePeriod, c);
                  double CloseS = iClose(OrderSymbol(), CandlePeriod, c);
 
                  if(CloseS <= OpenS)
                     GreenCandle = false; // not bullish
                  if(CloseS >= OpenS)
                     RedCandle   = false; // not bearish
 
                  if(!GreenCandle && !RedCandle)
                     break;
                 }
 
               // Closing
               if((OrderType() == OP_BUY && RedCandle) || (OrderType() == OP_SELL && GreenCandle))
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => CandleExit (SL) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => CandleExit (SL) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
 
              }
 
            //Moving Average (SL)
            if(MovingAverage_Closing && CoolDownCandle > 0 && iBarShiftCandle >= CoolDownCandle && inLoss)
              {
               double MovingAverageValue = fMA(OrderSymbol(), CandlePeriod, MovingAverage_Period, 0, MovingAverage_Method, MovingAverage_AppliedPrice, 1);
               if((OrderType() == OP_BUY && iCloseValue < MovingAverageValue) || (OrderType() == OP_SELL && iCloseValue > MovingAverageValue))
                 {
                  ResetLastError();
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
                     Print(__FUNCTION__, " => Moving Average (SL) => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError());
                  else
                    {
                     Print(__FUNCTION__, " => Moving Average (SL) => Order ", OrderTicket(), " Successfully Closed");
                     continue;
                    }
                 }
              }
           }
        }
     }
  }