How to select the previous order of the same symbol/type and check profit before opening a second order

 
Hi guys, I am having a persistent problem, I am trying to select the previous order of the same symbol and type (buy/sell), check if the profit has gone up by certain value (pyramid) before opening a second order. The code below shows what I did, but its not working. UsePoint is either 0.01 or 0.0001.
//BUY SIGNAL
     if(AFastMA > ASlowMA && BFastMA <= BSlowMA && BuyMarketCount(Symbol(),MagicNumber) == 0)
        {
        //Close All Sell Orders
        if(SellMarketCount(Symbol(),MagicNumber) > 0)
           {
           CloseAllSellOrders(Symbol(),MagicNumber,Slippage);
           }
           
        //Open 1st Buy Order
        if(equity > umargin)
           {
           ABuyTicket = OpenBuyOrder(Symbol(),LotSize,UseSlippage,MagicNumber);
           }

        //1st Buy Order Modification
        if(ABuyTicket > 0 && (StopLoss > 0 || TakeProfit > 0))
           {
           OrderSelect(ABuyTicket,SELECT_BY_TICKET);
           double OpenPrice = OrderOpenPrice();

           //Calculate and Verify Stop Loss (and Take Profit)
           double BuyStopLoss = CalcBuyStopLoss(Symbol(),StopLoss,OpenPrice);
           if(BuyStopLoss > 0)
               BuyStopLoss = AdjustBelowStopLevel(Symbol(),BuyStopLoss,5);

           double BuyTakeProfit = CalcBuyTakeProfit(Symbol(),TakeProfit,OpenPrice);
           if(BuyTakeProfit > 0)
               BuyTakeProfit = AdjustAboveStopLevel(Symbol(),BuyTakeProfit,5);

           //Add Stop Loss (and Take Profit)
           AddStopProfit(ABuyTicket,BuyStopLoss,BuyTakeProfit);
           }
        }
//+--------------------------------------------------------------------------------------+         
      if(ABuyTicket > 0)        //This is where I have problems
         {
         OrderSelect(ABuyTicket,SELECT_BY_TICKET);      //And here
         OpenPrice = OrderOpenPrice();                  //And here
         }
         
         //Open 2nd Buy Order
         if(((OpenPrice + (Pyramid * UsePoint)) < Close[1]) && (AFastMA > ASlowMA) && (AQuickMA > BQuickMA)
         && equity > umargin && (BuyMarketCount(Symbol(),MagicNumber) < 2))                                     //And here
            {
            BBuyTicket = OpenBuyOrder(Symbol(),LotSize,UseSlippage,MagicNumber);

            //2nd Buy Order Modification
            if(BBuyTicket > 0 && (StopLoss > 0 || TakeProfit > 0))
               {
               OrderSelect(BBuyTicket,SELECT_BY_TICKET);
               OpenPrice = OrderOpenPrice();

               //Calculate and Verify Stop Loss (and Take Profit)
               BuyStopLoss = CalcBuyStopLoss(Symbol(),StopLoss,OpenPrice);
               if(BuyStopLoss > 0)
                  BuyStopLoss = AdjustBelowStopLevel(Symbol(),BuyStopLoss,5);

               BuyTakeProfit = CalcBuyTakeProfit(Symbol(),TakeProfit,OpenPrice);
               if(BuyTakeProfit > 0)
                  BuyTakeProfit = AdjustAboveStopLevel(Symbol(),BuyTakeProfit,5);

               //Add Stop Loss (and Take Profit)
               AddStopProfit(BBuyTicket,BuyStopLoss,BuyTakeProfit);
               }
            }
 
monaphilips:
Hi guys, I am having a persistent problem, I am trying to select the previous order of the same symbol and type (buy/sell), check if the profit has gone up by certain value (pyramid) before opening a second order. The code below shows what I did, but its not working. UsePoint is either 0.01 or 0.0001.

Count the orders

 check the time, the youngest is yours

 
monaphilips I am trying to select the previous order of the same symbol and type

No such select loop posted.

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

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

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

 
William Roeder:

No such select loop posted.

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

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

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

I posted the code above as shown.

I don't think a select loop is required as the EA is used across several securities with the same magic number.

My problem is I want to select the previous order (ABuyticket) of the same symbol (possibly after several hours or days after opening it), check to see if price has moved in my favour by (Pyramid * UsePoint), if it has, then I open a second order (BBuyticket) of the same type.

My problem is how to select the previous order (ABuyticket)

Pls note: ABuyticket & BBuyticket are global variables

Reason: