Download MetaTrader 5
To add comments, please log in or register
Have you developed an application? Publish it in Code Base!
chua le
234
chua le 2015.04.17 06:28 

My Oredrclose is not working, please help.

 

      if (total>0)

        {

        if(bar3<-1) OrderClose(ticket,OrderLots(),Bid,3,Green);

        if(bar3>1)OrderClose(ticket,OrderLots(),Ask,3,Red);

        return (0);

       } 

Keith Watford
Moderator
7955
Keith Watford 2015.04.17 07:54  

As we have no idea of the values of

total

bar3

ticket

OrderLots()   (as we don't see an order selected)

How do we guess?

Check the return of OrderClose 

 

Does ticket refer to a Buy order or a Sell order?   It seems that if certain conditions apply you are trying to close at bid, if different conditions, you are trying to close the same type of order at ask

chua le
234
chua le 2015.04.17 08:32  

Hi,

Thank you 

This is the complete listing: see the highlighted portion, do i need to put the return(0) there, what is the different if there is no return(0) there?

 

 

 

//+------------------------------------------------------------------+
//|                                                 chuale_test1.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.abc.com"
#property version   "1.00"
#property strict

extern double   TakeProfit=10000;
extern double   Lots=0.1;
extern double   StopLoss=300;

double     bar1;
double     bar2;
double     bar3;
int        total,ticket;
datetime   previousTime;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//---

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---

   return(0);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
  {
   total=OrdersTotal();
/*
  counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
  IndicatorCounted() is actually for indicators not EA
  
   */

   bar1=(iClose(Symbol(),PERIOD_M1,2)-iClose(Symbol(),PERIOD_M1,3))*10000;
   bar2=(iClose(Symbol(),PERIOD_M1,1)-iOpen(Symbol(),PERIOD_M1,1))*10000;
   bar3=(iClose(Symbol(),PERIOD_M1,0)-iOpen(Symbol(),PERIOD_M1,0))*10000;
   
   Comment("Bar1 = "+DoubleToString(bar1,Digits)+"\n"+
           "Bar2 = "+DoubleToString(bar2,Digits)+"\n"+
           "Bar3 = "+DoubleToString(bar3,Digits)+"\n"+
           "Total="+DoubleToString(total,Digits)+"\n"
           );
   
//Close any orders if conditions are met
        if(total>0)
        {
        if(bar3<-1) OrderClose(ticket,OrderLots(),Bid,3,Green);
        if(bar3>1) OrderClose(ticket,OrderLots(),Ask,3,Red);
        return (0);
       }
  //if(Time[0]==previousTime) return(0);         //EA will not go past this point unless it is a new bar
  // previousTime=Time[0];                        // If it was a new bar , it's old now but continue to the end
// if no order open and new bar
   if(total<1)
     {
      if(bar2<0 && bar3>1)
     // if(bar3<-12)SendNotification ("Bar3 <-12 for "+Symbol());
      //if(bar2+bar3>20)SendNotification ("Bar2+Bar3 >20 for "+Symbol());
      //if(bar2+bar3<-20)SendNotification ("Bar2+Bar3 <-20 for "+Symbol());
      // if condition met
         {
         //place buy order
          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Chua EA",12345,0,Green);
          if(ticket<0)Print("Error OP_BUY order failed. error : "+GetLastError()); // if returned ticket is -1 OrderSent failed
          return(0);
         }
      // or if conditions for sell met
       if (bar2>0 && bar3<-1)
           {
           ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid+-TakeProfit*Point,"Chua EA",12345,0,Red);
           if(ticket<0)Print("Error OP_SELL order failed. error : "+GetLastError());
           return(0); //return to start
           }
       }
   return(0);
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
7955
Keith Watford 2015.04.17 12:51  
What is the value of OrderLots()   (as we still don't see an order selected)
whroeder1
13627
whroeder1 2015.04.17 12:54  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. As GumRai said, you can not use OrderLots unless you select the order first. Once you select the order, you can use OrderClosePrice instead of Bid/Ask.
  3. As GumRai said, Check your return codes (OrderClose) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  4. As GumRai said, where do you test the order type? Why would you close a buy order when the current candle is rising?
chua le
234
chua le 2015.04.18 05:17  

Hi,

 

How shall i place the orderselect? 

chua le
234
chua le 2015.04.18 05:26  

Hi,

 

May I know how many ways of select orders and close orders? I know select by symbol. 

Keith Watford
Moderator
7955
Keith Watford 2015.04.18 05:56  
chuale:

Hi,

 

May I know how many ways of select orders and close orders? I know select by symbol. 

 I don't know what that is.

There is, as far as I know, only one way to select an order and that is with OrderSelect() 

chua le
234
chua le 2015.04.21 04:24  
I manage to use OrderSelect() to resolve the problem. So now if I use MagicNumber , can I assign MagicNumber=1 to chart 1 and MagicNumber=2 and so on? 
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2015.04.21 04:58  
chuale:
I manage to use OrderSelect() to resolve the problem. So now if I use MagicNumber , can I assign MagicNumber=1 to chart 1 and MagicNumber=2 and so on? 
Yes
/
To add comments, please log in or register