StopLoss of Zero


When I apply the below code for back-testing, i am getting lot of "0% change" stop losses with #Bars as 2.

ma50 = ema(C, 50);
ma200 = ema(c, 200);

buycon1 = cross(ma50, ma200);

buy = buycon1;
buyprice = ref(o,1);

sellcon1 = cross(ma200, ma50);

sell = sellcon1;
sellprice = ref(o,1);

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

ret = (c - BuyPrice)/buyprice;

slcon1 = (ret <= (-0.02));
slcon2 = c < ma50;
slcon = slcon1 and slcon2;
slval = buyprice - valuewhen(slcon, ref(o,1));

applystop(stopTypeLoss, stopModePoint, slval,1,False, 0, 0, -1, 0);

Can someone please help me understand, where i am making the mistake?


I don't think you should be looking in the future.

Why don't you reverse it instead and check Buy condition on previous bar and use current bar Open price.

Buy = Ref( buycon1, -1);
BuyPrice = O;

and do the same thing for other variables.

1 Like

Got it. Thank you very much.