Just confirming how the broker side of MT4 handles a trade

 

Hi,

I certainly appreciate the willingness of everyone here to help--it has benefitted me greatly.  And I am sorry some are tired (qjol et al.)  Some day I do hope to donate to say thanks.

In testing my demo account I notice that sometimes an order can be filled in stages.  Say I send in an order for 1.5 lots of the EUR/USD pair (ECN).  I might show three trades that total 1.5 lots in the Trade tab or in the Order History Tab once all are closed out.  in code I send in the order, record the ticket if the send is successful, and use that ticket to modify the order for a stop loss and profit stop.

Each partial fill has a ticket # in the Order History.  It appears that even though I am using the original ticket # the server side recognizes that that order was split into three small orders during the fill process.  So when I modify the order using the original ticket the server is able to correctly identify and modify the partial fill even though it has a different ticket#.  Am I reading this correctly?

As always many thanks for the input.

N

 

Nah. Your coding may have some left over memory in handling the ticket number. I also have the same problem last time. 

Broker always done request based on your request. So, it must be your coding logic.

Show us your code and we will help you if we can. Please use SRC (besides video icon) to post codes so that it is easy to view.

 
niqmadu:

Hi,

I certainly appreciate the willingness of everyone here to help--it has benefitted me greatly.  And I am sorry some are tired (qjol et al.)  Some day I do hope to donate to say thanks.

In testing my demo account I notice that sometimes an order can be filled in stages.  Say I send in an order for 1.5 lots of the EUR/USD pair (ECN).  I might show three trades that total 1.5 lots in the Trade tab or in the Order History Tab once all are closed out.  in code I send in the order, record the ticket if the send is successful, and use that ticket to modify the order for a stop loss and profit stop.

Each partial fill has a ticket # in the Order History.  It appears that even though I am using the original ticket # the server side recognizes that that order was split into three small orders during the fill process.  So when I modify the order using the original ticket the server is able to correctly identify and modify the partial fill even though it has a different ticket#.  Am I reading this correctly?

As always many thanks for the input.

N

It's seems weird. Which broker are you using ? Can you show us your trade's history ?And maybe some source code, as requested by deysmacro.
 
niqmadu: It appears that even though I am using the original ticket # the server side recognizes that that order was split into three small orders during the fill process. in code I send in the order, record the ticket if the send is successful, and use that ticket to modify the order for a stop loss and profit stop.
deysmacro: Nah. Your coding may have some left over memory in handling the ticket number. I also have the same problem last time. Broker always done request based on your request. So, it must be your coding logic.
  1. Almost all brokers open one order per your OrderSend. So your one ticket number is sufficient and I agree with deysmacro it's likely a problem with your code.
  2. prupru posted last year about partially executed orders. Thus my code may open multiple orders.
  3. I also remember seeing another post about a broker that opens one partially, and other orders completing the original send appear (up to) minutes later. This is why I do NOT remember the ticket numbers, I ONLY use a OrderSelect loop. niqmadu what do you do when the terminal restarts? OS crash, power glitch, accidentally close the terminal or a chart: you've lost the ticket numbers. EA's must be coded to recover.
  4. Having to set TP/SL after the initial OrderSend is no longer required for ECN IIRC.
 
WHRoeder:
niqmadu: It appears that even though I am using the original ticket # the server side recognizes that that order was split into three small orders during the fill process. in code I send in the order, record the ticket if the send is successful, and use that ticket to modify the order for a stop loss and profit stop.
deysmacro: Nah. Your coding may have some left over memory in handling the ticket number. I also have the same problem last time. Broker always done request based on your request. So, it must be your coding logic.
  1. Almost all brokers open one order per your OrderSend. So your one ticket number is sufficient and I agree with deysmacro it's likely a problem with your code.
  2. prupru posted last year about partially executed orders. Thus my code may open multiple orders.
  3. I also remember seeing another post about a broker that opens one partially, and other orders completing the original send appear (up to) minutes later. This is why I do NOT remember the ticket numbers, I ONLY use a OrderSelect loop. niqmadu what do you do when the terminal restarts? OS crash, power glitch, accidentally close the terminal or a chart: you've lost the ticket numbers. EA's must be coded to recover.
  4. Having to set TP/SL after the initial OrderSend is no longer required for ECN IIRC.

Great points from all of you--thanks.  To WHRoeder's points

1.  What I think is happening is that the OrderModify is being processed before the original order is filled via partial fills or one complete one. Thus so far it has worked.  But once in the past I had what I now recognize as an order that was filled via two partial orders prior to OrderModify adding the SL/TP.  One ticket had the SL/TP, the other had neither.  I couldn't figure out what happened so I wrote a routine to check for zombie orders (orders or positions without a SL/TP) and close them.

2.  I'll dig through your provided links.  I am just getting going this morning.

3.  An OrderSelect loop makes great sense.  I don't remember the ticket number beyond the scope of the Buy/Sell subroutine.  I do track the time each order is placed and limit the frequency an order can be placed.  This is more to prevent multiple stop-outs on the same stale signal on an adverse news event.

4.  RE ECN.  This would be great.  I will test to verify.


This is the code for opening Long positions, there is a very similar Sell function.  I'll add an orderselect loop later today after reading through everything.


int buy()
   {
            
      double  vol  = ORDER_VOL;
      double  slip = 3 / POINT_ADJUSTMENT;

      double price = Ask;
      double    sl = 0;   
      double    ps = 0;   
      int      cmd = OP_BUY;
      bool     ord = true;      
   
      Alert("Sending order BUY on chart ",Period(),"  ", Symbol(),"  VOL=", vol, "   ", price, slip, sl, ps);
      int Ticket = OrderSend( Symbol(), cmd, vol, price, slip, sl, ps, NULL, MAGIC, 0, ACOL);
      RefreshRates();      
      if (Ticket>-1)
      {
        if(!OrderSelect( Ticket, SELECT_BY_TICKET))
        { 
           Alert("Could not select order ", Ticket, " for TP/SL.");
           return(0);
        }
        
        double op = OrderOpenPrice();          
        
        ps = op + TP*Point;      
        if (TP==0) ps=0;
        
        sl = op - TS*Point;   
        if (TS==0) sl=0;

        if(!OrderModify( Ticket, op, sl, ps, 0, White)) Alert("Order Modify failed");          
        LAST_OPEN_TIME = TimeCurrent();        
        return(0);
      } 
       
      //IF Arrived here then error on trade
      int LastError = GetLastError();
      string LastDesc = ErrorDescription(LastError);
      Print("Error Attempting to Buy=" + LastError);
      return(0);
   

   }
 
angevoyageur:
It's seems weird. Which broker are you using ? Can you show us your trade's history ?And maybe some source code, as requested by deysmacro.

Below are the trades on one of the demo accounts.  These are market orders.  The Lotsize is 2.9 but as you can see some of the orders are filled singly and some jointly.

I think that the order is being modified prior to it being filled as everything here has a SL/TP

Reason: