NEED HELP!! Newbie here trying to recreate EA... found error..

 

Hi, i already recreate an EA to what i want but i'm having issues... all error been fix.. but the EA close all pairs orders.. I just want the EA to close on 1 pair which the EA been placed... Could tell me where is the error?

extern double Price_Target=0.0;     //The Price at which you want to delete & close all orders.
extern string Profit_Target= "ENTER ABOVE THE PRICE YOU TARGET";
extern bool SELL = false;                                    
extern bool BUY = false; 
                                          
double vbid= MarketInfo(Symbol(),MODE_BID);
double vask= MarketInfo(Symbol(),MODE_ASK);
                                             
int Slippage=5;
int i;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+


int start()
  {
   if((BUY && iClose(NULL,0,1)<=Price_Target && Ask <= Price_Target) || (SELL && iClose(NULL,0,1)>=Price_Target && Bid >= Price_Target))

   {
    for(i=OrdersTotal()-1;i>=0;i--)
       {
       OrderSelect(i, SELECT_BY_POS);
       int type   = OrderType();
               
       bool result = false;
              
       switch(type)
          {

          case OP_BUYLIMIT  : result = OrderDelete( OrderTicket() );
          case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );
          case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );
          case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
          case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );      
          case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                                  
          }
          
       if(result == false)
          {
            Sleep(0);
          }  
       }
      Print ("Price Reach!! All Orders Have Been Closed & Deleted");
      return(0);
   }  

   
   Comment("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit(),
           "\nMy Account Profit Target: ",Price_Target);
   
  return(0);
}

 

Is the error on either of this?

          case OP_BUYLIMIT  : result = OrderDelete( OrderTicket() );
          case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );
          case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );
          case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
          case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );      
          case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

 or

    for(i=OrdersTotal()-1;i>=0;i--)
       {
       OrderSelect(i, SELECT_BY_POS);

And also,

iClose(NULL,0,1)

 NULL is the current symbol right? followed by 0 means the current TF am i right? or i must put NULL in order for it to get the info from current TF?


Thank You
 
Mohammad Rizal Bin Rahmat:

Hi, i already recreate an EA to what i want but i'm having issues... all error been fix.. but the EA close all pairs orders.. I just want the EA to close on 1 pair which the EA been placed... Could tell me where is the error?

 

Hi Mohammad,

two things I'm missing in the code of the start procedure:

int start()
  {
   if((BUY && iClose(NULL,0,1)<=Price_Target && Ask <= Price_Target) || (SELL && iClose(NULL,0,1)>=Price_Target && Bid >= Price_Target))

   {
    for(i=OrdersTotal()-1;i>=0;i--)
       {
       OrderSelect(i, SELECT_BY_POS);
       if (OrderSymbol() != Symbol()) continue;
       
       int type   = OrderType();
               
       bool result = false;
              
       switch(type)
          {

          case OP_BUYLIMIT  : result = OrderDelete( OrderTicket() );break;
          case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );break;
          case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );break;
          case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );break;
          case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );break  
          case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );break;
                                  
          }
          
       if(result == false)
          {
            Sleep(0);
          }  
       }
      Print ("Price Reach!! All Orders Have Been Closed & Deleted");
      return(0);
   }  

   
   Comment("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit(),
           "\nMy Account Profit Target: ",Price_Target);
   
  return(0);
}

 

See the added statements written in bold letters:

  • for limiting the orders on your current symbol you have to add   

if (OrderSymbol() != Symbol()) continue;

  • do not forget the break statements within the switch clause

iClose(NULL,0,1)

 is correct

 

Matthias/Bobcat 

 
Dr Matthias Hammelsbeck:

Hi Mohammad,

two things I'm missing in the code of the start procedure:

 

See the added statements written in bold letters:

  • for limiting the orders on your current symbol you have to add   

  • do not forget the break statements within the switch clause

 is correct

 

Matthias/Bobcat 

Thank you Dr Matthias. did the changes and all is good and working as to what i want. thank you so much for guiding me.
 
Dr Matthias Hammelsbeck:

Hi Mohammad,

two things I'm missing in the code of the start procedure:

 

See the added statements written in bold letters:

  • for limiting the orders on your current symbol you have to add   

  • do not forget the break statements within the switch clause

 is correct

 

Matthias/Bobcat 

Dr Matthias,

Could you explain to why i need to put a the break statements within the switch clause? thank you. just for my learning.
 
Mohammad Rizal Bin Rahmat:
Dr Matthias,

Could you explain to why i need to put a the break statements within the switch clause? thank you. just for my learning.

You can inform you further about the switch statement eg in the MQL4 Reference / Language Basics / Operators / Switch Operator

 

or  look into the following page: 

 http://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm


Matthias/Bobcat

switch statement in C
switch statement in C
  • tutorialspoint.com
  • www.tutorialspoint.com
switch statement in C - Learn ANSI, GNU and K/R standard of C programming language with simple and easy examples covering basic C, language basics, literals, data types, functions, loops, arrays, pointers, structures, input and output, memory management, pre-processors, directives etc.
 
Mohammad Rizal Bin Rahmat:
Thank you Dr Matthias. did the changes and all is good and working as to what i want. thank you so much for guiding me.
You are welcome
Reason: