Help with an EA - one variable seems to counteract another statement

 

 Hi all,

 

Appreciate any help here. I am new to MQL4 and have only been trying this for a week.

I have written a bunch of functions, which I then call, in the start function.

My problem seems to be the order in which things happen or possibly I need a loop, or maybe just the brackets are in the wrong place, but I have tried lots of variations to no avail. Here is a snippet of the code as it stands. (as i say I have tried lots of variations to this already). Basically the Barcheck vaiable seems to then conflict with the next statement.

Barcheck is a function that checks whether price is between a range. If it is in that range then it is true. I then want to take a trade the moment price is greater than that range. The problem seems to be though that as soon as price is greater than the range which is what I need for an entry ( ie if (buyprice > NormalizeDouble(EL,Digits)) - where EL is the EntryLine/top of range) it resets the BarCheck to false, and therefore my criteria is invalid.

I have added some debugging print statements at the bottom. You can see that the first two lines show Barcheck is true. I then need price to be greater than the EL at 1.02796, but as soon as it is in the third line it resets the Barcheck to false. 

I am sure this is just something simple - it always seems to be the case, but I can't figure it out

thanks

Simon 

 

int start()
  {
//----
double point_2 =Point/2;
double buyprice = Ask + point_2;
double sellprice= Bid +point_2;

  //START RUNNING THE SWING FUNCTION, SET FIB, SL & TP LINES
    Swings(); //this runs the function that defines the swings
    SL_TP(); //this runs the funtion that defines the SL and TP
    //BarCheck();
    
    //START LOOKING FOR ENTRY CRITERIA
  
    if (Trend == UpTrend)
    {
    if (OrderOpened <1)
    if (BarCheck() == true)
    if (buyprice >= NormalizeDouble(EL,Digits))
    if (OrdersTotal()<1)
    //OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,SL,TP,NULL,MagicNumber,0,Green);
    OpenBuyOrder(LotSize,SL,TP);
    //while (Trend == UpTrend)
    if (OrdersTotal()>0){OrderOpened=1;} 
    }

  
    if (Trend == DownTrend)
    {
    if (OrderOpened <1)
    if (BarCheck() == true)
    if (sellprice <= NormalizeDouble(EL,Digits))
    if (OrdersTotal()<1)
    OpenSellOrder(LotSize,SL,TP);
    //while (Trend == DownTrend)
    if (OrdersTotal()>0){OrderOpened=1;} 
    }
 return(0);
  }
16:43:39 2013.05.01 16:40  AUDUSD,H1: Bid:1.02776| Ask:1.02792| buyprice:1.02793| sellprice:1.02777| Fib61 = 1.02796|Fib73 = 1.02762| SL = 1.02762| TP = 1.02830| EL = 1.02796 | Trend = 1| Barcheck = 1| Orderopened = 0 
16:43:39 2013.05.01 16:40  AUDUSD,H1: Bid:1.02778| Ask:1.02794| buyprice:1.02794| sellprice:1.02778| Fib61 = 1.02796|Fib73 = 1.02762| SL = 1.02762| TP = 1.02830| EL = 1.02796 | Trend = 1| Barcheck = 1| Orderopened = 0 
16:43:39 2013.05.01 16:40  AUDUSD,H1: Bid:1.02781| Ask:1.02797| buyprice:1.02797| sellprice:1.02781| Fib61 = 1.02796|Fib73 = 1.02762| SL = 1.02762| TP = 1.02830| EL = 1.02796 | Trend = 1| Barcheck = 0| Orderopened = 0 
bool BarCheck()    // //check previous and current Bars  (currently the current price check is greyed out)
{
   if (Trend == UpTrend)   //will be buy trades
      if (ExtEntryLine==50)
         //while (Ask > Fib61Line)
        {
         if ((Ask < Fib50Line) && (Ask > Fib61Line))
         if ((iLow(NULL,ExtBarCheckTF,1) < Fib50Line) && (iLow(NULL,ExtBarCheckTF,1)> Fib61Line)) //low of previous bar is less than 50 greater than 61
        
         {
         BarCheck = true; 
         }
         
         }
    
 
         else if (ExtEntryLine==61)
         //while (Ask > Fib73Line)
         {
         if ((Ask < Fib61Line) && (Ask > Fib73Line))
         if ((iLow(NULL,ExtBarCheckTF,1) < Fib61Line) && (iLow(NULL,ExtBarCheckTF,1)> Fib73Line))//low of previous bar is less than 61 greater than 73
         
         {
         BarCheck = true; 
         }
         
         }
         
    if (Trend == DownTrend) //will be sell trades
     if (ExtEntryLine==50)
      //while (Bid >
        {
         if ((Bid > Fib50Line) && (Ask < Fib61Line))
         if ((iHigh(NULL,ExtBarCheckTF,1) > Fib50Line) && (iHigh(NULL,ExtBarCheckTF,1)< Fib61Line))//high of previous bar is greater than 50 less than 61
        
         {
         BarCheck = true; 
         }
        
         }
    
 
         else if (ExtEntryLine==61)
         {
         if ((Bid > Fib61Line) && (Ask < Fib73Line))
         if ((iHigh(NULL,ExtBarCheckTF,1) > Fib61Line) && (iHigh(NULL,ExtBarCheckTF,1)< Fib73Line))//high of previous bar is greater than 61 less than 73
         
         {
         BarCheck = true; 
         }
         
         }
   return(BarCheck); 
}


            
 
simoncs:

 Hi all,

 

Appreciate any help here. I am new to MQL4 and have only been trying this for a week.

I have written a bunch of functions, which I then call, in the start function.

My problem seems to be the order in which things happen or possibly I need a loop, or maybe just the brackets are in the wrong place, but I have tried lots of variations to no avail. Here is a snippet of the code as it stands. (as i say I have tried lots of variations to this already). Basically the Barcheck vaiable seems to then conflict with the next statement.

Barcheck is a function that checks whether price is between a range. If it is in that range then it is true. I then want to take a trade the moment price is greater than that range. The problem seems to be though that as soon as price is greater than the range which is what I need for an entry ( ie if (buyprice > NormalizeDouble(EL,Digits)) - where EL is the EntryLine/top of range) it resets the BarCheck to false, and therefore my criteria is invalid.

I am sure this is just something simple - it always seems to be the case, but I can't figure it out

thanks

Simon 

 

<CODE DELETED>

Please read some other posts before posting . . .

Please    edit   your post . . .    please use the   SRC   button to post code: How to use the   SRC   button. 

 
simoncs:

 Hi all,

 

Appreciate any help here. I am new to MQL4 and have only been trying this for a week.

I have written a bunch of functions, which I then call, in the start function.

My problem seems to be the order in which things happen or possibly I need a loop, or maybe just the brackets are in the wrong place, but I have tried lots of variations to no avail. Here is a snippet of the code as it stands. (as i say I have tried lots of variations to this already). Basically the Barcheck vaiable seems to then conflict with the next statement.

Barcheck is a function that checks whether price is between a range. If it is in that range then it is true. I then want to take a trade the moment price is greater than that range. The problem seems to be though that as soon as price is greater than the range which is what I need for an entry ( ie if (buyprice > NormalizeDouble(EL,Digits)) - where EL is the EntryLine/top of range) it resets the BarCheck to false, and therefore my criteria is invalid.

It might be a good idea to show the code for the BarCheck() function.
 
RaptorUK:
It might be a good idea to show the code for the BarCheck() function.


Sorry - have now posted the code in readable format in my first post.

I have been looking at other breakout type strategies but not found anything that has given me a clue yet. 

 
simoncs:


Sorry - have now posted the code in readable format in my first post.

I have been looking at other breakout type strategies but not found anything that has given me a clue yet. 

You need to be careful comparing double values . . .  read this thread:  Can price != price ?
 
RaptorUK:
You need to be careful comparing double values . . .  read this thread:  Can price != price ?


thanks - yes I had come across that thread and thought that might also be an issue, so I tried to use the formula that WHR suggests - just not sure I am using it correctly, though now at least the EA is taking more trades. I need to step through the error log (which I will do tomorrow as getting late here) to see if that is the root cause of the problem.

Is my interpretation of the formula correct? 

if (buyprice >= NormalizeDouble(EL,Digits)
//becomes
if ((buyprice - EL > Point/2)==0)


and

if (sellprice <= NormalizeDouble(EL,Digits))
//becomes
if((EL - sellprice > Point/2==0)
 
simoncs:


Is my interpretation of the formula correct? 

Don't use the  == 0,  

EL - sellprice > Point/2

 results in a true or false value . . . 

 
RaptorUK:

Don't use the  == 0,  

 results in a true or false value . . . 

 


ok sure that makes sense, thanks.

So now my price comparison is cleaned up, I still have the issue where as soon as price is greater than the EntryLine, which is the 2nd criteria, it sets the first criteria (Barcheck = true) back to false and therefore not a valid trade criteria.

so what I think I need is something that says whilst Barcheck = 1, and as soon as price is greater then take the trade.

have tried several iterations of while loop now but can't seem to get it to work. Or is it something to do with the way I do the BarCheck in the first place? Would I better off having this as a while loop?

any ideas? 

 
simoncs:


ok sure that makes sense, thanks.

So now my price comparison is cleaned up, I still have the issue where as soon as price is greater than the EntryLine, which is the 2nd criteria, it sets the first criteria (Barcheck = true) back to false and therefore not a valid trade criteria.

so what I think I need is something that says whilst Barcheck = 1, and as soon as price is greater then take the trade.

have tried several iterations of while loop now but can't seem to get it to work. Or is it something to do with the way I do the BarCheck in the first place? Would I better off having this as a while loop?

any ideas? 

Looking at your code above,  BarCheck() is not dependant on EL  so its value does not determine if BarCheck() returns true or false,  Print() the variables in the BarCheck() function so you can understand why it is returning false.
 
RaptorUK:
Looking at your code above,  BarCheck() is not dependant on EL  so its value does not determine if BarCheck() returns true or false,  Print() the variables in the BarCheck() function so you can understand why it is returning false.


Correct there is no dependancy on EL for the BarCheck - I am not expecting there to be. (My thinking was that I could have the entry criteria in a separate function.)

The function Barcheck just makes sure that price has hit the correct zone.

If price has hit this zone, thus validating the criteria (Barcheck = true), then as soon as price is less than the EL (if a buy), or greater than (if sell) I want to place a trade. (The EL happens to be either the top or bottom of the zone).

But the moment price moves out of the zone Barcheck becomes false again, and therefore that part of the criteria is not met.

So what I really need is two criteria to be met.

1. Trade hits the zone - (currently my Barcheck)

2. Trade then is greater than or less then the EL 

How can I make these things happen simultaneously? I was thinking some kind of loop maybe? Just not sure how to construct it so it works. (I am guessing here, there could be better ways to do this) 

 
simoncs:


How can I make these things happen simultaneously? I was thinking some kind of loop maybe? Just not sure how to construct it so it works. (I am guessing here, there could be better ways to do this) 

You can't do anything simultaneously with mql4,  it is purely serial.  Have you checked if BarCheck is returning true and false correctly ?
Reason: