Download MetaTrader 5

Trouble With A Basic RSI EA?

To add comments, please log in or register
Toby
15
Toby  

Hi, my first post here.

I'm very new to trading although i've been in the gambling industry for a long time. I've just started coding with MQL4 and have been testing myself by writing basic functions, etc.

I have this code and can't get it to take any trades in strategy tester? Hoping someone is kind enough to show me where my obvious elementary mistakes are, thanks.

In the simplest of terms, the EA should open Buy trades when RSI crosses above 50, and open Sell trades when it crosses below 50. what am I missing?

//+------------------------------------------------------------------+
//|                                                Strategy #001.mq4 |
//|                                                         forex_tb |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "forex_tb"
#property link      ""

extern double LotSize=0.1;
extern int RSI_Period=14;
extern double RSI_Barrier=50;
extern int TakeProfit=100;
extern int StopLoss=25;
extern int MagicNumber=1;

double point;
int pips;



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (ticksize == 0.00001 || point == 0.001)
   pips = ticksize*10;
   else pips = ticksize;
   
//----
   return (0);
   }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
     
   if(IsNewCandle()) CheckForRSITrade();  
//----
   return(0);
   }
//+------------------------------------------------------------------+

bool IsNewCandle()
   {
   static int BarsOnChart=0;
   if(Bars==BarsOnChart)
   return(false);
   BarsOnChart=Bars;
   return(true);
   }

//+------------------------------------------------------------------+

void OrderEntry(int direction)
   {
   if(direction==0)
      { 
      if(OpenOrdersThisPair(Symbol())==0)
         OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Red);
      }
   
   if(direction==1)
      {
      if(OpenOrdersThisPair(Symbol())==0)
         OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,MagicNumber,0,Red); //int sellticket=
      }                                                                                                                                                                                                 
   }

//+------------------------------------------------------------------+
int OpenOrdersThisPair (string pair)
   {
   int total=0;
      for(int i=OrdersTotal()-1;i>=0;i--)
         {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==pair) total++;
         }
         return(total);
   }   

//+------------------------------------------------------------------+
void CheckForRSITrade()
   {
   double rsi.Now = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,1);   
   double rsi.Then = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,2);   
   if(rsi.Now>RSI_Barrier && rsi.Then<RSI_Barrier) OrderEntry(0);
   if(rsi.Now<RSI_Barrier && rsi.Then>RSI_Barrier) OrderEntry(1);
   }

whroeder1
16564
whroeder1  
forex_tb: can't get it to take any trades in strategy tester?
Find out why. What are Function return values ? How do I use them ? - MQL4 forum
Keith Watford
Moderator
10541
Keith Watford  

One problem is that your variable pips will always be zero

OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Red);

This means that you are trying to place trades with SL and TP at the same price as the entry

Put this in a script and see if the result is what you expect

int pips;
pips = 0.00001*10;
Alert(pips);
pips = 0.0001;
Alert(pips);
Toby
15
Toby  

GumRai & WHRoeder, thank you both. Simple things I'm currently overlooking such as the data type.

I've changed the "pip" variable to a double.

To add comments, please log in or register