Help with code for pullback strategy EA - MQL4

 

Hi all 

I'm trying to code an EA for backtesting a fairly basic pullback trading strategy for EUR/USD. Basically, I wait for a trend to be identified by the 8,13,21 EMA on the 5 min timeframe and the 8,21 EMA on the 1H timeframe. Then wait for price to pullback and close inside the array of EMAs on the 5min frame, followed by an engulfing candle. I then look to enter the trade 2 pips below/above the high/low of the engulfing candle (so long as this occurs within the next 10 candles). SL is 2 pips above/below the high/low of the engulfing candle and TP is 1.5x risk. 

I'm having trouble achieving this on backtesting and wondered whether anyone could see where I going wrong. The code is below (can ignore RSI for now);

Many thanks 



input double tp;


input double sl;


input int emaperiod; 


void OnTick()

  {

//---


      // define the properties of the RSI 

      double rsicurrent = iRSI (_Symbol,0, 14, PRICE_CLOSE,1);

      double rsiprevious = iRSI(_Symbol,0, 14, PRICE_CLOSE,2); 

     

      double currentpriceclose = Close[1];

      double currentpriceopen = Open[1];

      double previouspriceclose = Close[2];

      double previouspriceopen = Open[2];

      double previouspricelow = Low[2];

      double previouspricehigh = High[2];

      

     


      

      double buylong = (High[1]+0.0002);

      double buyshort = (Low[1]-0.0002);

   

     

      

      double slbuy = (Low[1]-0.0002);

      double tpbuy = ((buylong)+(((buylong)-slbuy)*1.5));

      

      

      double slsell = (High[1]+0.0002);

      double tpsell = ((buyshort)-((slsell-(buyshort))*1.5));

      

      

     

     // define EMAs 

     

     

     double ema8 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_CLOSE, 1);

     double ema13 = iMA(NULL, 0, 13, 0, MODE_EMA, PRICE_CLOSE, 1);

     double ema21 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, 1);

     

     double ema81H = iMA(NULL,60,8,0, MODE_EMA, PRICE_CLOSE, 1);

     double ema131H = iMA(NULL,60,13,0,MODE_EMA, PRICE_CLOSE, 1);

     double ema211H = iMA(NULL,60,21,0,MODE_EMA, PRICE_CLOSE, 1);

     

     

if ((previouspriceclose < ema8) && (previouspriceclose > ema21) && (previouspriceopen > previouspriceclose) && 

(currentpriceclose > ema8) && (currentpriceclose > previouspriceopen) && (ema8 > ema13) && (ema13 > ema21) 

&& (currentpriceclose > ema81H) && (ema81H > ema211H)

 && (OrdersTotal() < 1)  && (Time[0]>=TimeCurrent())) {

OrderSend(_Symbol, OP_BUY,0.01, buylong ,20,slbuy,tpbuy,NULL,0,0,NULL); 

}

else if ((previouspriceclose > ema8) && (previouspriceclose < ema21) && (previouspriceopen < previouspriceclose) && 

(currentpriceclose < ema8) && (currentpriceclose < previouspriceopen) && (ema8 < ema13) && (ema13 < ema21)

&& (currentpriceclose < ema81H) && (ema81H < ema211H) 

 && (OrdersTotal() < 1) && (Time[0]>=TimeCurrent()) ){

OrderSend(_Symbol,OP_SELL,0.01, buyshort ,20,slsell,tpsell,NULL,0,0,NULL);

}


 

I think for you to identify such strategy, you should be putting them into functional formats and inputting them onto the ontick function. You need to organise the signals a bit better because you are piling all signals into one execution, there are certain checks that need to be made for an execution. Putting some of your signals on global may be an ideal approach. Here is an engulfing (not finished) example, something like this you need to add into the signals.


double Engulfing ()



{

double close; 

double prevclose;

double closeHigh;

bool result = false;

double calc;

double CloseOpen;

double open2;



close = Close [1];

prevclose = Close [2];

closeHigh = High[1];

CloseOpen = Open [1];

open2 = Open [2];


calc = Close[1] > open2 + 0.00020*_Point;





{

if(close > prevclose

   && close < closeHigh

   && calc

   && close < closeHigh) result = true;

   return(result);

   }

  }


void OnTick()

" indicators"


if(OrdersTotal() < 1) 
if(Time[0]>=TimeCurrent())){( "signal)}

/// Separate both if functions. 




Engulfing returns true from the value of 1 , if 
Engulfing()  == 1 , "do something". 

OrderSend();


This is a rough code but should be a guidance to look for an engulfing and inputting it as a signal (more calculations needed &  more tests). Make sure that this function when you work on 
is used AFTER you check signal eg ema ma. Your engulfing signal should be separate otherwise the ea will not execute anything. 
Just to reiterate. 1st signal, check candle 10 2nd signal, execute
 

Amazing thanks! Is there a way I can cancel the order if the price doesn't hit 2 pips above/below the high/low of the engulfing candle within the next 10 candles? 


Also, is it possible to set the SL and TP with reference to the engulfing candles? e.g. SL 2 pips above/below the high/low of the engulfing candle and then TP at 1.5 x risk? 


Many thanks for your help! 

 
samyjo222 #:

Amazing thanks! Is there a way I can cancel the order if the price doesn't hit 2 pips above/below the high/low of the engulfing candle within the next 10 candles? 


Also, is it possible to set the SL and TP with reference to the engulfing candles? e.g. SL 2 pips above/below the high/low of the engulfing candle and then TP at 1.5 x risk?

first all, you have quite a challenge. And that is to identify the previous 10 candles. Assuming you are using 5 minutes, you want to count how many bars there are in the first 10 which is 50 minutes until the signal and order expires. If signal one is checked (i.e ma and price closes), and is also within a 50 minute time zone, this can be returned in true or false also(not sure about the code for this) you can use the engulfing for entry as finalized, I can help you with the code just add me, it would be fun to work on this.   

 
Adj007 #:

first all, you have quite a challenge. And that is to identify the previous 10 candles. Assuming you are using 5 minutes, you want to count how many bars there are in the first 10 which is 50 minutes until the signal and order expires. If signal one is checked (i.e ma and price closes), and is also within a 50 minute time zone, this can be returned in true or false also(not sure about the code for this) you can use the engulfing for entry as finalized, I can help you with the code just add me, it would be fun to work on this.   

Thanks so much that's really helpful! 

And is it possible to set the SL and TP with reference to the engulfing candles? e.g. SL 2 pips above/below the high/low of the engulfing candle and then TP at 1.5 x risk?

 
With that you want a buylimit after all signals are met, but I am not sure if brokers allow a pullback buylimit of 2 points away from previous close. And regards to TP at 1.5, that is doable, just input an ATR indicator and calclulate tp, (with SL just set it to a default standard for now) and add it into your ordersend. Many videos on how to use ATR. 
 
samyjo222 #: Is there a way I can cancel the order if the price doesn't hit 2 pips above/below the high/low of the engulfing candle within the next 10 candles?

Select the order. Verify it is still pending. Get the bar shift of the OrderOpenTime. Delete the order if is more than ten.

 
William Roeder #:

Select the order. Verify it is still pending. Get the bar shift of the OrderOpenTime. Delete the order if is more than ten.

Ok, although doesn't the OrderSend function have an expiration argument within it's string?
 
samyjo222 #: doesn't the OrderSend function have an expiration argument within it's string?
Your original post said 10 candles.
  1. You can use that, if your broker allows expirations. Some do not. But:
  2. Remember that more than 10 candles is not the same as 10*PeriodSeconds().

    Don't assume every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

Reason: