Moving Average Retest EA

 

Hi guys


I'm developing a simple EA which is based on two Moving Averages (50 and 150). I made the code already for the MA break but now i need another condition, where the EA should only open a trade when the FMA gets retested.


Does anyone have an idea how I can code this? I uploaded a picture when you can see the trade the EA should open (after retesting the FMA). I developed with MQL4 :-)


   double SMA1    = iMA(Symbol(),PERIOD_CURRENT,150,0,MODE_SMA,PRICE_CLOSE,0);
   double SMA2    = iMA(Symbol(),PERIOD_CURRENT,150,0,MODE_SMA,PRICE_CLOSE,1);
   
   double FMA1    = iMA(Symbol(),PERIOD_CURRENT,50,0,MODE_SMA,PRICE_CLOSE,0);
   double FMA2    = iMA(Symbol(),PERIOD_CURRENT,50,0,MODE_SMA,PRICE_CLOSE,1);
   
  
   //Sell Conditions
   if((FMA2>SMA2) && (FMA1>SMA1))   
   {
      OrderSend(Symbol(),OP_SELL,5,Bid,2,(Bid+0.0002),(Bid-0.0005),NULL,magic_number);
      return;
   }


Thanks in advance!

Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 

Please don't post randomly in any section. Your question is not related to the section you posted.

MT4/mql4 has it's own section on the forum.

I have moved your topic to the correct section later, please don't create an other topic.

 

Does anyone have an idea how I can code this? 

//check condition for bullish retest(just a pseudo code! do not try compiling!
High[2]>FMA && Low[2]<FMA && Close[1]>FMA && Bid>High[1]
 
Yashar Seyyedin #:

Thank you brother, i'll try it!

 
Alain Verleyen #:

Ok thank you! I didn't see any sections. I just saw thread and posted another one :-)

 

Unfortunately it didn't work.I want to open a SHORT trade if the following rules are true:

- Stoch over level 80

- FMA (50) crossed the SMA (150) to the downside

- Price retests the SMA (150)


But i'm a bit lost with the retest and it opens a ton of trades .The EA is based on this youtube video:



What I've already done:

int m_twentyticks = 0;
bool m_opentrades = false;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   string signal ="";
   double K0      = iStochastic(Symbol(),PERIOD_CURRENT,5,5,5,MODE_SMA,0,MODE_MAIN,0);
   double K1      = iStochastic(Symbol(),PERIOD_CURRENT,5,5,5,MODE_SMA,0,MODE_MAIN,1);
   
   double D0      = iStochastic(Symbol(),PERIOD_CURRENT,5,5,5,MODE_SMA,0,MODE_SIGNAL,0);
   double D1      = iStochastic(Symbol(),PERIOD_CURRENT,5,5,5,MODE_SMA,0,MODE_SIGNAL,1);
   
   double SMA1    = iMA(Symbol(),PERIOD_CURRENT,150,0,MODE_SMA,PRICE_CLOSE,0);
   double SMA2    = iMA(Symbol(),PERIOD_CURRENT,150,0,MODE_SMA,PRICE_CLOSE,1);
   
   double FMA1    = iMA(Symbol(),PERIOD_CURRENT,50,0,MODE_SMA,PRICE_CLOSE,0);
   double FMA2    = iMA(Symbol(),PERIOD_CURRENT,50,0,MODE_SMA,PRICE_CLOSE,1);
   
   if(m_twentyticks == 20)
   {
      m_twentyticks = 0;
      m_opentrades = false;
   }
   
   m_twentyticks++;
   
   //Sell Conditions
   if((FMA2>SMA2) && (FMA1>SMA1) && (K0>80) && (D0>80) && (D0>K0) && (D1<K1) && !m_opentrades)    
   {
      OrderSend(Symbol(),OP_SELL,5,Bid,2,(Bid+0.0002),(Bid-0.0005),NULL,magic_number);
      m_opentrades = true;
      return;
   }

   //Buy Conditions
   if((FMA2<SMA2) && (FMA1<SMA1) && (K0<20) && (D0<20) && (D0<K0) && (D1>K1) && !m_opentrades) 
   {
      OrderSend(Symbol(),OP_BUY,5,Ask,2,(Bid-0.0002),(Bid+0.0005),NULL,magic_number);
      m_opentrades = true;
      return;
   }
   } 
Files:
image.png  83 kb
 
Dominic Frehner #:

Unfortunately it didn't work.I want to open a SHORT trade if the following rules are true:

- Stoch over level 80

- FMA (50) crossed the SMA (150) to the downside

- Price retests the SMA (150)


But i'm a bit lost with the retest and it opens a ton of trades .The EA is based on this youtube video:



What I've already done:

YOU sure about this? You are selling when fastma>slowma… everything look’s reversed 
 
  int m_twentyticks = 0;
   bool m_opentrades = false;
   input int magic_number=1234;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   string signal ="";

   if(m_twentyticks == 20)
     {
      m_twentyticks = 0;
      m_opentrades = false;
     }

   m_twentyticks++;

//Sell Conditions
   if(Trade()=="sell"&& SMA_touch(ORDER_TYPE_SELL) && !m_opentrades)
     {
      int ticke_t=  OrderSend(Symbol(),OP_SELL,5,Bid,2,(Bid+0.0002),(Bid-0.0005),NULL,magic_number);
      m_opentrades = true;

     }

//Buy Conditions
   if(Trade()=="buy" && SMA_touch(ORDER_TYPE_BUY) && !m_opentrades)
     {
      int ticket=  OrderSend(Symbol(),OP_BUY,5,Ask,2,(Bid-0.0002),(Bid+0.0005),NULL,magic_number);
      m_opentrades = true;

     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool SMA_touch(ENUM_ORDER_TYPE type)
  {
   if(type==ORDER_TYPE_BUY)
     {
      if(High[2]<SMA(2) && Close[1]>SMA(1))
         return true;
     }
   if(type==ORDER_TYPE_SELL)
     {
      if(High[2]>SMA(2) && Close[1]<SMA(1))
        {
         return true;
        }
     }


   return false;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double K(int buff) {return(iStochastic(Symbol(),PERIOD_CURRENT,5,5,5,MODE_SMA,0,MODE_MAIN,buff));}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double D(int buff) {return(iStochastic(Symbol(),PERIOD_CURRENT,5,5,5,MODE_SMA,0,MODE_SIGNAL,buff));}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double SMA(int buff) {return(iMA(Symbol(),PERIOD_CURRENT,150,0,MODE_SMA,PRICE_CLOSE,buff));}
double FMA(int buff) {return(iMA(Symbol(),PERIOD_CURRENT,50,0,MODE_SMA,PRICE_CLOSE,buff));}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string  Trade()
  {
   string result="null";

   if(FMA(1)>SMA(1) && FMA(0)>SMA(0)&&
      K(1)<20 && D(1)<20  && D(0)>20 && K(0)>20)
      result="buy";

   if(FMA(1)>SMA(1) && FMA(0)>SMA(0)&&
      K(1)>80 && D(1)>80  && K(0)<80 && D(0)<80)
      result="sell";


   return result;
  }
//+------------------------------------------------------------------+
 
Daniel Cioca #:
YOU sure about this? You are selling when fastma>slowma… everything look’s reversed 
Brother you‘re completely right! That was my mistake! Thanks for your help! On a sell the FMA needs to be lower than the SMA of course :-) And thanks a lot for the corrections! Gonna try this tomorrow😊
 
Dominic Frehner #:
Brother you‘re completely right! That was my mistake! Thanks for your help! On a sell the FMA needs to be lower than the SMA of course :-) And thanks a lot for the corrections! Gonna try this tomorrow😊

There is a typo mistake on SMA_touch() function. You should reverse that . Use  SMA_touch(ORDER_TYPE_BUY) for buy side and  SMA_touch(ORDER_TYPE_SELL) for sell side

 
Daniel Cioca #:

There is a typo mistake on SMA_touch() function. You should reverse that . Use  SMA_touch(ORDER_TYPE_BUY) for buy side and  SMA_touch(ORDER_TYPE_SELL) for sell side

Perfect thanks! Let me try that quick!