how to pass take profit value of one order to another

 

In my EA I open 2 orders at once. 

Order #1 : set TakeProfit value ( TakeProfit value calculated based on ATR of the time of order placed )

Order #2 : do not set TakeProfit value at the opening


Later on I want to run trailing stop for the "Order #2" and I need to pass the TakeProfit value I used in the Order #1 as trailing value ( which means the ATR value of the order creation ).

Means the ATR of the time when both orders placed.

Please suggest your idea to achieve this.


Below is the content of OnTick() function

void OnTick()
  {    
  if (OrdersTotal() == 0)
    {  
      if ( Signal() == "LONG") 
      {
         BuyOrder();
         SecondBuyOrder();     
      }
      else if (Signal() == "SHORT")
      {
         SellOrder();         
         SecondSellOrder();
      }
    }
    else
    {
         ActivateTrailingSTOP();
     }


Order functions are defined in global area, above OnTick().

Below is sample order of BUY, where I calculate ATR to use in OrderSend() as TP.

   void BuyOrder()
   {
      if (OrdersTotal() == 0)
      {
         double StopLoss = StopLoss();
         double ATR = iATR(Symbol(),PERIOD_H1,24,1);        
         double TakeProfit = ATR;
         if(Digits==3)
         {
             SLPips = StopLoss*100;
         }
         if(Digits==5) 
         {
            SLPips = StopLoss*10000;
         } 
         double Lots = CalculateLotSize(SLPips);
         bool Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 10, Bid-StopLoss, Bid+TakeProfit, "BUY order Sent", MagicNumber1);
      }
   }
 
calculate the difference of Order #1 to trail Order #2
 
Samuel Akinbowale:
calculate the difference of Order #1 to trail Order #2
Thank you for the sugession.
Order #1 will quit earlier than order #2 as my intention is to trail order #2 as much as possible if the market is moving in favor.

How to keep the calculated difference to pass into the trailing function on each new tick ?
 
keep the ticket before close and use
 
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==)
       {
   
 
Samuel Akinbowale:
keep the ticket before close and use
 
Thank you for the tip.
Following your comment i found the way to find last closed order... I can use this :)

int i =OrdersHistoryTotal()-1;
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
double closingPrice =OrderClosePrice();
 
imalkaiw: I can use this :)
  1. Do not assume history has only closed orders.
    Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum

  2. Using OrdersTotal (OrdersHistoryTotal) directly and/or no Magic number filtering on your OrderSelect 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
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  3. Why did you post your MT4 question in the Root / 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
    Next time post in the correct place. The moderators will likely move this thread there soon.