You could do this with a loop. I've added a date so that you get limited iterations in case of a large history. And sometimes the closing price slips so that it's near but not beyond the SL.
datetime time=TimeCurrent(); datetime today=time-(time%(24*3600)); double slippage=3*_Point; int stops=0; for(int pos=OrdersHistoryTotal()-1; pos>=0; pos--) { if(! OrderSelect(pos,SELECT_BY_POS,MODE_HISTORY)) continue; if(OrderCloseTime()<today) break; if(OrderSymbol()==_Symbol && OrderMagic()==Magic && OrderStopLoss()>0) { if(OrderType()==OP_BUY && OrderClosePrice()<=OrderStopLoss()+slippage) stops++; if(OrderType()==OP_SELL && OrderClosePrice()>=OrderStopLoss()-slippage) stops++; } }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Im trying to count the number of stop losses that were triggered during a back test.. Right now, I've been working on getting a condition that will trigger every time a stop loss is hit. The problem i'm having is if a stop loss is hit and then a new trade is immediately opened, the condition is triggered twice. I'm pretty sure there is a better way to code this using a for loop, but I cant get it to work. This is the closest i have gotten.