Cant close my Order if price move the other way

 

Hello,

 

Im trying to fix my EA for 12 hours until now, Every thing going smooth and my orders open as I want to,

But I cant get them close if my pair change direction...

 

Here is it :

 

//+------------------------------------------------------------------+
//|                          45435555555555555555555555555555555.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"


extern bool     AllowBuy=true;
extern bool     AllowSell=true;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
   
   
   
for (int i = OrdersTotal() - 1; i>=0; i--)
if (OrderSelect(i, SELECT_BY_POS)
&&  OrderSymbol() == Symbol()
   ) return;
   
//-------------------------------------------------------------------+
 
 
if(AllowBuy==true){// halt1
 
// Buy criteria
if ((Hour()==00)&&(Minute()==00)&&(
      iOpen("AUDJPY",PERIOD_D1,1)<iClose("AUDJPY",PERIOD_D1,1)))  //Signal Sell
 {
   int openbuy=OrderSend(Symbol(),OP_BUY,1,Ask,0,0,0); 
OrderModify(openbuy,OrderOpenPrice(),Bid-99999*Point,Ask+99999*Point,0,Blue);
   if(openbuy<1){int buyfail=1;}
 }
 
}// halt1
 
if(AllowSell==true){// halt2
RefreshRates();
 // Sell criteria
 if ((Hour()==00)&&(Minute()==00)&&(
      iOpen("AUDJPY",PERIOD_D1,1)>iClose("AUDJPY",PERIOD_D1,1)))  //Signal Sell
 {
   int opensell=OrderSend(Symbol(),OP_SELL,1,Bid,0,0,0);
 OrderModify(opensell,OrderOpenPrice(),Ask+99999*Point,Bid-99999*Point,0,Blue);
   if(opensell<1){int sellfail=1;}
 }
 
}// halt2
   
//-----------------------------------------------------------------------------------------------------

THIS SECTION, its not working

if(OrdersTotal()>0 && OrderType()==OP_BUY) { if(       iOpen("GBPJPY",PERIOD_M30,1)<iClose("GBPJPY",PERIOD_M30,1)&&iOpen("GBPUSD",PERIOD_M30,1)<iClose("GBPUSD",PERIOD_M30,1)&&iOpen("AUDJPY",PERIOD_M30,1)>iClose("AUDJPY",PERIOD_M30,1))           { OrderClose(OrderTicket(),OrderLots(),Bid,100); } } if(OrdersTotal()>0 && OrderType()==OP_SELL) { if(       iOpen("GBPJPY",PERIOD_M30,1)>iClose("GBPJPY",PERIOD_M30,1)&&iOpen("GBPUSD",PERIOD_M30,1)>iClose("GBPUSD",PERIOD_M30,1)&&iOpen("AUDJPY",PERIOD_M30,1)<iClose("AUDJPY",PERIOD_M30,1))           { OrderClose(OrderTicket(),OrderLots(),Ask,100); } }                //----    return(0);   } //+------------------------------------------------------------------+

 

I would appreciate your help,

Thanks 

 
Leopardos:

Hello,

 

Im trying to fix my EA for 12 hours until now, Every thing going smooth and my orders open as I want to,

But I cant get them close if my pair change direction...


Bearing in mind this code which returns from start() if the OrderSelect() works and the order selected is for the chart symbol . . .

for (int i = OrdersTotal() - 1; i>=0; i--)
   if ( OrderSelect(i, SELECT_BY_POS)
      &&  OrderSymbol() == Symbol() ) 
    
      return;

  . . .  what Order is selected when you call  OrderTicket() and OrderLots()  ?   Don't you want to know when your calls to OrderXxxxx() fail ? for example,  wouldn't you like to know the error code if your OrderClose() fails ?  Read this:  What are Function return values ? How do I use them ?

 

Your not working section is never reached by your code.

You return at the beginning of the start() function if OrderSelect() does not fail.  

for (int i = OrdersTotal() - 1; i>=0; i--)
if (OrderSelect(i, SELECT_BY_POS)
&&  OrderSymbol() == Symbol()
   ) return;

Add some print() statements to find errors like that one.

 
RaptorUK:

Bearing in mind this code which returns from start() if the OrderSelect() works and the order selected is for the chart symbol . . .

  . . .  what Order is selected when you call  OrderTicket() and OrderLots()  ?   Don't you want to know when your calls to OrderXxxxx() fail ? for example,  wouldn't you like to know the error code if your OrderClose() fails ?  Read this:  What are Function return values ? How do I use them ?

  

Well, I cant fix it, when ever I adjust something , I get a lot of trades per second, or my order wont close ...

I had to write this code to get no more than 1 trade per second while testing ..

Could you help me with it and getting the order to close if iOpen>...... happens? I would appreciate your help a lot..

 

Thanks 

 

So , this is what I got so far:

 

for (int i = OrdersTotal() - 1; i>=0; i--)
if (OrderSelect(i, SELECT_BY_POS == true)
   ) return;
   else
if(
      iOpen("GBPJPY",PERIOD_M30,1)<iClose("GBPJPY",PERIOD_M30,1)&&iOpen("GBPUSD",PERIOD_M30,1)<iClose("GBPUSD",PERIOD_M30,1)&&iOpen("AUDJPY",PERIOD_M30,1)>iClose("AUDJPY",PERIOD_M30,1))

          { OrderClose(OrderTicket(),OrderLots(),Bid,100); }



if(
      iOpen("GBPJPY",PERIOD_M30,1)>iClose("GBPJPY",PERIOD_M30,1)&&iOpen("GBPUSD",PERIOD_M30,1)>iClose("GBPUSD",PERIOD_M30,1)&&iOpen("AUDJPY",PERIOD_M30,1)<iClose("AUDJPY",PERIOD_M30,1))

          { OrderClose(OrderTicket(),OrderLots(),Ask,100); }

 

 I just cant get it and im frustrated after 12 hours of trying ...

Just how to make it, if true then return BUT to my Close order script and not to open a new one..

 

 

Thanks again 

 
Leopardos:

 

Just how to make it, if true then return BUT to my Close order script and not to open a new one..

Why do you not use your defined variables AllowBuy and AllowSell? Set AllowBuy to false when you opened a buy order, reset it to true when you close it. The same for AllowSell? Do I miss something... ?

 
OrderModify(openbuy,OrderOpenPrice(),Bid-99999*Point,Ask+99999*Point,0,Blue);
What are Function return values ? How do I use them ? - MQL4 forum
Reason: