What is wrong with this if Statement?

 

Hi, 

I made this EA which will take only one trade at time if the buy/sell condition satisfies. It will not take any new trade if current trade is online. Once current trade got closed it will check for buy/sell conditions again.

But currently its not taking any trades. I tested the code, part by part. I observed that if statement is not getting satisfied when I compare Close[1] < Lowest or Close[1] > Highest

Here is the code.

 int B1,B2,B0;
 bool can_buy=true;
 bool can_sell=true;
 
// Init function---------------------------------------------------------
int init()
{

int CalcDigits = (int)MarketInfo(Symbol(),MODE_DIGITS);

if(CalcDigits == 2 || CalcDigits == 4) CalcSlippage = SlippagePips;
else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;      


if(CalcDigits == 2 || CalcDigits == 3) CalcPoint1 = 0.01;
else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint1 = 0.0001;
      
UsePoint = CalcPoint;
UseSlippage = (int) CalcSlippage; 
return (0);
}
//-------------------------------------------------------------
int start()
{
 double CalcDigits = MarketInfo(Symbol(),MODE_DIGITS);
      {
      if(CalcDigits == 2 || CalcDigits == 3) CalcPoint = 100;
      else if (CalcDigits == 4 || CalcDigits == 5) CalcPoint = 10000;
      }

//------------------------Gathering Data for trade condition.

if(Close[3]>Open[3]) B2 = 1;
if(Close[3]<Open[3]) B2 = 0;

if(Close[2]>Open[2]) B1 = 1;
if(Close[2]<Open[2]) B1 = 0;

if(Close[1]>Open[1]) B0 = 1;
if(Close[1]<Open[1]) B0 = 0;

int H = iHighest(NULL,0,MODE_HIGH,3,1);
int L = iLowest(NULL,0,MODE_LOW,3,1);
double Highest = iHigh(NULL,0,H);
double Lowest = iLow(NULL,0,L);

    for(int x=OrdersTotal()-1;x>=0;x--)
      {
         if(OrderSelect(x,SELECT_BY_POS) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
          {
          if(OrderType()==OP_BUY)
             can_buy=false;
          if(OrderType()==OP_SELL)
             can_sell=false;
          }
      }
      
  //---------------
  if(can_sell)
     {
      if(B1== 1 && B2 == 1 && B0 == 0 && Close[1] < Lowest)
      {

       double SellStopLoss = Highest;
       double SellTakeProfit = Bid - (TakeProfit *  CalcPoint1);
       SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);  
        }  
     }
     
  if(can_buy)
  
    {
      if(B1==0 && B2 == 0 && B0 == 1 && Close[1] > Highest)
      {
       double BuyStopLoss = Lowest;
       double BuyTakeProfit = Ask + (TakeProfit *  CalcPoint1);
       BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green); 
      }
    }
return(0);
}    

 Any Suggestion?

Thank you. 

 
int L = iLowest(NULL,0,MODE_LOW,3,1);

double Lowest = iLow(NULL,0,L);

Lowest is the low of bars 1,2 and 3

      if(B1== 1 && B2 == 1 && B0 == 0 && Close[1] < Lowest)

It is not possible that the close of bar[1]  will be lower than the low of bars 1,2 and 3

 
GumRai:

Lowest is the low of bars 1,2 and 3

It is not possible that the close of bar[1]  will be lower than the low of bars 1,2 and 3




Thank you for correcting me.. 

But After I altered it, Bid  < Lowest, that means if bid price is lower than last 3 bars lowest low, sell.

Now it takes only 2 trades. One buy trade after it got closed,  one sell trade, then later no trade.

Why is that?

 
cashcube: One buy trade after it got closed,  one sell trade, then later no trade. Why is that?
if(OrderSelect(x,SELECT_BY_POS) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
 {
 if(OrderType()==OP_BUY)
    can_buy=false;
 if(OrderType()==OP_SELL)
    can_sell=false;
 }
It open a buy, set can_buy to false; it will never open another buy.
It opens a sell, sets can_sell to false; it will never trade again.
If you had printed your if variables you would have found out why.
 
WHRoeder:
cashcube: One buy trade after it got closed,  one sell trade, then later no trade. Why is that?
It open a buy, set can_buy to false; it will never open another buy.
It opens a sell, sets can_sell to false; it will never trade again.
If you had printed your if variables you would have found out why.

Thank you WHReoder,

I was also thinking about it...I copied this code from somewhere else.

Actually as I mentioned, EA should take only one trade at a time. When current trade is online, it shouldn't take another trade until it got closed. Then how to do that? With OrdersTotal()? 

 
cashcube:

I was also thinking about it...I copied this code from somewhere else.


 

The code is an example that I gave you in your previous thread.

 bool can_buy=true;
 bool can_sell=true;
 for(int x=OrdersTotal()-1;x>=0;x--)
    {
    if(OrderSelect(x,SELECT_BY_POS) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
          {
          if(OrderType()==OP_BUY)
             can_buy=false;
          if(OrderType()==OP_SELL)
             can_sell=false;
          }
    }
  
  if(can_buy)
     {
     //Check whether to place order
     }
     
  if(can_sell)
     {
     //Check whether to place order
     }

Note, can_buy and can_sell are locally declared variables


It can easily be modified to only allow 1 trade open at a time

 bool can_trade=true;
 for(int x=OrdersTotal()-1;x>=0;x--)
    {
    if(OrderSelect(x,SELECT_BY_POS) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
          {
           can_trade=false
          }
    }

Then check conditions to trade. If by some strange chance it is possible that both buy and sell conditions can be satisfied, you would need to take that into account

 
GumRai:

The code is an example that I gave you in your previous thread.

Note, can_buy and can_sell are locally declared variables


It can easily be modified to only allow 1 trade open at a time

Then check conditions to trade. If by some strange chance it is possible that both buy and sell conditions can be satisfied, you would need to take that into account

Yes you have shared the code with me. But misunderstood the local part. Now I declared them inside start() function & it works perfectly.

Thank you again..