Issue with programming EA

Back to topics list To post a new topic, please log in or register
avatar
3
Theekop 2016.06.16 23:35
 


Hey all!

 I recently started programming my very first EA. It may look a little confusing at first but i hope it is readable. Th reason for this post is that i have encountered a problem. It comes down to this. I want to execute a buy order once 3 requirement are met. 

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
extern int    Period_MA1 = 10;
extern int    Period_MA2 = 20;
extern int    Period_MA3 = 40;
extern int    Period_MA4 = 47;
extern double vol        = 1;
extern int    slip       = 3;
extern int    Maxord     = 3;
extern double SLV        = 0.0002;
extern int    Period_RSI = 14;

double StopLevel;
double SL;
double StopMin;
double TP;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD);
   Alert("Stoplevel =",StopLevel);
   StopMin=StopLevel*0.00001;
   return(0);
  }
//--------------------------------------------------------------------------// 
int start() // Special function start()
  {
   int total=OrdersTotal();
   if(total<Maxord)
     {
      double MA1;
      double MA2;
      double MA3;
      double MA4;
      double RSI;
      //--------------------------------------------------------------------------// Tech. ind. function call

      MA1=iMA(NULL,PERIOD_M5,Period_MA1,0,MODE_EMA,PRICE_CLOSE,0);
      MA2=iMA(NULL,PERIOD_M5,Period_MA2,0,MODE_EMA,PRICE_CLOSE,0);
      MA3=iMA(NULL,PERIOD_M5,Period_MA3,0,MODE_EMA,PRICE_CLOSE,0);
      MA4=iMA(NULL,PERIOD_M5,Period_MA4,0,MODE_EMA,PRICE_CLOSE,0);
      RSI=iRSI(NULL,0,Period_RSI,PRICE_CLOSE,0);
      //--------------------------------------------------------------------------// Condition 1: Moving Average Trend
      if((MA3<MA1) && (MA3<MA2) && (MA4<MA1) && (MA4<MA2)) 
        {
         Alert("Condition 1 fulfilled");
         //--------------------------------------------------------------------------// Condition 2: RSI
         if(RSI<30)
           {
            Alert("Condition 2 fulfilled");
            //--------------------------------------------------------------------------// Condition 3: Candlesticks
            if(iHigh(Symbol(),PERIOD_CURRENT,0)<iLow(Symbol(),PERIOD_CURRENT,1))
              {
               Alert("Condition 3 fulfilled");
               //--------------------------------------------------------------------------// Calculate StopLoss & TakeProfit value      
               SL=iLow(Symbol(),PERIOD_CURRENT,0)-SLV;
               Alert("SL =",SL);

               if(StopMin>(Ask-SL))
                 {
                  SL=Ask-StopMin;
                  Alert("Stoploss too small");
                 }
               TP=Ask+SL;
               //--------------------------------------------------------------------------// Place Order           
               int ticket=OrderSend(Symbol(),OP_BUY,vol,Ask,slip,SL,TP);
               Alert("Orders Placed!");
              }
           }
        }
      //--------------------------------------------------------------------------// End of Buy Order
     }
   return(0);
//--------------------------------------------------------------------------// Exit start()
  }
//+------------------------------------------------------------------+

 The problem here is that my EA is not accurately assessing condition 3. I want my EA to recognize that when a candlestick closes with a high that is lower than the low of the previous candlestick, it is supposed to send in an order. However, when the described situation arose in a test the EA did not recognize it as such. See the picture attached for a clearer example. Any advice on what is going wrong here?  Again, i apologize for the mess that is my code.

 Thanks in advance! 

 

 

Example 


avatar
2017
gooly 2016.06.16 23:42 #
 

I would store the condition in three (four) variables, like:

double h = iHigh(Symbol(),PERIOD_CURRENT,0)<iLow(Symbol(),PERIOD_CURRENT,1),
       l = iLow(Symbol(),PERIOD_CURRENT,1);
bool cond_1 = (MA3<MA1) && (MA3<MA2) && (MA4<MA1) && (MA4<MA2),
     cond_2 = RSI<30,
     cond_3 = h<l
     cond_4 = cond_1 && cond_2 && cond_3;

and track them either in the debugger or - in case if visual testing - show them in the Comment();


avatar
1197
FMIC 2016.06.17 00:17 #
 
Theekop:


... condition 3. I want my EA to recognize that when a candlestick closes with a high that is lower than the low of the previous candlestick ...

The key here is that you wrote "when it closes", but the candle at index 0 is the CURRENT UN-CLOSED candle bar. When it closes it becomes the candle at index 1.

So instead of comparing candle [0] and candle [1], you should be comparing candle [1] with candle [2].

if( High[1] < Low[2] ) { ... }

// "High[1]" is the same as "iHigh(Symbol(),PERIOD_CURRENT,1)"
// "Low[2]"  is the same as "iLow( Symbol(),PERIOD_CURRENT,2)"

The same applies to the Moving Averages, you should be looking at the closed candle [1] and not on the current candle [0]. However, you seem to be using a different TimeFrame (namely M5) for the Moving Averages - is that correct?

Also, be careful how you calculate the stop-loss where you might have to look at both candle [0] and candle [1] to get the lowest of the two.

avatar
3
Theekop 2016.06.17 07:42 #
 

Thanks a lot for your suggestions. Do you happen to know these details are explained more thoroughly? The explanation in the reference manual does not mention the information you gave me. The different Timeframe for the MA's is used to detect whether there is a upwards trend, and M5 is more suitable for that.  

avatar
1197
FMIC 2016.06.17 08:07 #
 
Theekop:

Thanks a lot for your suggestions. Do you happen to know these details are explained more thoroughly? The explanation in the reference manual does not mention the information you gave me. The different Timeframe for the MA's is used to detect whether there is a upwards trend, and M5 is more suitable for that.  

The information I gave you is originally from the documentation. I just gave you a concise aggregated explanation of information originally scattered among the various elements involved.

Back to topics list  

To add comments, please log in or register