Counting the number of Stop losses in a back test

To add comments, please log in or register
J4str
36
J4str  

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.



closebySL=false;

if (NewOrder==True)    //This is set to true when a new order is placed

{

OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY);

if (StringFind(OrderComment(),"Entry[sl]")!=-1) {closebySL=true;}

if(closebySL==true){

Print("SL HIT =",OrdersHistoryTotal()  );

NewOrder=False;

}}


lippmaje
1057
lippmaje  

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++;
     }
  }
To add comments, please log in or register