Download MetaTrader 5

Modify/manage multiple orders problem? Or Modify order problem?

To add comments, please log in or register
Sell your applications via the Market. It is secure!
Duarte Silva
127
Duarte Silva 2016.08.21 09:58 

Hi,

I'm making an EA to work with fractals and each time that appears an upper/lower fractal and its > or < than EMA it should generate new orders or modify the existing ones and in open orders it should change only the SL.

At beginning it puts the buy stop order, change it and after some candles it open the position. When starts to have several orders of the same type it gives some errors when modify orders... I have made some history orders: 

OrderIndex OrderTicket Open date
BUY_STOP0 111/01/2006 -> Modified in 19/01 -> turns BUY order in 24/01/2006 -> Changes SL in 30/01 -> Closes in 06/02/2006
BUY_STOP1 230/01/2006

After 1st BUY_STOP order closes it changes the index:

OrderIndex OrderTicketOpen Date 
BUY_STOP0230/01/2006 -> Tried to modify at 09/03 (ERROR 130) -> > Tried to modify at 22/03 (ERROR 130) -> Opens at 19/04 -> Tried to modify at 24/04 (ERROR 130)
SELL_STOP1321/02/2006 -> Modified in 02/03 -> Modified in 10/03 -> Modified in 15/03
 BUY_STOP2424/04/2006 -> turns BUY order in 25/04/2006

 At first I was thinking that t was an error on modify order, because it gives Error 130,  but I had this working before changing the modify order function... And I think that SL it OK for the order but i maybe selecting the wrong order...

Now I think that the problem is in the manage/modify function but I'm not seeing where it is!!! I don't understand how index order is working for several orders. I need that every time it appears the initial condition it manages the modify orders of the same type. Example: when appears the low fractal in uptrend i want to change ALL BUY or BUY STOP orders to change the SL value.

 

I specified the OrderType in the ModifyOrders function string: 

void ModifyOrders(string sTypeOrder)
  {
   int CountOrders = 0, IndexSave = 0, OrderTypeNum = 0;
   //Converte o tipo de ordem   
   if(sTypeOrder == "OP_BUY") OrderTypeNum = 0;
   else if(sTypeOrder == "OP_SELL") OrderTypeNum = 1;
   else if(sTypeOrder == "OP_BUYLIMIT") OrderTypeNum = 2;
   else if(sTypeOrder == "OP_SELLLIMIT")  OrderTypeNum = 3;  
   else if(sTypeOrder == "OP_BUYSTOP") OrderTypeNum = 4;
   else if(sTypeOrder == "OP_SELLSTOP")  OrderTypeNum = 5;  
   else OrderTypeNum = -1;
   
   Print("*** Função Modificação de ordens ***: Order Number = ", OrderTypeNum, " TypeOrder: ", sTypeOrder);
    
   for(PositionIndex = OrdersTotal()-1; PositionIndex >= 0; PositionIndex --) //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
     {
      if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES)) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
         {
         if(OrderMagicNumber() == iMagicNum && OrderSymbol() == Symbol() && OrderType() == OrderTypeNum)
            {
            Print("Ordem seleccionada do tipo pretendido. Ticket: ", OrderTicket(), " Tipo:", OrderType(), " Price:", OrderOpenPrice(), " SL:", OrderStopLoss(), " TP:", OrderTakeProfit());
            if (sTypeOrder == "OP_BUY" || sTypeOrder == "OP_SELL")   // No caso das ordens abertas apenas alteramos o stop loss vindo do main
               {
               PriceValue = OrderClosePrice();
               TakeProfitValue = OrderTakeProfit();
               }
            
            Print("Dados para modificação de ordens: Price: ", PriceValue, " SL: ", StopLossValue, " TP: ", TakeProfitValue);
            if(OrderModify(ticket,PriceValue,StopLossValue,TakeProfitValue,0,Green))
              {
               Print("Ordem de modificação bem sucedida.");
              }
            else Print("Erro: Ordem de modificação mal executada!!",GetLastError());
            
            CountOrders = CountOrders + 1;
            Print("Posição do índice de ordens: ", PositionIndex, "; Nº de ordens do mesmo tipo: ", CountOrders);
            }
         }
     }
  }

 

If it is BUY or SELL I only change the SL, on the pending orders I change all value that come from OnTick()...

Can you help me finding the problem?

One more question: when we modify open orders the change doesn't appear on the graphic like BUYSTOP or SELLSTOP order? 

 

The mql4 goes in attachment. Thanks in advance.

Duarte Silva
127
Duarte Silva 2016.08.22 08:44  

Hi,

I have seen the log file and made some code to try to discover whats the problem and I found out that this appened after some changes in the ModifyOrders function that I show on previous comment. Before changes I was interested in manage just one open order of each type but I think that code it wasn't very reliable... Now I want to work with several orders.

 The previous code was:

void ModifyOrders(string sTypeOrder)
  {
   int CountOrders = 0, IndexSave = 0, OrderTypeNum = 0;
   //Converte o tipo de ordem   
   if(!StringCompare(sTypeOrder, "OP_BUY",false)) OrderTypeNum = 0;
   else if(sTypeOrder == "OP_SELL") OrderTypeNum = 1;
   else if(sTypeOrder == "OP_BUYLIMIT") OrderTypeNum = 2;
   else if(sTypeOrder == "OP_SELLLIMIT")  OrderTypeNum = 3;  
   else if(sTypeOrder == "OP_BUYSTOP") OrderTypeNum = 4;
   else if(sTypeOrder == "OP_SELLSTOP")  OrderTypeNum = 5;  
   else OrderTypeNum = -1;
   
   Print("*** Função Modificação de ordens ***: Order Number = ", OrderTypeNum, " TypeOrder: ", sTypeOrder);
    
   for(PositionIndex = OrdersTotal()-1; PositionIndex >= 0; PositionIndex --) //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
     {
      if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES)) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
         {
         if( OrderMagicNumber() == iMagicNum && OrderSymbol() == Symbol() && (OrderType() == OrderTypeNum))
            {
            CountOrders = CountOrders + 1;
            IndexSave = PositionIndex;
            Print("Posição do índice de ordens: ", IndexSave, "; Nº de ordens do mesmo tipo: ", CountOrders);
            }
         }
     }
   if (CountOrders == 1)
      {
      if(OrderSelect(IndexSave,SELECT_BY_POS,MODE_TRADES))
         {
         Print("Ordem seleccionada - Ticket Correto");
         ticket = OrderTicket();
         
         if (sTypeOrder == "OP_BUY" || sTypeOrder == "OP_SELL")   // No caso das ordens abertas apenas alteramos o stop loss
         {
         PriceValue = OrderClosePrice();
         TakeProfitValue = OrderProfit();
         }
         Print("Dados para modificação de ordens: Price: ", PriceValue, " SL: ", StopLossValue, " TP: ", TakeProfitValue);
         if(OrderModify(ticket,PriceValue,StopLossValue,TakeProfitValue,0,Green))
           {
            Print("Ordem de modificação bem sucedida.");
           }
         else Print("Erro: Ordem de modificação mal executada!!");
         }
      else Print("Ordem mal seleccionada - Ticket incorreto");
      }
   if (CountOrders >= 2) Print("#ERRO: Nº de ordens ", sTypeOrder, " superior a 1: ",CountOrders," ordens! ");
  }

 

 I don't understand what was the change the I made to make such error modify orders... For the same order I have two results:

 Previous Code results

 0 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: *** Função Modificação de ordens ***: Order Number = 4 TypeOrder: OP_BUYSTOP

0 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: Index Order: 0; Nº de ordens do mesmo tipo: 1

0 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: Ordem seleccionada - Ticket Correto

0 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: Data for order modify: Price: 1.2125 SL: 1.1795 TP: 1.278499999999999

2 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: modify #2 buy stop 0.10 EURUSD at 1.21250 sl: 1.17950 tp: 1.27850 ok

0 08:14:17 2006.03.09 00:00  teste3_Fractal_v19-08-2016 EURUSD,Daily: Ordem de modificação bem sucedida.

 

Actual Code results (1st comment)

0 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: *** Função Modificação de ordens ***: Order Number = 4 TypeOrder: OP_BUYSTOP

0 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: Ordem seleccionada do tipo pretendido. Ticket: 2 Tipo:4 Price:1.2354 SL:1.201 TP:1.3042

0 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: Data for order modify: Price: 1.2125 SL: 1.1795 TP: 1.278499999999999

3 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: OrderModify error 130

0 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: Erro: Ordem de modificação mal executada!!130

0 07:22:19 2006.03.09 00:00  teste3_Fractal_v20-08-2016 EURUSD,Daily: Index Order: 0; Nº de ordens do mesmo tipo: 1 

 

Why is that for the same values, same index order, same order it gives different results?

To add comments, please log in or register