RSI Strategy Opening Trades

 

Hello!

I need some help. I have an MT5 EA working right now based on the RSI Strategy. It works very well, but I wanted to make a change and I can't get it to work.

There is only one position open at a time. Even though I have coded stoploss and take profit, I have them both with value 0.

My buy position opens when the 30 line is crossed and it closes when the 75 line is crossed above, but also, at the same time, my sell position opens as soon as the line is crossed and the buy position is closed.

What I want to do, is that once the buy position closes, it waits until the 75 line is crossed below to open a sell position. I have come to see that I am spending a lot of money on Swaps while waiting for it to cross below the line. Same goes for the buying position. 

I have tried to adjust it, but I can't get it to work and I honestly can't see a way to get it to work. Can you help me, please? Thank you!!

Here is my code for the opening positions


      // check for buy positions
      if(cntBuy==0 && bufferRSI[1]>=InpRSILowerLevel && bufferRSI[0]<InpRSILowerLevel){
      
         if(InpCloseSignal){if(!ClosePositions(2)){return;}}
         double sl = InpStopLoss==0 ? 0 : currentTick.bid - InpStopLoss * _Point;
         double tp = InpTakeProfit==0 ? 0 : currentTick.bid + InpTakeProfit * _Point;
         if(!NormalizePrice(sl)){return;}
         if(!NormalizePrice(tp)){return;}
         
         trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,InpLotsize,currentTick.ask,sl,tp,"RSI EA");
      }
      
      // check for sell position
      if(cntSell==0 && bufferRSI[1]<=InpRSIUpperLevel && bufferRSI[0]>InpRSIUpperLevel){
      
         if(InpCloseSignal){if(!ClosePositions(1)){return;}}
         double sl = InpStopLoss==0 ? 0 : currentTick.ask + InpStopLoss * _Point;
         double tp = InpTakeProfit==0 ? 0 : currentTick.ask - InpTakeProfit * _Point;
         if(!NormalizePrice(sl)){return;}
         if(!NormalizePrice(tp)){return;}

         trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,InpLotsize,currentTick.bid,sl,tp,"RSI EA");
       }


And this is my closing position function


// close positions
bool ClosePositions(int all_buy_sell){

   int total = PositionsTotal();
   for(int i=total-1; i>=0; i--){
      ulong ticket = PositionGetTicket(i);
      if(ticket<=0){Print("Failed to get position ticket"); return false;}
      if(!PositionSelectByTicket(ticket)){Print("Failed to select position"); return false;}
      long magic;
      if(!PositionGetInteger(POSITION_MAGIC,magic)){Print("Failed to get position magicnumbet"); return false;}
      if(magic==InpMagicnumber){
         long type;
         if(!PositionGetInteger(POSITION_TYPE,type)){Print("Failed to get position type");return false;}
         if(all_buy_sell==1 && type==POSITION_TYPE_SELL){continue;}
         if(all_buy_sell==2 && type==POSITION_TYPE_BUY){continue;}
         trade.PositionClose(ticket);
         if(trade.ResultRetcode() !=TRADE_RETCODE_DONE){
            Print("Failed to close position. ticket:",
                  (string)ticket," result:",(string)trade.ResultRetcode(),":",trade.CheckResultRetcodeDescription());
         }
       }
   }
   
   return true;
}  
 

The problem is in your entry logic,

// buy position
if(cntBuy==0 && bufferRSI[1]>=InpRSILowerLevel && bufferRSI[0]<InpRSILowerLevel)


//sell position
if(cntSell==0 && bufferRSI[1]<=InpRSIUpperLevel && bufferRSI[0]>InpRSIUpperLevel)

your enter sell position when previous bar is less then 75 and current bar is above 75 and the EA does what you code.

if you want to sell when RSI crosses below 75 the you must code it like this,

//buy position - crossing above from lower level
bufferRSI[1]<=InpRSILowerLevel && bufferRSI[0]>InpRSILowerLevel)

// sell position - crossing below from upper level
bufferRSI[1]>=InpRSIUpperLevel && bufferRSI[0]<InpRSIUpperLevel

you need to change both of your logic to make it more equal rather change only the sell position condition only

 
Luandre Ezra #:

The problem is in your entry logic,

// buy position
if(cntBuy==0 && bufferRSI[1]>=InpRSILowerLevel && bufferRSI[0]<InpRSILowerLevel)


//sell position
if(cntSell==0 && bufferRSI[1]<=InpRSIUpperLevel && bufferRSI[0]>InpRSIUpperLevel)

your enter sell position when previous bar is less then 75 and current bar is above 75 and the EA does what you code.

if you want to sell when RSI crosses below 75 the you must code it like this,

//buy position - crossing above from lower level
bufferRSI[1]<=InpRSILowerLevel && bufferRSI[0]>InpRSILowerLevel)

// sell position - crossing below from upper level
bufferRSI[1]>=InpRSIUpperLevel && bufferRSI[0]<InpRSIUpperLevel

you need to change both of your logic to make it more equal rather change only the sell position condition only

thank you so much!!

I will fix the code as suggested.

Again, thanks a lot!

Reason: