Expert Advisor - EMA for open and RSI for close

[Deleted]  

Hello,

I have decided to start learning MQL4 and I have created my first Expert Advisor. I just wonder if you can give me any feedback, comments if my approach is reasonable or if there are no unnoticed glitches in the code I have created. Anyway, I would appreciate comments :).

extern double Lots = 0.3;
extern string OpenModeString = "======= OPEN MODE =======";
extern int    OpenMode = 1;
extern string OpenMode01String = "======= OPEN MODE 1 =======";
extern int    FirstEMA = 9;
extern int    SecondEMA = 18;
extern string CloseModeString = "======= CLOSE MODE =======";
extern int    CloseMode = 1;
extern string CloseMode01String = "======= CLOSE MODE 1 =======";
extern int    PeriodRSI = 9;

int start()
{
   if (OpenMode == 1) // page 185
   {
      // Calculate values of indicators
      double PreviousFirstEMA = iMA(NULL, 0, FirstEMA, 0, MODE_EMA, PRICE_CLOSE, 1);
      double PreviousSecondEMA = iMA(NULL, 0, SecondEMA, 0, MODE_EMA, PRICE_CLOSE, 1);
      double CurrentFirstEMA = iMA(NULL, 0, FirstEMA, 0, MODE_EMA, PRICE_CLOSE, 0);
      double CurrentSecondEMA = iMA(NULL, 0, SecondEMA, 0, MODE_EMA, PRICE_CLOSE, 0);
   
      // Open long or short position
      int ticket;
      if (PreviousFirstEMA < PreviousSecondEMA && CurrentFirstEMA > CurrentSecondEMA)
      { // Open long
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Long order",16384,0,CLR_NONE);
         if(ticket<0)
         {
            Print("OrderSend failed with error #",GetLastError());
            return(0);
         }
      }
      if (PreviousFirstEMA > PreviousSecondEMA && CurrentFirstEMA < CurrentSecondEMA)
      { // Open short
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Ask,3,0,0,"Long order",16384,0,CLR_NONE);
         if(ticket<0)
         {
            Print("OrderSend failed with error #",GetLastError());
            return(0);
         }
      }
   }
   
   // Close long or short position
   if (CloseMode == 1) // page 193
   {
      // Calculate RSI
      double CurrentRSI = iRSI(NULL,0,PeriodRSI,PRICE_CLOSE,0);
      double PreviousClose = iClose(NULL,0,1);
      double CurrentClose = iClose(NULL,0,0);
      
      // Check if we should close long or short positions
      bool CloseAllBuy = false;
      bool CloseAllSell = false;
      if (CurrentRSI > 75 && PreviousClose < CurrentClose)
      { // Close long when RSI > 75% and PRICE_CLOSE(now) < PRICE_CLOSE(previous)
         CloseAllBuy = true;
      }
      if (CurrentRSI < 25 && PreviousClose > CurrentClose)
      { // Close short when RSI < 25% and PRICE_CLOSE(now) > PRICE_CLOSE(previous)
         CloseAllSell = true;
      }
      
      // Close all long or short positions
      int total = OrdersTotal();
      for (int i = total - 1; i >= 0; i--)
      {
         OrderSelect(i, SELECT_BY_POS);
         int type = OrderType();
         bool result = false;
         switch (type)
         {
            case OP_BUY:
               if (CloseAllBuy == true)
               {
                  result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, CLR_NONE);
                  if (result < 0)
                     Print("OrderClose (Buy) failed with error #",GetLastError());
                  return(0);
               }
               break;
            case OP_SELL:
               if (CloseAllSell == true)
               {
                  result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 0, CLR_NONE);
                  if (result < 0)
                     Print("OrderClose (Buy) failed with error #",GetLastError());
                  return(0);
               }
               break;
         }
      }
   }
}

I have decided to try the following approach - I create several different ways of opening positions and several ways of closing those. Currently I do not care about stop loss or take profit. I check every combination of opening and closing conditions, e.g. 1st method of opening vs 1st method of closing, then 1st open vs 2nd close, 1st open vs 3rd close, 2nd open vs 1st close, 2nd open vs 2nd close and so on. (Currently I have only one opening and one closing condition). I use the same date range, i.e. 1.1.2001 - 1.1.2013, the same pair EURUSD, the same timeframe M15, the same slippage (18 with disconnected MT4), always Open Prices Only mode.

At the beginning I would like to check opening and closing logics as described in "Technical Traders Guide to Computer Analysis of the Futures Market" by Charles LeBeau & David W. Lucas. Then I will move to more complex things but first of all I need to master basics.

Best regards!

 
johnyjj2:

Hello,

I have decided to start learning MQL4 and I have created my first Expert Advisor. I just wonder if you can give me any feedback, comments if my approach is reasonable or if there are no unnoticed glitches in the code I have created. Anyway, I would appreciate comments :).

I have decided to try the following approach - I create several different ways of opening positions and several ways of closing those. Currently I do not care about stop loss or take profit. I check every combination of opening and closing conditions, e.g. 1st method of opening vs 1st method of closing, then 1st open vs 2nd close, 1st open vs 3rd close, 2nd open vs 1st close, 2nd open vs 2nd close and so on. (Currently I have only one opening and one closing condition). I use the same date range, i.e. 1.1.2001 - 1.1.2013, the same pair EURUSD, the same timeframe M15, the same slippage (18 with disconnected MT4), always Open Prices Only mode.

At the beginning I would like to check opening and closing logics as described in "Technical Traders Guide to Computer Analysis of the Futures Market" by Charles LeBeau & David W. Lucas. Then I will move to more complex things but first of all I need to master basics.

Best regards!

Check your OrderSelect() has worked,  if it hasn't your other trading function calls in your OrderClose() will be garbage.

If CloseAllBuy and CloseAllSell are both false why bother running the for loop ?  you can use OrderClosePrice()  ( after a successful OrderSelect() ) for the close price of an OP_BUY or OP_SELL then you don't need to distinguish between them . . .

Slippage is not simulated in the Strategy Tester