Multiple orders problem - page 4

 
luisneves:

Hi Kronin,

 Your modification is working. You have limited the max orders, bur is that possible to get them all closed, instead ?

I've try to do that with the following code, but seems to  not  work.

 Best Regards

Luis 

Hmmm, I am not sure I understand what you mean...
Do you mean, instead of open the last order (order 7 if MaxOrders is set to 7) close all orders?

I am quite sure you dont want to wait until MaxOrders is reached and then immediately close all. The last order would most of the time (or all the time) end with a loss.
Btw. Have you seen the PM about spread controller

 
kronin:

Hmmm, I am not sure I understand what you mean...
Do you mean, instead of open the last order (order 7 if MaxOrders is set to 7) close all orders?

I am quite sure you dont want to wait until MaxOrders is reached and then immediately close all. The last order would most of the time (or all the time) end with a loss.
Btw. Have you seen the PM about spread controller? 


Hi Kronin,

  Thank you for your prompt response.

  Say that after a ping pong cycle of n orders (MaxOrders) all open orders should be closed, I know that could not make sense, but even so.  

 I'm trying to include code for Reentry the order in case it fails and have doubts if that make sense because there is already the code of IsContextBusy () Sleep (10). As far I understand, the meaning is " if server is busy or other ea in the platform is attempting to communicate, then wait 10 milliseconds and try again". So I suppose that the EA will maintain this behavior  until a ticket have been received, am I right ?

Regarding PM issue, in that case better to not make use of Test at  weekend or do that with the platform offline....

I've insert line 5, but think that is not necessary because  line 1 you have already do that, am I right ? 

 

int LastClosedTicket=GetTicketFromHistory(Symbol(),MagicNumber); //1 
   if(LastClosedTicket>0)
    {                                                                 
      Print("LastClosedTicket=",LastClosedTicket);                   
      if(OrderSelect(LastClosedTicket,SELECT_BY_TICKET))
      {
      if(OrderSymbol()== Symbol() && OrderMagicNumber()== MagicNumber) //5           
       {            
         if(OrderType()==OP_BUY)
         {
         BuyAllowed=false;SellAllowed=true;
         } 
         else                   
         {
         BuyAllowed=true;SellAllowed=false;
         } 
        }   
      }
    }             
  return(0);   
  }
 
  //+---------------------------------------------------------------------------+

 

Another issue,

In the following piece of code the lines in Bold where is sym  and mn  don't should one replace it with Symbol() and MagicNumber? 

 

GetTicketFromHistory

int GetTicketFromHistory(string sym,int mn,int orderposinhistory=0)// <------------------------Should replace sym and mn with Symbol () and MagicNumber ?
 {
   bool debug=false;
   if(debug)Print("Orders in history: ", OrdersHistoryTotal());
   int ticket,count=1,i;
   datetime orderclosedates[];
   for(i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
      if(debug)Print("OrderSymbol: ",OrderSymbol(),"; OrderMagicNumber: ",OrderMagicNumber(),
                     " ;OrderType: ",OrderType(),"; OrderTicket: ",OrderTicket(),
                     "; OrderCloseTime: ",TimeToStr(OrderCloseTime()),";"); 
      if((OrderMagicNumber()  ==mn) // <---------------------------------------------------------Should replace  mn with  MagicNumber ?
         &&(OrderSymbol()     ==sym)// <---------------------------------------------------------Should replace sym with Symbol () ?
         &&(OrderType()==OP_BUY||OrderType()==OP_SELL))
         {                       
         ArrayResize(orderclosedates,count);
         orderclosedates[count-1]=OrderCloseTime();
         count++;

 

 Here, in following code I've inserted filters as per your advise;

 (//Phil: 

  //Check the total amount of  orders. I would add a filter for market, magic and symbol...//<----------- Not sure what you mean by market filter..... (Phil: market orders, no pending orders)

  //.... but i am lazy now.....it is something for you to do.

 Is that correct ? 

int start()
 {//0 
                                         
  OTLastTick = OTCurrentTick;                      
  OTCurrentTick = OrdersTotal();
     if(OrderSymbol() == Symbol()           //<--------filter for symbol and magic number, but for market (suppose market orders can't understand how....)
      && OrderMagicNumber() == MagicNumber) //<-|
     if(OTCurrentTick == 0 && OTLastTick > 0)
      {
      BuyTrigger = Ask + OpenDistance * pt;
      SellTrigger = Bid - OpenDistance * pt;
      }             
     if(OTCurrentTick >0 )Trail();                   
     if(OTLastTick >= 2                     //<------could you comment the meaning of this line ?                            
     &&OTCurrentTick < OTLastTick
     && OTCurrentTick > 0)
     {
      CloseAllOnSL();return;
     }      
     if(OTCurrentTick >= MaxOrders)return;                                                            
     if(OTCurrentTick > 0)OpenOppositeOrder();                        
     if(OTCurrentTick == 0)
     {
      BuyAllowed = true;
      SellAllowed = true;

 


Best Regards

Luis 

 

I dont comment on the using of functions. RaptorUK did it already a few posts before. Read again and understand why you dont need to change anything in the code given. Even better, understand why you can use it as it is in other EAs.

OrdersTotal() gives back an amount of orders. You probably want to know, the orders belong to your EA or to manual trading or the orders are pending- or already market oreders. I would create a function (int) what gives back the amount of orders the EA has created. 

     if(OTLastTick >= 2 //<------could you comment the meaning of this line ?  
If you have only one order open (less than 2), it doesn't make any sense to call CloseAllOnSL function..... the only open order is obviously not stopped out.

 
kronin:

I dont comment on the using of functions. RaptorUK did it already a few posts before. Read again and understand why you dont need to change anything in the code given. Even better, understand why you can use it as it is in other EAs.

OrdersTotal() gives back an amount of orders. You probably want to know, the orders belong to your EA or to manual trading or the orders are pending- or already market oreders. I would create a function (int) what gives back the amount of orders the EA has created. 

     if(OTLastTick >= 2 //<------could you comment the meaning of this line ?  
If you have only one order open (less than 2), it doesn't make any sense to call CloseAllOnSL function..... the only open order is obviously not stopped out.


Hi Kronin,

Thank you for your response to my issues.

I will put my up most attention to your code as well RaptorUK advise given before.

Best Regards

Luis 

 

Hi Kronin,

 An issue came out and have to do with the use of two EA's. I f I put two EA's in different charts and with different magic number  they do not work at same time. I receive an error as uninit reason 5 but having take a look in the forum can't find information that puts me in the right direction to deal with that issue.

Have you any complementary information ?

Best Regards

Luis 

 
luisneves:

Hi Kronin,

 An issue came out and have to do with the use of two EA's. I f I put two EA's in different charts and with different magic number  they do not work at same time. I receive an error as uninit reason 5 

https://docs.mql4.com/constants/uninit
 

Hi RaptorUK,

Thank you for your prompt response.

In the link provided I found,

REASON_PARAMETERS5Inputs parameters was changed by user.

But with that information can't do nothing( as far I know). Can't understand what inputs parameters have been changed. The ea is the same, only change the magic number to avoid conflicts between the other ea. 

By the way once you are here could you tell me if in the following code one have to introduce code to get the orders been send again while ticket haven't been received the same for closing orders ? 

while(IsTradeContextBusy()) Sleep(10);
          RefreshRates();       
                       
     BuyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,RealSlippage,0,0,"Buy Order",MagicNumber,0,Green);
     if(BuyTicket > -1)

 Best regards

Luis 

 
luisneves:

Hi RaptorUK,

Thank you for your prompt response.

In the link provided I found,

REASON_PARAMETERS5Inputs parameters was changed by user.

But with that information can't do nothing( as far I know). Can't understand what inputs parameters have been changed. The ea is the same, only change the magic number to avoid conflicts between the other ea. 

If you changed the Magic Number . . .

extern int    MagicNumber    = 08012013;

  . . .  and it is an extern  then you have changed a parameter,  the EA restarts and gives reason 5

 

If you want to run your EA on multiple pairs I suggest you do 2 things,  first get your EA working correctly on one pair,  test it fully so you know it is working.  Then, secondly,  introduce a Mutex so your trades are handled when the other EA is not trying to do the same thing.  

 
RaptorUK:

If you changed the Magic Number . . .

  . . .  and it is an extern  then you have changed a parameter,  the EA restarts and gives reason 5

 

If you want to run your EA on multiple pairs I suggest you do 2 things,  first get your EA working correctly on one pair,  test it fully so you know it is working.  Then, secondly,  introduce a Mutex so your trades are handled when the other EA is not trying to do the same thing.  


Hi RaptorUk,

Thank you for your response about the error, I suppose that have change name and magic number and put the second ea into chart without close the platform. 

Regarding the other issue about reentry orders that have to do with a fail in order closing that returned an error as timeout. that's why i don't know if the code as it is will reenter the order close or order send.

Best Regards

Luis 

 
luisneves:


Hi RaptorUk,

Thank you for your response about the error, I suppose that have change name and magic number and put the second ea into chart without close the platform. 

Regarding the other issue about reentry orders that have to do with a fail in order closing that returned an error as timeout. that's why i don't know if the code as it is will reenter the order close or order send.

Best Regards

Luis 

Read through the execution error codes and make your code comply with the information given there, for example . . . 

ERR_TRADE_TIMEOUT128Timeout for the trade has been reached. Before retry (at least, in 1-minute time), it is necessary to make sure that trading operation has not really succeeded (a new position has not been opened, or the existing order has not been modified or deleted, or the existing position has not been closed)
Reason: