# How can I set an opposite Pending order on the SL of a Position AND Automatically Cancel it, when the other Position hits a specific targetprice??

467

Question is above, This is my Plan:

1 : ->Signal
2 : opening a Position (remembering its SL Level)
3 : setting an opposite pending order with x2 the initial lot size and 10 pips TP and finally setting SL Below/Above your initial trade SL + 5 pips
4 : if your initial trade hit the TP (or Trailingstop) then cancel the pending order (<-this is where I struggle)

I dont find an accurate way to do step 4.

I tried to get the ticket of the initial Position & the Pending (i hope this is right):

```         double Ask =SymbolInfoDouble(_Symbol, SYMBOL_ASK);
Print ("Long!!");

{

LastPosTicketLong = most_recently_opened_Pos();

if(ShortPending(Price,Price+StopLossShort2,Price-TakeProfitShort2)) //Function for Pending Order

{LastOrderTicketLong = most_recently_opened_Order();}

}

...

ulong most_recently_opened_Pos()
{
long   youngestPositiontime=0;
ulong youngestPosition=-1;
int total =PositionsTotal();
for(int i=0;i<total;i++)
{
ulong ticket=PositionGetTicket(i);
long Positiontime = PositionGetInteger(POSITION_TIME);

if (Positiontime > youngestPositiontime)
{
youngestPositiontime = Positiontime;
youngestPosition = ticket; }
}
if (youngestPosition>0)
{
return(youngestPosition);
}
else {return(0);}
}

ulong most_recently_opened_Order()
{
long   youngestOrdertime=0;
ulong youngestOrder=-1;
int total =OrdersTotal();
for(int i=0;i<total;i++)
{
{
ulong ticket=PositionGetTicket(i);
long Ordertime = OrderGetInteger(ORDER_TIME_SETUP);

if (Ordertime > youngestOrdertime)
{
youngestOrdertime = Ordertime;
youngestOrder = ticket;
}
}
}
if (youngestOrder>0)
{
return(youngestOrder);
}else {return(0);}
}          ```

If this above is right I only need a way, how I can detect if the Initial order has been closed (wether by unchanged SL or by TP/Trailing stop     or    if it made a profit or a loss)

How can i get these Information of the closed Position???

Thank you so much ! :O