Close order next tick

 

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());
}
 
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 ?

 

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);}
 
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 ?

 
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?
 

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;
      }
          
 
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 ?

 

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.
 
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().
 
And code above has the same problem.
 
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 ?
Reason: