[HELP][MQL4] Can i make a EA wait for what happens the next bars ?

 

Hello,


This EA is buying when the HMA indicator is turning green and selling when its turning red. But then it needs to wait till the ROC is getting above 0 for a buy and below 0 for a sell.


Right now its only opening a trade if it happens at the same time. Mostly the hma turns green first and then the ROC crosses 0. So mostly its not opening trades, because its not happening at the same time


What do I need to do in order to let it "wait" for the ROC and then open a trade ?


#property copyright ""
#property link      ""
#property version   "1.00"
#property strict
extern int ROCPeriod1  = 10;
extern int ROCPeriod2  = 20;
extern int ROCType1    = 0;
extern int ROCType2    = 0;;

//Custom function

int Cross() {

double ROCP1=iCustom(NULL,0,"ROC2_VG",ROCPeriod1,ROCType1,0,1);

double ROCP1_1=iCustom(NULL,0,"ROC2_VG",ROCPeriod1,ROCType1,0,0);

Print(StringConcatenate("ROCP1: ", ROCP1));

Print(StringConcatenate("ROCP1_1: ", ROCP1_1));

if(ROCP1 > 0 && ROCP1_1 > 0){return(1);}

if(ROCP1 < 0 && ROCP1_1 < 0){return(-1);}

return(0);

}
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
if (OrdersTotal() > 0) {
        return;
    }

    //Buy: when current line is up and blue and previous line is down and red
    
    double buy_hmacurrent = iCustom(NULL, 0, "hma-trend-indicator", 30,3,0, 0, 0);

    double buy_hmaprevious = iCustom(NULL, 0, "hma-trend-indicator", 30,3,0, 0, 1);



    //Sell: when current line is down and red and previous line is up and blue
    
    double sell_hmacurrent = iCustom(NULL, 0, "hma-trend-indicator", 30, 3, 0, 1, 0);

    double sell_hmaprevious = iCustom(NULL, 0, "hma-trend-indicator", 30, 3, 0, 1, 1);
    
  
   
    // Here, we fetch the current 3-period ATR value
    double atr = iATR(NULL, 0, 14, 0);
     
     
     
    // Since the stop loss will be a multiple of this ATR value,
    // we define this multiple. 2.5 times the ATR value
    // is often a good start:
    double atrMultiple = 3;
     
     
     
    // Now, we set our stop loss.
    // We multiply the ATR value by the ATR multiple
    // and divide by the Point constant
    // in order to get a value in points:
    int stopLoss = (int)(atr * atrMultiple / Point);
     
     
     
    // And, since we have a dynamic stop loss value,
    // it makes sense to use the same pips amount as our take profit
    int takeProfit = stopLoss;
    
    
    
    // Dynamic lot sizes based on our stop loss
    double lots = calculateLotSize(stopLoss);
    
    
    
    // Buy Rules
        if (Cross()>0)
        if (sell_hmaprevious!=EMPTY_VALUE && sell_hmacurrent==EMPTY_VALUE && buy_hmacurrent!=EMPTY_VALUE)
        
          {
        if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask - stopLoss * Point, Ask + takeProfit * Point, "1", 12345, 0, Green)) {
            Print("Buy order succeeded!");
        }
    }
    
    

   // Sell Rules
        if(Cross()<0)
        if(buy_hmaprevious!=EMPTY_VALUE && buy_hmacurrent==EMPTY_VALUE && sell_hmacurrent!=EMPTY_VALUE)
        
         {
        if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Bid + stopLoss * Point, Bid - takeProfit * Point, "2", 12345, 0, Red)) {
            Print("Sell order succeeded!");
        }
    }
   
}
//+------------------------------------------------------------------+

/**
 * Calculate the amount of lots needed based on stop loss
 *
 * @return  double
 */
double calculateLotSize(int StopLossPoints)
{
    // 1% risk per trade
    int risk = 2;
    
    double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
    double minLot  = MarketInfo(Symbol(), MODE_MINLOT); 
    double maxLot  = MarketInfo(Symbol(), MODE_MAXLOT);
    double tickVal = MarketInfo(Symbol(), MODE_TICKVALUE);

    double lotSize = AccountBalance() * risk / 100 / (StopLossPoints * tickVal);

    return MathMin(
        maxLot,
        MathMax(
            minLot,
            NormalizeDouble(lotSize / lotStep, 0) * lotStep // This rounds the lotSize to the nearest lotstep interval
        )
    ); 
}
 

If you need the ROC crosses below/above 0, you can change the Cross function to :

int Cross() 
  {
   double ROCP1=iCustom(NULL,0,"ROC2_VG",ROCPeriod1,ROCType1,0,1);
   double ROCP1_1=iCustom(NULL,0,"ROC2_VG",ROCPeriod1,ROCType1,0,0);
   Print(StringConcatenate("ROCP1: ",ROCP1));
   Print(StringConcatenate("ROCP1_1: ",ROCP1_1));
   if(ROCP1<0 && ROCP1_1>0){return(1);}
   if(ROCP1>0 && ROCP1_1<0){return(-1);}
   return(0);
  }

But if you don't need the ROC crosses, only above/below EA, then change the Cross function to :

int Cross() 
  {
   double ROCP1_1=iCustom(NULL,0,"ROC2_VG",ROCPeriod1,ROCType1,0,0);
   Print(StringConcatenate("ROCP1_1: ",ROCP1_1));
   if( ROCP1_1>0){return(1);}
   if( ROCP1_1<0){return(-1);}
   return(0);
  }
 
Biantoro Kunarto:

If you need the ROC crosses below/above 0, you can change the Cross function to :

But if you don't need the ROC crosses, only above/below EA, then change the Cross function to :

Thats not what i mean. The problem i have is, that the ROC gets above 0 later then the first buy signal. but right not the EA only takes trades when its happening at the same time

 

Try changing this:

    // Buy Rules
        if (Cross()>0)
        if (sell_hmaprevious!=EMPTY_VALUE && sell_hmacurrent==EMPTY_VALUE && buy_hmacurrent!=EMPTY_VALUE)
        

...to this:

    // Buy Rules
        if (   Cross()>0
            && sell_hmaprevious!=EMPTY_VALUE && sell_hmacurrent==EMPTY_VALUE && buy_hmacurrent!=EMPTY_VALUE)
        

The conditions must be "Cross" AND "sell_hmaprevious"... basically, your entry conditions are just based on:

if (sell_hmaprevious!=EMPTY_VALUE && sell_hmacurrent==EMPTY_VALUE && buy_hmacurrent!=EMPTY_VALUE)

...if I am correct and that makes any sense to you...

VisaVersa for "Sell" obviously...

   // Sell Rules
        if(   Cross()<0
           && buy_hmaprevious!=EMPTY_VALUE && buy_hmacurrent==EMPTY_VALUE && sell_hmacurrent!=EMPTY_VALUE)
 
Mike Tanton:

Try changing this:

...to this:

The conditions must be "Cross" AND "sell_hmaprevious"... basically, your entry conditions are just based on:

...if I am correct and that makes any sense to you...


The problem is, that the ROC crosses 0 mostly way later. The EA is only opening trades when it happens at the same time.


If the ROC crosses 0 after the HMA turns green/red, then its not opening any trades. But I want it to.


i have the feeling it can de done with iBar. But im not sure.

 
fiehejulien:

The problem is, that the ROC crosses 0 mostly way later. The EA is only opening trades when it happens at the same time.

Well... it is delayed because of your Cross() function... read what Biantoro said above... but you could also be using the wrong "hma-trend-indicator" buffer values... If the buffer values are the "Arrows" of the indicator then your entry conditions will only be "true" on that candle... if however you use the "Dots"... then your "HMA" entry conditions will be true over several candles....

... if that makes any sense...

 
Mike Tanton:

Well... it is delayed because of your Cross() function... read what Biantoro said above... but you could also be using the wrong "hma-trend-indicator" buffer values... If the buffer values are the "Arrows" of the indicator then your entry conditions will only be "true" on that candle... if however you use the "Dots"... then your "HMA" entry conditions will be true over several candles....

... if that makes any sense...

https://ibb.co/1sn7MZc

That's when it should open a trade. The HMA Indicator is a line in my case. As you can see on the picture. If its just the HMA, then everything is fine.


My problem is that it doesn't open trades when the hma turns green/red first and then the ROC crosses or the ROC crosses first and then the hma turns green/red.


I'm sorry if i didn't explain that clearly before

AUDCHFDaily
AUDCHFDaily
  • ibb.co
Bild AUDCHFDaily gespeichert in imgbb.com
 

Attach your HMA indi so that I can have a look at the buffers... but an easy fix would be to simply take the "Slope" of your "HMA" indi line.... something like:

double HMA_Slope = (iCustom(NULL, 0, "hma-trend-indicator", 30,3,0, 0, 0) - iCustom(NULL, 0, "hma-trend-indicator", 30,3,0, 0, 1));

Then change your Entry conditions to:

    // Buy Rules
        if (   Cross()>0
            && HMA_Slope > 0)

    // Sell Rules
        if (   Cross()<0
            && HMA_Slope < 0)

...now the "Entry" conditions should be "True" whenever the HMA line is "Green" AND ROC crosses above "0"... and whenever the HMA line is "Red" and ROC crosses down below "0".... well... it should


Sorry... AND change your Cross() function to what Biantoro said...

int Cross() 
  {
   double ROCP1=iCustom(NULL,0,"ROC2_VG",ROCPeriod1,ROCType1,0,1);
   double ROCP1_1=iCustom(NULL,0,"ROC2_VG",ROCPeriod1,ROCType1,0,0);
   Print(StringConcatenate("ROCP1: ",ROCP1));
   Print(StringConcatenate("ROCP1_1: ",ROCP1_1));
   if(ROCP1<0 && ROCP1_1>0){return(1);}
   if(ROCP1>0 && ROCP1_1<0){return(-1);}
   return(0);
  }
Hope that helps...
 
Mike Tanton:

Attach your HMA indi so that I can have a look at the buffers... but an easy fix would be to simply take the "Slope" of your "HMA" indi line.... something like:

Then change your Entry conditions to:

...now the "Entry" conditions should be "True" whenever the HMA line is "Green" AND ROC crosses above "0"... and whenever the HMA line is "Red" and ROC crosses down below "0".... well... it should


Sorry... AND change your Cross() function to what Biantoro said...

Hope that helps...

Thank you ! Its already working well, but its not taking any short trades now.

 

...it was the "buffer" number (normly is with these 'Changing Color' trend indicators)....

Buffer 0 - is the "Green" color "Up" trend value...

Buffer 1 - is the "Red" color "Down" trend value...

Buffer 2 - is the actual indicator "Line" value....

.....so, your "Slope" calculation should be using Buffer 2... otherwize the calc. will only be using the Up or Down value.... hence only making "Long" trades...

double HMA_Slope = (iCustom(NULL, 0, "hma-trend-indicator", 30,3,0, 2, 0) - iCustom(NULL, 0, "hma-trend-indicator", 30,3,0, 2, 1));

...should work ok now...

 
Mike Tanton:

...it was the "buffer" number (normly is with these 'Changing Color' trend indicators)....

Buffer 0 - is the "Green" color "Up" trend value...

Buffer 1 - is the "Red" color "Down" trend value...

Buffer 2 - is the actual indicator "Line" value....

.....so, your "Slope" calculation should be using Buffer 2... otherwize the calc. will only be using the Up or Down value.... hence only making "Long" trades...

...should work ok now...

Thank you very much !!! its working just fine !

Reason: