How to stop this from entering a trade on every tick

 
Hi all, new to MQ4 so reallyt stumped!!!  I am trying to get the attached 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 so it when the condition is met but on every tick for  that bar.  I have tried multiple ways but just cant work it out.  Any help woudl be really appreciated,
Files:
MYStrat1.txt  5 kb
 
Andrew Britain: Hi all, new to MQ4 so reallyt stumped!!!  I am trying to get the attached 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 so it when the condition is met but on every tick for  that bar.  I have tried multiple ways but just cant work it out.  Any help woudl be really appreciated,

Please attach a proper code file with the ".mq4" extension and not just a standard text file with code in them.

 
Andrew Britain: new to MQ4 so reallyt stumped!!!  I am trying to get the attached to only enter a trade at the start of a new bar
  1. Don't rename extensions.
  2. Where is your new bar test? How are "you trying?"
  3. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

 

Hi all, thanks for the comments.

I have added this line into the OnTick - 

if (!IsNewBar()) return;                   //    This EA only runs at the start of each bar

and that is using this 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);
}    
Now, if I dont add those 2 bits of code, it will run as I want it to but it fires on each Pip when the criteria is met.  But as soon as I add those 2 bits of code it, runs but no trades are fired.   I have attached the script, hopefully I have done it right, sorry but new to all this so its my first post
 
Andrew Britain #: Hi all, thanks for the comments. I have added this line into the OnTick - and that is using this function. Now, if I dont add those 2 bits of code, it will run as I want it to but it fires on each Pip when the criteria is met.  But as soon as I add those 2 bits of code it, runs but no trades are fired.  I have attached the script, hopefully I have done it right, sorry but new to all this so its my first post

It should be the .mq4 file, not the .ex4

 
Andrew Britain #: Now, if I dont add those 2 bits of code, it will run as I want it to but it fires on each Pip when the criteria is met.  But as soon as I add those 2 bits of code it, runs but no trades are fired.  
 double BarValueBulls = High[0] - Open[0];  // Get size of bar for longs
   double BarValueBears = Open[0] - Low[0];   // Get size of bar for shorts

Now you are processing the first tick of a new bar. Therefor, High[0] equals Open[0] equals Low[0]. Why does no trades surprise you?

 
Andrew Britain #Hi all, thanks for the comments. I have added this line into the OnTick - Now, if I dont add those 2 bits of code, it will run as I want it to but it fires on each Pip when the criteria is met.  But as soon as I add those 2 bits of code it, runs but no trades are fired.  I have attached the script, hopefully I have done it right, sorry but new to all this so its my first post

Please EDIT your post and use the CODE button when you post code.

Code button in editor

 
William Roeder #:

Now you are processing the first tick of a new bar. Therefor, High[0] equals Open[0] equals Low[0]. Why does no trades surprise you?

Thanks for the feedback William, I have fixed the issue by adding this


 int       ticket=0;
   
        if (bullisheng && last5barsbear) 
   
      if(OrdersTotal()==0)
   
         ticket = OrderSend(Symbol(),OP_BUY, 0.02,Ask,3,StopForLongs,LimitForLongs,"",0,0,Red);
                
   if (bearisheng && last5barsbull)
   
       if(OrdersTotal()==0)
                        
            ticket = OrderSend(Symbol(),OP_SELL,0.02,Bid,3,StopForShorts,LimitForShorts,"",0,0,Blue);     
 
William Roeder #:

Now you are processing the first tick of a new bar. Therefor, High[0] equals Open[0] equals Low[0]. Why does no trades surprise you?

Seems I may have been a bit hasty with my celebrations!!  On looking at the results it is clear that I have only managed to reduce  the number of open trades to 1 and it is still firing on tick once all the criteria are met.  I think I undertsand your point but it still leaves me back at square one.
Reason: