Trying to get first order value from the loop

 

Have this problem in my code:

for(int i=0;i<OrdersTotal();i++){
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
                if(OrderType()==(...)){
                        OpenPrice=OrderOpenPrice(); // Correct value, it's first open order
                        FirstOrder=OrderTicket();   // Wrong value, gives last order.
                        FirstProfit=OrderProfit();  // Wrong value, gives last order profit.
                        if(OrderTicket()==OrderTicket()){ // Other way
                                FirstOrder=OrderTicket();   // Wrong value, gives last order.
                                FirstProfit=OrderProfit();  // Wrong value, gives last order profit.

Anyone can lend a hand?

 
David Diez:

Have this problem in my code:

Anyone can lend a hand?

You can check out these threads:

https://www.mql5.com/en/forum/153300

https://www.mql5.com/en/forum/127468

Finding the First Order Open Price from several Open orders
Finding the First Order Open Price from several Open orders
  • 2014.10.03
  • www.mql5.com
I am trying to find the first open price from several open orders and symbols. Below is my initial attempt. Need help getting this to work...
 
David Diez:

Have this problem in my code:

Anyone can lend a hand?

What do you mean by first order, the earliest?

I don't see you comparing OrderOpenTime() at all.

if(OrderTicket()==OrderTicket())

When will this ever be false? So what's its purpose.

 
Keith Watford:

What do you mean by first order, the earliest?

I don't see you comparing OrderOpenTime() at all.

When will this ever be false? So what's its purpose.

Yes, I mean the first open order.

That OrderTicket()==OrderTicket() works for me to close first open order at reversal signal in another advisors.

But it's suposed to be always taken the first order value in a forward counter loop.

 
David Diez:

Yes, I mean the first open order.

That OrderTicket()==OrderTicket() works for me to close first open order at reversal signal in another advisors.

It won't work for anything as it will always be true!

 
David Diez:

https://www.mql5.com/en/forum/314649

Why are you posting the same question when you have already been answered????

Get first ticket values in the loop
Get first ticket values in the loop
  • 2019.05.30
  • www.mql5.com
Have this loop to get some values for code operation: As far as I could see, FirstTicket and FirstProfit values are not corrresponding to the first...
 
Keith Watford:

What do you mean by first order, the earliest?

I don't see you comparing OrderOpenTime() at all.

When will this ever be false? So what's its purpose.

Ok, if I do this:

         if(OrderOpenTime()<OrderOpenTime()){
            OpenTime=OrderOpenTime();
            FirstOrder=OrderTicket();
            FirstProfit=OrderProfit();
            }

...or this:

         if(OrderTicket()<OrderTicket()){
            FirstOrder=OrderTicket();
            FirstProfit=OrderProfit();
            }

I get as first unexistent order, this is Ticket 0 with Profit 0.

 
David Diez:

Ok, if I do this:

...or this:

I get as first unexistent order, this is Ticket 0 with Profit 0.

Show the complete loops and where variables are assign their starting values.

if(OrderOpenTime()<OrderOpenTime())
//
if(OrderTicket()<OrderTicket())

Now do you think will these conditions will ever be true??

 
Keith Watford:

Show the complete loops and where variables are assign their starting values.

Now do you think will these conditions will ever be true??

Keith Watford you're a hustler.

   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&
      OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber){
         if(OpenTime==0||OpenTime<OrderOpenTime()){
            Ticket=OrderTicket();
            Profit=OrderProfit();
            }
         if(OrderType()==OP_BUY){
            BuyOrders++;
            BuyProfit+=OrderProfit();
            BuyOpenPrice=OrderOpenPrice();
            if(OrderOpenPrice()==BuyOpenPrice){
               FirstBuyOrder=OrderTicket();
               FirstBuyProfit=OrderProfit();
               }
            }
         if(OrderType()==OP_SELL){
            SellOrders++;
            SellProfit+=OrderProfit();
            SellOpenPrice=OrderOpenPrice();
            if(OrderOpenPrice()==SellOpenPrice){
               FirstSellOrder=OrderTicket();
               FirstSellProfit=OrderProfit();
               }
            }
         }
      }
 
            BuyOpenPrice=OrderOpenPrice();
            if(OrderOpenPrice()==BuyOpenPrice)
            SellOpenPrice=OrderOpenPrice();
            if(OrderOpenPrice()==SellOpenPrice){

Can you honestly not see anything wrong here??

               FirstBuyOrder=OrderTicket();
               FirstBuyProfit=OrderProfit();

These will just be assigned the value of the last buy order checked in the loop. Same goes for sells.


Why am I a hustler?

 
Keith Watford:

Can you honestly not see anything wrong here??

These will just be assigned the value of the last buy order checked in the loop. Same goes for sells.


Why am I a hustler?

First question, if OpenPrice gives the pos 0 this is first open price, why does not the same with ticket or/and profit? Why does it continue iteration looking for higher values?

Second question, why if I compare OpenPrice, OpenTime or OrderTicket by < to find the earlier within the iteration it leads me to an unexistent ticket(0)?

Third question if OP is first OP, don't actually know why this if returning the right value whenever it doesn't to Ticket an Profit, but in that case, if I'm getiting the right OP value and then if this value is correct condition may be give its Ticket and Profit.