Stop Loss of current trade and a stop order for a new trade at the same price, how does the OnTrade function react?

 

Hey guys, I'm currently working on an expert advisor, in which I use the OnTrade() function to trigger chart object creation functions. Basically, I want to manually draw arrows and lines on the chart, somewhat similar to how the native trade entry/exit arrows and sl/tp lines work. In order to do so, I first check in the OnTrade function if there is a relevant open position, if there is, I get the position ticket, if not, the position ticket is 0. So, if I get a non 0 position ticket, I get the position details, such as open price, tp, sl, position open time, etc. and use those values to draw on the chart. On the other side the same, if I get a 0 position ticket, I assume that either the OnTrade event got triggered by opening an order, which I check for, or, the position just got closed, in which case I again want to get the details of the last position and draw on the chart.


Now, the follwing problem: What happens if there is an open position with a stop loss price of x, and a stop or limit order at the same price? Can I be sure that there is a moment where my getPositionTicket function returns 0 in order to detect the closing of the last position? Or is it even possible that the new position gets executed before the last one gets successfully closed and I therefore never get a 0 position ticket and never detect the closing of the position in the first place? And if thats the case, what would be ways to work around that? Heres the relevant snippets of my code:

void OnTrade(){
    ulong positionTicket = getOpenPositionTicket();                 					//returns 0 if theres no relevant open position, returns the position ticket if there is
    if(positionTicket != 0){
        PositionSelectByTicket(positionTicket);
        datetime positionTime = PositionGetInteger(POSITION_TIME);
        double positionPriceOpen = PositionGetDouble(POSITION_PRICE_OPEN);
        //...
        //...
        ObjectCreate(0, "SLLine", OBJ_TREND, 0, endTime, positionPriceSl, closeTime, positionPriceSl);
        //...
    }
    else{
	//check if it was triggered by an order placement or position close
        //get corresponding position/deal from history
        //get position/deal data
        //draw objects on chart
    }
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Execution of trade operations results in the opening of a position, changing of its volume and/or direction, or its disappearance. Trade operations...