Download MetaTrader 5

Close order next tick

To add comments, please log in or register
m
105
m 2013.12.04 08:03 

Hello, Have anyone any ideas for such problem:

when an ea sent order and transaction was done (platform X) after next tick order closed (loss depends on next tick - less or more than spread). Next, ea sent order (the same direction) and everything was ok to the next transaction. Event starts again, but not on the every platform (using platform Y everything from start is ok). To send order I use code below. Abc is condition.

if (OrdersTotal()==0 && abc)
   {
      RefreshRates();
      Ticket=OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0);
   }
if (OrdersTotal()==0 && abc)
   {
      RefreshRates();
      Ticket=OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,0);
   }
  
Alert (GetLastError());
}
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.12.04 09:45  
kot_filemon:

Hello, Have anyone any ideas for such problem:

when an ea sent order and transaction was done (platform X) after next tick order closed (loss depends on next tick - less or more than spread). Next, ea sent order (the same direction) and everything was ok to the next transaction. Event starts again, but not on the every platform (using platform Y everything from start is ok). To send order I use code below. Abc is condition.


Handle your errors properly, just Alerting GetLastError() is pointless . . . What are Function return values ? How do I use them ?

Your OrderSend() is not sending a SL or TP are you adding this later ? or are you closing the order in your code ?

m
105
m 2013.12.04 12:38  

Code is without any kind of SL and TP. Only buy-sell signal (alternately). Below is code for opposite close orders I use.

for (int i=OrdersTotal()-1;i>=0;i--)
   {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      if (OrderSymbol()==Symbol())
      RefreshRates();
      if (Type==OP_BUY && abc ){OrderClose(OrderTicket(),OrderLots(),Bid,2);}
      RefreshRates();
      if (Type==OP_SELL && abc){OrderClose(OrderTicket(),OrderLots(),Ask,2);}
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.12.04 14:02  
kot_filemon:

Code is without any kind of SL and TP. Only buy-sell signal (alternately). Below is code for opposite close orders I use.

You want to close any Order even if it doesn't match the Symbol for the chart the EA is on ? that is what you are doing. Do you really need the RefreshRates() ? twice ? if your OrderSelect() fails you still try to close the Orders . . . why ?

Add some { } braces to make the picture clearer . . .

Also, check the return values from the OrderClose() calls and report any errors and relevant variables: What are Function return values ? How do I use them ?

whroeder1
14737
whroeder1 2013.12.04 14:07  
for (int i=OrdersTotal()-1;i>=0;i--)
   {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) if (OrderSymbol()==Symbol()) RefreshRates();
      if (Type==OP_BUY && abc ){OrderClose(OrderTicket(),OrderLots(),Bid,2);}
Do you think that code does what you want it to?
m
105
m 2013.12.10 17:40  

I tried write new one, but I have still the same problem.


for (int i=OrdersTotal()-1;i>=0;i--)
   {
     if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
       {
         if (OrderMagicNumber()==MagicNumber)
           {
            if (Type==OP_BUY && (ma<ma1))
              {
               RefreshRates();
               OrderClose(OrderTicket(),OrderLots(),Bid,1);
              }
              else return;
            if (Type==OP_SELL && (ma>ma1))
              {
               RefreshRates();
               OrderClose(OrderTicket(),OrderLots(),Ask,1);
              }
              else return;
            }
            break;
         }
         break;
      }
          
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.12.10 18:08  
kot_filemon:

I tried write new one, but I have still the same problem.

Why are you asking for more help when you do not take the advice already given ?

RaptorUK:


Also, check the return values from the OrderClose() calls and report any errors and relevant variables: What are Function return values ? How do I use them ?

m
105
m 2013.12.10 18:51  

It is very simple. I took advice, read your link, and in the my opinion it should works, but didn't work correctly:)

OrderSend is type int, and gives MagicNumber. I set up magic number as 1234. OrderSelect() is a bool type (true or false).

1. first orderSelect I check if it is ok. I set up OrderSelect as false (if it is true, break a function. if not continue)

2. check OrderMagicNumber. If it is the same continue, if not return from a loop.

3. If OrderMagicNumber is the same, open order is buy and criterion is met ea sends OrderClose.

I saw few tutorials similar my past code.
m
105
m 2013.12.10 19:05  
Here is the code from MT4 platform (built in)
 for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
. I don't have OrderSymbol().
m
105
m 2013.12.10 19:37  
And code above has the same problem.
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.12.10 20:24  
kot_filemon:

It is very simple. I took advice, read your link, and in the my opinion it should works, but didn't work correctly:)

OrderSend is type int, and gives MagicNumber.

No, it doesn't . . . it gives the ticket number if the OrderSend() worked . . . why are you checking if the orderSend() has worked ? don't you want to know if it fails ?
12
To add comments, please log in or register