Need help with simple RSI buy Sell code

Hello Friend,

I am new to mt5 . I am learning mql5 coding.

My EA works like this


indicator RSI(14)

Buy = RSI cross 30 from bottom

Sell = RSI cross 70 from top

I need help to develop a code

  1. open buy position if there is any buy signal and close if any sell position is opened.

  2. If there is any sell signal it opens Sell position and close any Buy position if opened.

I would really help if someone could help me on this piece of code. I am using mt5

If you want help in the learning process we can help you if you share specific parts of the source code where you have doubts.

hello mrakey thanks a lot for your response.

I will share the code:

#include<Trade\Trade.mqh>

CTrade trade;

void OnTick()

{

string signal ="";

double  Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

double  Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

double myRSIArray[];

int myRSIDefinition = iRSI (_Symbol,_Period,14,PRICE_CLOSE);

ArraySetAsSeries(myRSIArray,true);

CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);

double myRSIValue=NormalizeDouble(myRSIArray[0],2);



if (myRSIValue < 30  ) signal ="buy";

if (myRSIValue > 70) signal ="sell";



if (signal =="buy" && PositionsTotal()<1)   //  >>>>> if RSI < 30 Buy signal is generated

trade.Buy(0.01,NULL);                // >>>> buy positioned is opened for .01 lot

/// Need Help on this part - when buy condition is met any Sell position should be closed and buy position should be opened for .01 lot

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------



if (signal =="sell" && PositionsTotal()<1)  // >>>>>>  if RSI > 70 Sell signal is generated

trade.Sell(0.01,NULL);    //  Sell positioned is opened for .01 lot

///Need Help on this Part -  when Sell condition is met any buy position should be closed and Sell positioned should be opened for .01 lot



Comment ("The current signal is: ",signal);

Thanks in advance.

did some research and tried PositionClose function. But i could not code how to close an existing position and open new position when condition of the indicator is reversed.

I would really appreciate if someone could help me with this piece of good. Thanks again.


#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

int pos=0;
int myRSIDefinition;

CTrade trade; // To send orders and close positions
CPositionInfo positionInfo; // to get info about current positions

int OnInit()
  {
// Define the handler once at EA Init, not every tick
   myRSIDefinition=iRSI(Symbol(),Period(),14,PRICE_CLOSE);
// If is not posible to get indicator handler print some error information and return with error code
   if(myRSIDefinition==INVALID_HANDLE)
     {
      Print("There was an error creating RSI handler!");
      return(INIT_FAILED);
     }
   return INIT_SUCCEEDED;
  }

void OnTick()
  {
   string signal="";
   double  Ask=NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),Digits());
   double  Bid=NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),Digits());
   double myRSIArray[];
   ArraySetAsSeries(myRSIArray,true);
   CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);
   double myRSIValue=NormalizeDouble(myRSIArray[0],2);
   if(myRSIValue<30)
      signal="buy";
   if(myRSIValue>70)
      signal="sell";

   if(signal=="buy")
     {
      //First close all Sell positions
      for(pos=0; pos<PositionsTotal(); pos++)
        {
         //Select the position to load info
         if(positionInfo.SelectByIndex(pos))
           {
            // Get the position type, if sell then close it
            if(positionInfo.PositionType()==POSITION_TYPE_SELL)
              {
               trade.PositionClose(positionInfo.Ticket());
              }
           }
        }
      trade.Buy(0.01,Symbol());
     Comment ("The current signal is: ",signal);
     }

   if(signal=="sell")
     {
      //First close all Buy positions
      for(pos=0; pos<PositionsTotal(); pos++)
        {
         //Select the position to load info
         if(positionInfo.SelectByIndex(pos))
           {
            // Get the position type, if buy then close it
            if(positionInfo.PositionType()==POSITION_TYPE_BUY)
              {
               trade.PositionClose(positionInfo.Ticket());
              }
           }
        }
      trade.Sell(0.01,Symbol());
     Comment ("The current signal is: ",signal);
     }
     


  }

I hope this is what you need to fix.

This is a start, maybe you want to add other conditions.

Look at the comments, i hope you can understand me.

This is what i wanted. Awesome work. Thanks a lot Edwin. One last help if you don’t mind

It is opening lots of buy sell positions. Is there any way to limit the positions opened ? For example if i want to have only 1 or 2 positions open.