how to break a function and call next when condition becomes true for next

 

Hi,

I am brand new to MQL4 and this is my very 1st attempt at writing an EA. I have created two functions for trendLong and trendShort. If current bar's open is higher than daily open then it should open a long position and vice versa for short positions.

1- What I see is that it only opens long positions.

2- It does not check if condition to sell is now true.

3- It does not close a position when opposite condition becomes true.

I hope someone of you can help guide me through the learning process. 

Files:
 
Well, simply put the cursor on  OrderSelect(..) and press f1 - now read what OrderSelect returns and what true might be as an integer!
 
Carl Schreiber:
Well, simply put the cursor on  OrderSelect(..) and press f1 - now read what OrderSelect returns and what true might be as an integer!
Hi Carl. Thanks for your reply. I have gone through OrderSelect() multiple times and unable to figure out where am I making a mistake. Please guide.
 
extern double lot = 1.0;
extern int sl = 100;
extern int tp = 100;
extern int tf = 0;
extern int slippage = 5;

double DailyOpen     =   iOpen(NULL,PERIOD_D1,0);
double CurrentBar    =   iOpen(NULL,0,0);

You shouldn't assign values obtained from iOpen here; data might not be available yet.

bool trendLong()
  {
   if (CurrentBar > DailyOpen) return(true);
   else return(EMPTY_VALUE);
  }

A bool should return either true or false. Not EMPTY_VALUE, which is 2147483647, which will be interpreted as true.

The way you have written it, trendLong() always returns true. And so does trendShort().

void OnTick()
  {  
   if(trendLong() == true) ticket = OrderSelect(ticket,SELECT_BY_TICKET);
   if(OrdersTotal() != 0)
     {
      ticket = OrderClose(ticket,lot,Bid,slippage);
     }
   else
     {
      (OrderCloseTime() < CurrentBar);
       openBuy();
     }
   if (trendShort() == true)
   ticket = OrderSelect(ticket,SELECT_BY_TICKET);
   if (OrdersTotal() != 0)
     {
      ticket = OrderClose(ticket,lot,Bid,slippage);
     }
   else
     {
      (OrderCloseTime() < CurrentBar);
      openSell();
     }
  }

This whole section doesn't really make any sense. There are expressions with no effect trying to use OrderCloseTime, you're assigning the value of OrderSelect (true / false) to ticket. Closing orders. I don't know what you're trying to do, but I'm sure it can be done more simply ;-)
 


 

 
honest_knave:
extern double lot = 1.0;
extern int sl = 100;
extern int tp = 100;
extern int tf = 0;
extern int slippage = 5;

double DailyOpen     =   iOpen(NULL,PERIOD_D1,0);
double CurrentBar    =   iOpen(NULL,0,0);

You shouldn't assign values obtained from iOpen here; data might not be available yet.

bool trendLong()
  {
   if (CurrentBar > DailyOpen) return(true);
   else return(EMPTY_VALUE);
  }

A bool should return either true or false. Not EMPTY_VALUE, which is 2147483647, which will be interpreted as true.

The way you have written it, trendLong() always returns true. And so does trendShort().

void OnTick()
  {  
   if(trendLong() == true) ticket = OrderSelect(ticket,SELECT_BY_TICKET);
   if(OrdersTotal() != 0)
     {
      ticket = OrderClose(ticket,lot,Bid,slippage);
     }
   else
     {
      (OrderCloseTime() < CurrentBar);
       openBuy();
     }
   if (trendShort() == true)
   ticket = OrderSelect(ticket,SELECT_BY_TICKET);
   if (OrdersTotal() != 0)
     {
      ticket = OrderClose(ticket,lot,Bid,slippage);
     }
   else
     {
      (OrderCloseTime() < CurrentBar);
      openSell();
     }
  }

This whole section doesn't really make any sense. There are expressions with no effect trying to use OrderCloseTime, you're assigning the value of OrderSelect (true / false) to ticket. Closing orders. I don't know what you're trying to do, but I'm sure it can be done more simply ;-)
 


 

Wow honest_knave!! THANK YOU! I was assigning iOpen values when they were not even there. Okay. So, I have simplified my attempt by removing extra functions and what not from code. Now, I see that EA does open both Buy and Sell trades. The next thing I am trying to do here is that if a condition becomes false then EA should close position and open a trade in favor of new condition. I am pasting my code here. Please review. Many many thanks for your advice and guidance.

----------------------------------------------------------------- 

extern double lot = 1.0;
extern int sl = 100;
extern int tp = 100;
extern int slippage = 5;
int ticket;

void OnTick()
   {
   double DailyOpen     =   iOpen(NULL,PERIOD_D1,0);
   double CurrentBar    =   iOpen(NULL,0,0);

   if (CurrentBar > DailyOpen)
      {  
     ticket = OrderSelect(ticket,SELECT_BY_TICKET);     //trying to use this to check if any ticket is previously open
     ticket = OrderClose(ticket,lot,Bid,slippage);      //trying to close any previously open ticket
     if (OrdersTotal() == 0)
     ticket = OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-sl*Point,Ask+tp*Point,"EA Trade");
      }
   else
      {
      ticket = OrderSelect(ticket,SELECT_BY_TICKET);
      ticket = OrderClose(ticket,lot,Bid,slippage);    
      if (OrdersTotal() == 0)
      ticket = OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,Bid+sl*Point,Bid-tp*Point,"EA Trade");
      }  
    }
 

I've edited your post to use SRC.

Thinking through your logic:

if (CurrentBar > DailyOpen)

When this evaluates as true, you need to:

1. Close any short positions

2. Open a long position, if you haven't already.

You could have a variable to remember if you have already opened an order.

However, if the overhead isn't too much I always suggest checking the information rather than relying upon a saved variable.

This means your EA is more robust and can recover from restarts or in case something else closes out a position etc.

See if this makes sense to you. 

   if(CurrentBar > DailyOpen)
     {  
      bool buy_order_found = false;                      // presume there isn't a buy order
      for(int i=OrdersTotal()-1; i>=0; i--)              // loop through all the orders in the terminal
        {
         if(!OrderSelect(i,SELECT_BY_POS))  continue;    // if unable to select the order, move on
         if(OrderSymbol() != _Symbol)       continue;    // if a different symbol, move on
         if(OrderMagicNumber() != magic_no) continue;    // if a different magic number, move on
         int order_type = OrderType();
         if(order_type == OP_SELL)                       // if a sell order found, close it
           {
            if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage))
              {
               printf("OrderClose() failed. Error code: %i",GetLastError());
              }
           }
         else if(order_type == OP_BUY) buy_order_found = true;  // if a buy order found, change the flag
        }
      if(!buy_order_found)      // if no buy orders were found, open one
        {
         RefreshRates();
         if(OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-sl*Point,Ask+tp*Point,"EA Trade",magic_no)<0)
           {
            printf("OrderSend() failed. Error code: %i",GetLastError());
           }
        }
     }


 

 
honest_knave:

I've edited your post to use SRC.

Thinking through your logic:

if (CurrentBar > DailyOpen)

When this evaluates as true, you need to:

1. Close any short positions

2. Open a long position, if you haven't already.

You could have a variable to remember if you have already opened an order.

However, if the overhead isn't too much I always suggest checking the information rather than relying upon a saved variable.

This means your EA is more robust and can recover from restarts or in case something else closes out a position etc.

See if this makes sense to you. 

   if(CurrentBar > DailyOpen)
     {  
      bool buy_order_found = false;                      // presume there isn't a buy order
      for(int i=OrdersTotal()-1; i>=0; i--)              // loop through all the orders in the terminal
        {
         if(!OrderSelect(i,SELECT_BY_POS))  continue;    // if unable to select the order, move on
         if(OrderSymbol() != _Symbol)       continue;    // if a different symbol, move on
         if(OrderMagicNumber() != magic_no) continue;    // if a different magic number, move on
         int order_type = OrderType();
         if(order_type == OP_SELL)                       // if a sell order found, close it
           {
            if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage))
              {
               printf("OrderClose() failed. Error code: %i",GetLastError());
              }
           }
         else if(order_type == OP_BUY) buy_order_found = true;  // if a buy order found, change the flag
        }
      if(!buy_order_found)      // if no buy orders were found, open one
        {
         RefreshRates();
         if(OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-sl*Point,Ask+tp*Point,"EA Trade",magic_no)<0)
           {
            printf("OrderSend() failed. Error code: %i",GetLastError());
           }
        }
     }


 

AWESOME!! Thank you very much!! that's like a mentor. Many thanks!