Download MetaTrader 5

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

To add comments, please log in or register
Mohammad Rizal Bin Rahmat
284
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?

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
Frederic Metraux
513
Frederic Metraux  

Hello,


You must check symbol with OrderGetString(ORDER_SYMBOL)

Ernst Van Der Merwe
4802
Ernst Van Der Merwe  
          if(!OrderSelect(i, SELECT_BY_POS) || OrderSymbol()!=_Symbol)
            continue;
          int type   = OrderType();
                  
          bool result = false;
                 
          switch(type)
             {
   
             case OP_BUYLIMIT  : result = OrderDelete( OrderTicket() ); continue;
             case OP_BUYSTOP   : result = OrderDelete( OrderTicket() ); continue;
             case OP_SELLLIMIT : result = OrderDelete( OrderTicket() ); continue;
             case OP_SELLSTOP  : result = OrderDelete( OrderTicket() ); continue;
             case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), Bid, 50, clrRed ); continue;      
             case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), Ask, 50, clrRed );
                                     
             }

btw this expression:

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

does the opposite of this:

If BUY is true, when anytime a Candle close Ask Price is above or equal to "Price Target", it will close and delete all orders
If SELL is true, when anytime a Candle Close Bid Price is below or equal to "Price Target", it will close and delete all orders.

should be:

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


To add comments, please log in or register