Finding out when Pending Orders become orders, and then modifying those Orders

 
void DrawOnNewOrderExecution()
{
   double arrowPrice = 0;

   if(OrdersInThisTrade == OrdersTotal())
      {
         if(OrdersTotal() > 0)  //An Open position has been executed
            {
               if (OrderSelect(OrdersInThisTrade-1, SELECT_BY_POS, MODE_TRADES))
                   {
                     if (OrderSymbol() == ChartSymbol() && OrderMagicNumber()== magicnumber)
                        {
                           if (OrderType() == OP_BUY)
                              {  // Draw Order Lines
                                 ObjectCreate("BuyOrderLine-"+IntegerToString(OrdersInThisTrade)+"-"+IntegerToString(magicnumber), OBJ_HLINE, 0, Time[0], OrderOpenPrice());
                                 ObjectSet("BuyOrderLine-"+IntegerToString(OrdersInThisTrade)+"-"+IntegerToString(magicnumber), OBJPROP_COLOR, clrGreen);
                                 ObjectSet("BuyOrderLine-"+IntegerToString(OrdersInThisTrade)+"-"+IntegerToString(magicnumber), OBJPROP_WIDTH, 2);
                                 ObjectSet("BuyOrderLine-"+IntegerToString(OrdersInThisTrade)+"-"+IntegerToString(magicnumber), OBJPROP_STYLE, STYLE_SOLID);
                                 
                                 // Adjust Arrow Drawing Location
                                 arrowPrice = OrderOpenPrice() - (10 * Point); // Adjust arrow position as needed
                                 // Draw arrows
                                 ObjectCreate(0, "BuyOrderArrow-"+IntegerToString(OrdersInThisTrade)+IntegerToString(magicnumber), OBJ_ARROW, 0, Time[0], arrowPrice);
                                 ObjectSetInteger(0, "BuyOrderArrow-"+IntegerToString(OrdersInThisTrade)+IntegerToString(magicnumber), OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
                                 ObjectSetInteger(0, "BuyOrderArrow-"+IntegerToString(OrdersInThisTrade)+IntegerToString(magicnumber), OBJPROP_COLOR, clrGreen);
                              }
                           if (OrderType() == OP_SELL)
                              {
                                 ObjectCreate("SellOrderLine-"+IntegerToString(OrdersInThisTrade)+"-"+IntegerToString(magicnumber), OBJ_HLINE, 0, Time[0], OrderOpenPrice());
                                ObjectSet("SellOrderLine-"+IntegerToString(OrdersInThisTrade)+"-"+IntegerToString(magicnumber), OBJPROP_COLOR, clrRed);
                                ObjectSet("SellOrderLine-"+IntegerToString(OrdersInThisTrade)+"-"+IntegerToString(magicnumber), OBJPROP_WIDTH, 2);
                                ObjectSet("SellOrderLine-"+IntegerToString(OrdersInThisTrade)+"-"+IntegerToString(magicnumber), OBJPROP_STYLE, STYLE_SOLID);
                               
                                 // Adjust Arrow Drawing Location
                                 arrowPrice = OrderOpenPrice() + (10 * Point); // Adjust arrow position as needed
    
                                 // Draw arrows
                                    ObjectCreate(0, "SellOrderArrow-"+IntegerToString(OrdersInThisTrade)+IntegerToString(magicnumber), OBJ_ARROW, 0, Time[0], arrowPrice);
                                    ObjectSetInteger(0, "SellOrderArrow-"+IntegerToString(OrdersInThisTrade)+IntegerToString(magicnumber), OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
                                    ObjectSetInteger(0, "SellOrderArrow-"+IntegerToString(OrdersInThisTrade)+IntegerToString(magicnumber), OBJPROP_COLOR, clrRed);

                              }
                        }
                    }
      OrdersInThisTrade++;
          }
      }
}

Hey Guys, I can't seem to be able to find out when or how exactly my pending orders turn into actual orders.

Lets say I have 2 pending orders on the chart that are both reflected with a green Zigzag line on the graph. And lets say that one of them expires, and the other one turns into an order.

I want to be able to modify the one that turns into an order and is reflected by the green doted line on the chart that is drawn by default (to blue, for buy orders, and red for sell orders) and place a solid arrow under the open price (Red for sell, and blue for buy).

Later I take another 2 pending orders, and this time, both turn into orders within the same trade, and I want to be able to also modify them to also have them reflect the changes on the graph. So in total, I should have now have 3 lines drawn on the chart with the modified changes.

Finally, when all open positions are closed, I want all the lines to get deleted from the chart, and just reflect in the trade history


Would someone be able to help me out with this... I've been banging my head over it for some time now, but still struggling 😣

Much appreciate your time and help with this guys  😊🙏

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
Trade Orders in DOM - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  • Usually people who cannot code do not receive free help on this forum, although it could happen.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free).
  • Finally, you also have the option to hire a programmer in the Freelance section.

By the way, is this a MQL5 or a MQL4 question?

Your wording suggests that it may be a MQL4 question, as you refer to orders instead of positions.

Should it be about MQL4, please note that MQL4 and MetaTrader 4, has it's own section on the forum.

 
Fernando Carreiro #:
  • Usually people who cannot code do not receive free help on this forum, although it could happen.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free).
  • Finally, you also have the option to hire a programmer in the Freelance section.

By the way, is this a MQL5 or a MQL4 question?

Your wording suggests that it may be a MQL4 question, as you refer to orders instead of positions.

Should it be about MQL4, please note that MQL4 and MetaTrader 4, has it's own section on the forum.


Dear Fernando, first of all, thank you for your response. I think you understood me wrong! I am not asking for someone to "free help", or someone go code an entire EA for me! I am asking for a small part that I mentioned didn't work out with me, while coding my EA.

Here is what I did so far, but it was not working as expected. Yes, this is in MQL4.

Here I am trying to draw an arrow and a line whenever a pending order converts to an actual order. I call the function from onTick. It draws it only when the first pending order converts to an order, but not for the rest, and it goes into an infinite loop...

Can someone from the community please tell me what is wrong, or if they suggest another approach to this... 

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

...

Now what im thinking of doing is to capture the ticket numbers of all pending orders as they are created in an array, and running them into a loop and checking when this ticket number == OP_BUY or OP_SELL and then drawing the line and the arrow, but again here, I don't want to keep drawing the same for the same order when it comes back around the loop. I was thinking of keeping another tracker array where I capture the ticket numbers of the orders that now have lines and checking them before entering the loop. But that would be complex. 

Anyone with a better idea or approach 

Much appreciated


 
adamsaad #: Here is what I did so far, but it was not working as expected. Yes, this is in MQL4.
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. You were asked to edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)  Why didn't you? Why did you make a second post? Why didn't you use the code button?
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  3.   if(OrdersInThisTrade == OrdersTotal())    //  OrdersInThisTrade is a global integer initialized to 0;

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy.
         Trade current timeframe, one strategy, and filter by symbol requires one MN.
         If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

  4. adamsaad: Would someone be able to help me out with this... 

    Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

 
adamsaad #:

Dear Fernando, first of all, thank you for your response. I think you understood me wrong! I am not asking for someone to "free help", or someone go code an entire EA for me! I am asking for a small part that I mentioned didn't work out with me, while coding my EA.

Here is what I did so far, but it was not working as expected. Yes, this is in MQL4.

Here I am trying to draw an arrow and a line whenever a pending order converts to an actual order. I call the function from onTick. It draws it only when the first pending order converts to an order, but not for the rest, and it goes into an infinite loop...

Can someone from the community please tell me what is wrong, or if they suggest another approach to this... 

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Hover your mouse over your post and select "edit" ... 

...

Now what im thinking of doing is to capture the ticket numbers of all pending orders as they are created in an array, and running them into a loop and checking when this ticket number == OP_BUY or OP_SELL and then drawing the line and the arrow, but again here, I don't want to keep drawing the same for the same order when it comes back around the loop. I was thinking of keeping another tracker array where I capture the ticket numbers of the orders that now have lines and checking them before entering the loop. But that would be complex. 

Anyone with a better idea or approach 

Much appreciated


Where is your loop? I cannot see it…
As I understood you are placing pendings in pairs… so I would suggest to use a structure … and an array of this structure, to hold the tickets and some other data that you want to keep for each order… than you can check if one pending activated , delete the other one, and draw whatever you want to draw on the chart , and then delete that index from the array, this way you only draw once 

 
Daniel Cioca #:
Where is your loop? I cannot see it…
As I understood you are placing pendings in pairs… so I would suggest to use a structure … and an array of this structure, to hold the tickets and some other data that you want to keep for each order… than you can check if one pending activated , delete the other one, and draw whatever you want to draw on the chart , and then delete that index from the array, this way you only draw once 


Thank you Daniel for your reply. Yes, I will store all the tickets of the pending orders in an array and then check if they changed to an actual order, draw the line when they do, and then clear the ticket data so it doesn't draw it again. Thank you for your help

 
adamsaad #:

Thank you Daniel for your reply. Yes, I will store all the tickets of the pending orders in an array and then check if they changed to an actual order, draw the line when they do, and then clear the ticket data so it doesn't draw it again. Thank you for your help

Array of a structure… if you need to store the tickets in pairs 
 
Daniel Cioca #:
Array of a structure… if you need to store the tickets in pairs 

Thanks Daniel, I did it and it is working find