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
}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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: