EA sends oreder on tick and not at start of new candle

Andu77

Trader
Apr 9, 2023
2
0
6
50
I am trying to get the below code to only enter a trade at the start of a new bar when it meets the condition of being a bullish/bearish bar after 5 bearish or bullish candles. At the moment it does fire off a trade but it seems to do it as soon as the condition is met but on a tick for that bar. I have tried multiple ways but just cant work it out. Any help woudl be really appreciated,

MQL4:
/*
*/
 
#property version   "1.00"
#property strict
 
 
// Input parameters
input double   stopspriceXAU = 12;
input int      EngPercent = 6;
input int      limitnumber = 40;
input int      stopnumber = 3;
 
 
// Global variables
 
double      bullisheng;
double      bearisheng;
double    numofPipsforLongs;
double    numofPipsforShorts;
 
 
 
 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
 
 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
 
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
 
 
if (!IsNewBar()){
   double BarValueBulls = High[0] - Open[0];  // Get size of bar for longs
   double BarValueBears = Open[0] - Low[0];   // Get size of bar for shorts
 
   double SixtyPercentBulls = (BarValueBulls / 10) * EngPercent;  // check if valid bar over 60% of full bar range
   double SixtyPercentBears = (BarValueBears / 10) * EngPercent;  // check if valid bar over 60% of full bar range
 
   double BullaboveSixty = Open[0] + SixtyPercentBulls;
   double BearbelowSixty = Open[0] - SixtyPercentBears;
 
   int lowestbar = iLowest(_Symbol, _Period, MODE_LOW, stopnumber, 1);
   int highestbar = iHighest(_Symbol, _Period, MODE_HIGH, stopnumber, 1);
   int highbar1 = iHighest(_Symbol, _Period, MODE_HIGH, limitnumber,1);
   int lowbar1 = iLowest(_Symbol, _Period, MODE_LOW, limitnumber, 1);
 
   // Limit values
   double LimitForLongs=(High[highbar1] + stopspriceXAU);
   double LimitForShorts=(Low[lowbar1] - (stopspriceXAU));
 
   // Stop values
   double StopForLongs=(Low[lowestbar]);
   double StopForShorts=(High[highestbar]);
 
   double testforsizeofstopshorts = StopForShorts - Close[0];
   double testforsizeofstoplongs = Close[0] - StopForLongs;
 
   double   last5barsbear = Close[1] < Open[1] && Close[2] < Open[2] && Close[3] < Open[3] && Close[4] < Open[4] && Close[5] < Open[5];
    double   last5barsbull = Close[1] > Open[1] && Close[2] > Open[2] && Close[3] > Open[3] && Close[4] > Open[4] && Close[5] > Open[5];
 
    // Engulfing conditions
   bullisheng = Close[0] >= BullaboveSixty && Open[1] >= Close[1] && Close[0] > Open[0] && Close[0] >= Open[1] && Close[0] - Open[0] > Open[1] - Close[1];
   bearisheng = Close[0] <= BearbelowSixty && Close[1] >= Open[1] && Open[0] > Close[0] && Open[1] >= Close[0] && Open[0] - Close[0] > Close[1] - Open[1];
 
 
    if (testforsizeofstopshorts < 0.7 || testforsizeofstopshorts > 1)
 
       StopForShorts = Close[0] + 0.6 + stopspriceXAU;
       numofPipsforShorts = StopForShorts - Close[0];
 
 
   if (testforsizeofstoplongs < 0.7 || testforsizeofstoplongs > 1)
 
      StopForLongs = Close[0] - 0.6 - stopspriceXAU;
      numofPipsforLongs =  Close[0] - StopForLongs;
 
   int       ticket=0;
 
    if (bullisheng && last5barsbear)
 
      if(OrdersTotal()==0)
 
         ticket = OrderSend(Symbol(),OP_BUY, 0.04,Ask,3,StopForLongs,LimitForLongs,"",0,0,Red);
 
   if (bearisheng && last5barsbull)
 
       if(OrdersTotal()==0)
 
            ticket = OrderSend(Symbol(),OP_SELL,0.04,Bid,3,StopForShorts,LimitForShorts,"",0,0,Blue);   
         return;    }
    return;
 
}
 
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
bool    IsNewBar() {
 
    static datetime    currentTime    =    0;                                                        //    Will retain last value between calls
    bool    result    =    (currentTime!=Time[0]);                                                //    returns true at each new bar
    if (result)            currentTime    =    Time[0];                                                //    Update current at a new bar
    return(result);
 
}
 
 
   // We print the position size in lots.
  // Print("Position size in lots Longs -  ", CalculateLotSizeLongs(numofPipsforLongs));
   //Print("Position size in lots shorts - ", CalculateLotSizeLongs(numofPipsforShorts));
 
  // Print("num of pips longs -  ", numofPipsforLongs);
  // Print("num of pips shorts - ", numofPipsforShorts);
 
  //Print("highbar1 = ", High[highbar1]);
 //  Print("limit for longs = ", LimitForLongs);
// 
 //  Print("highestbar = ", High[highestbar]);
//   Print("stop for shorts = ", StopForShorts);
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,212
1,504
144
Odesa
www.earnforex.com
Two remarks:

1. This condition passes only when it is NOT a new bar, so it will trigger on all ticks of all bars except the first ticks:
MQL4:
if (!IsNewBar()){
2. In your main bearish/bullish checks you are checking the current bar - [0] - but if you want to check these conditions on the bar's first tick, they will be meaningless as the current bar won't be formed yet. You should be checking the latest finished bar instead - [1].
 

Andu77

Trader
Apr 9, 2023
2
0
6
50
Two remarks:

1. This condition passes only when it is NOT a new bar, so it will trigger on all ticks of all bars except the first ticks:
MQL4:
if (!IsNewBar()){
2. In your main bearish/bullish checks you are checking the current bar - [0] - but if you want to check these conditions on the bar's first tick, they will be meaningless as the current bar won't be formed yet. You should be checking the latest finished bar instead - [1].
Thank you for your comments, I see what you mean and I have made changes based on your feedback and it now works how I want it to!! Really appreciate the help