Get first ticket values in the loop

 

Have this loop to get some values for code operation:

   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&
         OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber){
         if(OrderType()==OP_BUY){
            BuyLots+=NormalizeDouble(OrderLots(),2);
            BuyProfit+=NormalizeDouble(OrderProfit(),2);
            OpenPriceLong=NormalizeDouble(OrderOpenPrice(),Digits);
            OpenPriceLongSum+=NormalizeDouble(OrderOpenPrice(),Digits);
            BreakevenFactorLong+=NormalizeDouble(OrderLots()*OpenPriceLong,Digits+2);
            FirstBuyOrder=OrderTicket();
            FirstBuyProfit=OrderProfit();
            }
         if(OrderType()==OP_SELL){
            SellLots+=NormalizeDouble(OrderLots(),2);
            SellProfit+=NormalizeDouble(OrderProfit(),2);
            OpenPriceShort=NormalizeDouble(OrderOpenPrice(),Digits);
            OpenPriceShortSum+=NormalizeDouble(OrderOpenPrice(),Digits);
            BreakevenFactorShort+=NormalizeDouble(OrderLots()*OpenPriceShort,Digits+2);
            FirstSellOrder=OrderTicket();
            FirstSellProfit=OrderProfit();
            }
         }

As far as I could see, FirstTicket and FirstProfit values are not corrresponding to the first order in every case, so I've heard that it's needed a datetime condition to get FirstTicket and its profit value. So anybody can enlighten us?

 
   datetime buyOrderTime=TimeCurrent();

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && 
         OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_BUY)
           {
            BuyLots+=NormalizeDouble(OrderLots(),2);
            BuyProfit+=NormalizeDouble(OrderProfit(),2);
            OpenPriceLong=NormalizeDouble(OrderOpenPrice(),Digits);
            OpenPriceLongSum+=NormalizeDouble(OrderOpenPrice(),Digits);
            BreakevenFactorLong+=NormalizeDouble(OrderLots()*OpenPriceLong,Digits+2);
            if(OrderOpenTime()<buyOrderTime)
              {
               buyOrderTime=OrderOpenTime();
               FirstBuyOrder=OrderTicket();
               FirstBuyProfit=OrderProfit();
              }
           }

Not compiled or tested.

 
Keith Watford:

Not compiled or tested.

So OrderOpenTime could be any before TimeCurrent, think's not a solution.
 
David Diez:
So OrderOpenTime could be any before TimeCurrent, think's not a solution.

You're not seeing it.

Obviously, the first run of the loop, the variable will store the open time of that order.

Subsequently only those with an earlier open time will update the variable.

So by the time the loop completes, it must be the earliest.

 
  1.             if(OrderOpenTime()<buyOrderTime){
                   buyOrderTime=OrderOpenTime();
                   FirstBuyOrder=OrderTicket();
                   FirstBuyProfit=OrderProfit();
                }
    Keith is correct. Each time it finds an earlier order, it updates the variables. The variable buyOrderTime should probably be renamed earliestBuyTime.

  2.             BreakevenFactorLong+=NormalizeDouble(OrderLots()*OpenPriceLong,Digits+2);
    
    For the break even price you will need to add
    int BuyCount=0;
       for(…){
          if(…){
             if(OrderType()==OP_BUY){
                ++BuyCount;
                ⋮
       }
       BreakEvenPriceLong = BreakevenFactorLong / MathMax(1,BuyCount);

  3. No need for the TimeCurrent() call where a constant will do.
    #define  MAX_DATETIME   D'3000.12.31 23:59:59'
    datetime buyOrderTime=MAX_DATETIME;
    
  4. And I assume the variables being summed are zero before the loop, (BuyLots, BuyProfit, OpenPriceLongSum, BreakevenFactorLong.)

 
Keith Watford:

You're not seeing it.

Obviously, the first run of the loop, the variable will store the open time of that order.

Subsequently only those with an earlier open time will update the variable.

So by the time the loop completes, it must be the earliest.

What if i try to get profit values from index POS?

I'm working in this code for two weeks and still got no solution.

Have an internal loop to get some values for expert operation, where one of them is FirstBuyProfit. It's suposed to be the first buy order current profit.

   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&
         OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber){
         if(OrderType()==OP_BUY){
            BuyLots+=NormalizeDouble(OrderLots(),2);
            BuyProfit+=NormalizeDouble(OrderProfit(),2);
            FirstBuyProfit=NormalizeDouble(OrderProfit(),2);
            OpenPriceLong=NormalizeDouble(OrderOpenPrice(),Digits);
            OpenPriceLongSum+=NormalizeDouble(OrderOpenPrice(),Digits);
            BreakevenFactorLong+=NormalizeDouble(OrderLots()*OpenPriceLong,Digits+2);
            }

Then I have into the order management loop, which is backcounter (i--), the averaging cycle where I compare FirstBuyProfit to LastBuyProfit in order to close them by overlapping:

               if(Bid<=AveragingLong){ // DISTANCE CONDITION (TRUE)
                  Print("Averaging LONG under ",AveragingLong);
                  Comment("Averaging LONG under ",AveragingLong);
                  double LastBuyProfit=OrderProfit();
                  if(LastBuyProfit+FirstBuyProfit>=0){ // CONDITION NEVER MET
                     Print("Close LONG by averaging");
                     Comment("Close LONG by averaging");
                     if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)){
                        if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNONE)){
                           Print("OrderClose error ",GetLastError());
                           RefreshRates();
                           return;
                           }
                        }
                     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
                        if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrSilver)){
                           Print("OrderClose error ",GetLastError());
                           RefreshRates();
                           return;
                           }
                        }
                     
So does anyone have some idea?
 
David Diez:
So does anyone have some idea?

You've been given the answer to your first question

 if(OrderOpenTime()<buyOrderTime){
               buyOrderTime=OrderOpenTime();
               FirstBuyOrder=OrderTicket();
               FirstBuyProfit=OrderProfit();
            }

yet you have chosen to ignore it.

 
Keith Watford:

You've been given the answer to your first question

yet you have chosen to ignore it.

I'm trying to make it in a simpler way without tickets, if it's possible.
 

You've been given it.

You're being argumentative just like you previously did at
          Last Open Price issue - Symbols - MQL4 programming forum #2 #4

 
William Roeder:

You've been given it.

You're being argumentative just like you previously did at
          Last Open Price issue - Symbols - MQL4 programming forum #2 #4

That datetime condition changed nothing.
Reason: