stop loss detection inconsistency issues - can't seem to find the bug!

 

Hi guys, I'm having some issues with a little function I've produced. I collect all the orders...

int totalOrderCount;
int icnt;
totalOrderCount = OrdersTotal();
for (icnt=0;icnt<totalOrderCount;icnt++) {
tbx_CheckForRiskyStopLoss(icnt);
}

Which calls tbx_CheckForRiskyStopLoss passing the current ticket...

bool tbx_CheckForRiskyStopLoss(int ticket) {
if(OrderSelect(ticket, SELECT_BY_POS)==true) {
if (((OrderOpenPrice() - OrderStopLoss())*100) == riskyStopLossInPips) {
Print("Ticket: ", ticket, " has stop loss: ", ((OrderOpenPrice() - OrderStopLoss())*100), " pips -- RISKY!");
return(true);
} else {
Print("Ticket: ", ticket, " has stop loss: ", ((OrderOpenPrice() - OrderStopLoss())*100), " pips -- SAFE!");
tbx_PrintDbg(ticket);
return(false);
}
} else {
Print("OrderSelect failed error code is",GetLastError());
}
}

I've opened some orders *all* with 500 pip stop loss as a test, with riskyStopLossInPips = 500, and most of the time the result is correct, however sometimes it returns false.

Here's some output with some debug data:

2011.02.08 16:20:22 2011.02.04 22:59 XAGEA XAGUSD,H1: Ticket: 5 DBG. OPEN:29.12 SL:24.12 BID:29.07 ASK:29.11
2011.02.08 16:20:22 2011.02.04 22:59 XAGEA XAGUSD,H1: Ticket: 5 has stop loss: 500 pips -- SAFE!
2011.02.08 16:20:22 2011.02.04 22:59 XAGEA XAGUSD,H1: Ticket: 4 has stop loss: 500 pips -- RISKY!
2011.02.08 16:20:22 2011.02.04 22:59 XAGEA XAGUSD,H1: Ticket: 3 has stop loss: 500 pips -- RISKY!
2011.02.08 16:20:22 2011.02.04 22:59 XAGEA XAGUSD,H1: Ticket: 2 has stop loss: 500 pips -- RISKY!
2011.02.08 16:20:22 2011.02.04 22:59 XAGEA XAGUSD,H1: Ticket: 1 has stop loss: 500 pips -- RISKY!
2011.02.08 16:20:22 2011.02.04 22:59 XAGEA XAGUSD,H1: Ticket: 0 has stop loss: 500 pips -- RISKY!


All tickets have an S/L of 500, completely static, so I do not know why ticket 5 returns that it is "safe", yet looking at the debug line above it (which is called when the routine hits the "safe" part of the conditional statement, shows that the open is 29.12 and S/L is 24.12. Now im no mathematician but (29.19-24.12)*100cents = 500cents, which is equal to riskyStopLossInPips (XAG just happens to have 1 cent per pip).

I am not sure why this works only some of the time, and why tickets such as ticket 5 are being reported incorrectly. If anyone has any clues or suggestions i'd most appreciate it!

Regards,

Sera

Reason: