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.

If you want someone to create a code for you, you must create an order in the Freelance section: https://www.mql5.com/en/job

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • www.mql5.com
i need the service of Forex expert to create Trade copier with the following function Fully automated copying  Super fast and precise copying  Support both Provider and Receiver  Support only  Local   Support both Trade and Signal mode  Multi Providers and Receivers allowed  Support unlimited local receivers  Allow set subscription expiry for...
 

hello Edwin 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. 

 

I 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.

 
rkv007: I will share the code
Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor
 
#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.

PS: If you share code, use code button (Alt+S), this helps to read code in a better way

 
William Roeder:
Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor
Sure i was not aware of it. I will edit the code. Thanks
 
Edwin Hernando Artunduaga Quiñonez:

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.

PS: If you share code, use code button (Alt+S), this helps to read code in a better way

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.

 
hi
 

Hello Friend,

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

My EA works like this

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

indicator DeMarker(14)

Buy = DeMarke cross 0,3 from  bottom

Sell = DeMarke cross 0,7 from top

I need help to develop a code buy and sell

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 mt

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Ismail Ihtiyaroglu #:

Hello Friend,

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

My EA works like this

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

indicator DeMarker(14)

Buy = DeMarke cross 0,3 from  bottom

Sell = DeMarke cross 0,7 from top

I need help to develop a code buy and sell

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 mt

iDeMarker zone exit

iDeMarker zone exit

Рис. 1. iDeMarker zone exit

Reason: