Trying to build a simple RSI EA

To add comments, please log in or register
Ekvationen
66
Ekvationen  
Trying to develop a simple Expert Advisor with RSI functionality but it does not seem to work, in addition to this, I wonder how I limit the number of trades to one at a time.
Feel free to improve and correct the EA below.

// import the trade library
#include<Trade\Trade.mqh>

// create an instance of CTrade called trade
CTrade trade;

void OnTick()
  {
      // Get the ask price
      double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      double myRSIValue = iRSI (_Symbol,_Period,14,PRICE_CLOSE);
      
      // Open some buy positions to have something to close
      if ((PositionsTotal()<1)&&myRSIValue > 30)
         {
            trade.Buy(0.10,NULL,Ask,0,(Ask+100 * _Point),NULL);
         }
      
      // if we have a profit close all positions
      if (myRSIValue > 70)
         {
            CloseAllPositions();
         }
  }
  
void CloseAllPositions()
   {
      // from the number of positions count down to zero
      for(int i=PositionsTotal()-1; i>=0; i--) // look at all positions
      {
         // get the ticket number from the current position
         int ticket=PositionGetTicket(i);
         // Close the current position
         trade.PositionClose(i);
      }
   }
Thanks in advance, Ekvationen
William Roeder
20228
William Roeder  
Ekvationen: Trying to develop a simple Expert Advisor with RSI functionality but it does not seem to work, in addition to this, I wonder how I limit the number of trades to one at a time.
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here and our crystal balls are cracked.

  2. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum
Lakshan Perera
9849
Lakshan Perera  
Ekvationen:
Trying to develop a simple Expert Advisor with RSI functionality but it does not seem to work, in addition to this, I wonder how I limit the number of trades to one at a time.
Feel free to improve and correct the EA below.

Thanks in advance, Ekvationen

Use a flag,

// import the trade library
#include<Trade\Trade.mqh>

// create an instance of CTrade called trade
CTrade trade;

void OnTick()
  {
      static bool flag = false; //static means that the Boolean variable declared will not be declared and set to false on every Tick.
        
      // Get the ask price
      double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      double myRSIValue = iRSI (_Symbol,_Period,14,PRICE_CLOSE);
      
      // Open some buy positions to have something to close
      if ((PositionsTotal()<1)&&myRSIValue > 30)
         {
           if( flag == false ) // Order will be opened only if the flag == false
           {
            flag = true; // once it has been opened set the flag to true, in this way, it will not open any trades over and over again..                        
            trade.Buy(0.10,NULL,Ask,0,(Ask+100 * _Point),NULL);
           }    
         }
        else
         {
           flag = false; //if the RSI condition mentioned above become false then, the flag set to false, so more orders can be opened..        
         }
      
      // if we have a profit close all positions
      if (myRSIValue > 70)
         {
            CloseAllPositions();
         }
  }
  
void CloseAllPositions()
   {
      // from the number of positions count down to zero
      for(int i=PositionsTotal()-1; i>=0; i--) // look at all positions
      {
         // get the ticket number from the current position
         int ticket=PositionGetTicket(i);
         // Close the current position
         trade.PositionClose(i);
      }
   }

Ask me if anything is not clear.

To add comments, please log in or register