OrderSelect() question

 

Hi all, I can use "OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);" function but I cannot select orders in the past using "OrderSelect(1,SELECT_BY_POS,MODE_HISTORY);". Any thoughts?

Thanks much :)

 
deVries:

there is no ticket with number 1 on your account.....
He's using SELECT_BY_POS. The selection starts at 0 so make sure that you've got more than 1 order in history.
 
WhooDoo22:
Hi all, I can use "OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);" function
but I cannot select orders in the past using "OrderSelect(1,SELECT_BY_POS,MODE_HISTORY);".
  1. MODE is ignored when you select by ticket. Just use "OrderSelect(ticket,SELECT_BY_TICKET)." You must look at OrderCloseTime() if you need to know wither the order has closed.
  2. Do you have more than 1 trade in history?
  3. Are you filtering by MN and pair so your EA is compatible with every other including itself and manual trading?
  4. You must count down. Loops and Closing or Deleting Orders - MQL4 forum
    bool    MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES){
        if (!OrderSelect(iWhat, eSelect, ePool)     return (false);
        if (OrderMagicNumber() <  magic.number)     return (false);
        if (OrderMagicNumber() >  magic.number.max) return (false);
        if (OrderSymbol()      != chart.symbol)     return (false);
        if (ePool != MODE_HISTORY)                  return (true);
        return(OrderType() <= OP_SELL); //Avoid cr/bal forum.mql4.com/32363#325360
                                        //Never select canceled orders.
    }
    :
        for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--)
        if (MySelect(iPos, SELECT_BY_POS)){
            double size = OrderLots(); ...
    :
        for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--)
        if (MySelect(iPos, SELECT_BY_POS, MODE_HISTORY)){
            int nextTkt = OrderTicket();
    
  5. Are you trying to select the last closed order? Order History sort by closing date - MQL4 forum
 

Thanks for post DeVries!

Gotch'ya, I ll be sure to add a "comment" that shows "OrdersHistoryTotal() to see how many orders I have total :) - thanks heelflip43!

WHRoeder,

1. MODE is ignored when you select by ticket. Just use "OrderSelect(ticket,SELECT_BY_TICKET)." You must look at OrderCloseTime() if you need to know wither the order has closed.

Question: I understand that if "OrderCloseTime() == 0" then the order is "Open" and if "OrderCloseTime() > 0" then the order is "Closed". My question is "How do I specify an order?.. Example: Lets say I want to select order 10 in total orders history. How can I "select the order 10"? (I found this... Maybe there is a correlation? https://docs.mql4.com/trading/OrderCloseTime

2. Do you have more than 1 trade in history?

Answer: "Yes".

3. Are you filtering by MN and pair so your EA is compatible with every other including itself and manual trading?

Answer: I don't understand the meaning of abbreviation "MN". (Please excuse my ignorance) :) I am guessing "pair" means GBPUSD or any other currency. I don't understand what "compatible with every other including itself and manual trading?" means. I will explain my position here: Priority 1 = Write strategy into code that functions in strategy tester. Priority 2 = "Code Cleanup" or Simplify all code.

Right now, I am in Priority 1 phase. When I have completely coded strategy I will begin Priority 2 and funnel all my power into cleaning up the code. I will most likely do as you instruct in step 3. :)

4. You must count down. https://www.mql5.com/en/forum/139654

Answer: I will plow a few hours into this to recognize its topics concepts. (I think I saw a post by RaptorUK, so I will enjoy what he and the other posters wrote)

5. Are you trying to select the last closed order?

Answer: I am. ( I am, but I wish to use a function that would select the last order OR any previous order, like if I wished to select order # 8 from list of all orders place )

Thanks much for your support WHRoeder n' thanks much to everyone for your posts!

 

eureka! eureka! ;) I admit... I am slow sometimes :) If any coder kicked his/her foot into a wall because they can't figure out how to select previous orders hopefully this will help him/her continue their journey. (the below code is an example of selecting the last order... So if you just opened a new order, this code will print the open AND close time of your last order. :) whew! ahh, time to catch up on some sleep. Goodnight! n' Thanks to all for support!

if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)==true)
{
datetime ctm=OrderOpenTime();
if(ctm>0) Print("Open time for the order 1 ", ctm);
ctm=OrderCloseTime();
if(ctm>0) Print("Close time for the order 1 ", ctm);
}
else
Print("OrderSelect failed error code is",GetLastError());
 
WhooDoo22:
Answer: I don't understand the meaning of abbreviation "MN". (Please excuse my ignorance) :) I am guessing "pair" means GBPUSD or any other currency. I don't understand what "compatible with every other including itself and manual trading?" means.
Did you bother to even look at the code posted?
    if (OrderMagicNumber() >  magic.number.max) return (false);
    if (OrderSymbol()      != chart.symbol)     return (false);
Why would you want your EA to look at trades it DID NOT MAKE? If it's trading the GBPUSD, why would you want to look at a EURJPY trade? If another EA or the human did a trade, the magic number won't match, Why would you want your EA to look at that trade?
 

Why would you want your EA to look at trades it DID NOT MAKE?

Answer: I do not want the EA to look at trades it did not make.

If it's trading the GBPUSD, why would you want to look at a EURJPY trade?

Answer: The EA that I am developing does not trade multiple currency pairs. (if I placed manual trades, the EA would look at those trades too, but I will fix this after EA strategy is completed)

If another EA or the human did a trade, the magic number won't match, Why would you want your EA to look at that trade?

Answer: You are right, if another EA or human placed a trade the magic number won't match. (This is to be corrected after EA strategy is completed)

Also, I do not want EA to look at outside orders placed by other EAs or human. :)

I did look at the code posted. Thank you for posting it, but I have my own ways of keeping up with orders. There is more than one way to keep track of orders besides using magic numbers.

Thanks very much WHRoeder!

 

Here is the solution for selecting orders.

// code for selecting last order's profit;

   OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
   if(OrderCloseTime()!=0){
     {
      Comment("OrderProfit() for last order = ",OrderProfit());
     }

// code for selecting a specific order's profit; (in this case, order 2);

   OrderSelect(2,SELECT_BY_POS,MODE_HISTORY);
     {
      Comment("OrderProfit() for the 2nd order = ",OrderProfit());
     }

// (it might be the 3rd order because the first order might be 0. Just
//    test it, and maybe post which order it selects if you like.);




    

Reason: