Sell (Exit/SL) criteria is not working

Dear All,

I have been struggling to figure out the issue as why one of the sell criteria in my code isnt functioning (although simple criteria). I am posting the code as well as the related snapshot. The idea is to "Buy" when price bounces from 52 week low (entry above high of Signal bar) and exit when 52 week high comes (which is working fine) or when price closes below the Low of "bottom" signal bar ( SL - which isnt working).

Code is as follows:

DonchianUpper =HHV(Ref(H,-1),pds);
DonchianLower = LLV(Ref(L,-1),pds);
DonchianMiddle = (DonchianUpper+DonchianLower)/2;
Lastweekvolume= HHV(Ref(V,-1),7);


Bottom = Low <= DonchianLower ;
Bottomcandlelow= ValueWhen(Bottom,Low);
Bottomcandlehigh= ValueWhen(Bottom,High);

BarLimit = 20;

Buy= Close > Bottomcandlehigh AND BarsSince(Bottom) <= BarLimit;
Sell = Close <= Bottomcandlelow OR Close >= DonchianUpper;

Sell= ExRem(Sell,Buy);

PlotShapes(IIf(Sell, shapeDownArrowSell, shapeNone),colorBlack, 0,H, Offset=-55);
PlotShapes(IIf(Buy, shapeUpArrow
Buy, shapeNone),colorGreen, 0,L, Offset=-45);

Filter =1;
SetSortColumns(2) ;
SetOption("NoDefaultColumns", True);
AddColumn( DateTime(), "Date", formatDateTime );



As a long time member, you should have seen the notice to be a Verified member.

Please, search and follow the steps to get Verified (Search Verified or Verified badge).

Additionally, please use the Code Blocks ("</>") when posting code. That let's the other forum members who are interested in helping you out, to quickly and easily grab your code and test it out.


My account is verified long back, if i am not wrong. Please see, and guide if something else needed for the same from my end.


Does the "User name at forum" match your User name here? It looks too short in the picture.

1 Like

License verified. Thanks for pointing out dear, since i am have multiple licenses...the username mismatch was there. Now rectified!!


I think you need to learn the Ref() command. I suspect that your Buy and Sell tests are comparing to the "current bar".

So try:

 Sell = Close <= Ref(Bottomcandlelow,-1)  OR Close >= Ref(DonchianUpper, -1);

Let us know if that helps.


Thank you so much @snoopy.pa30. Sometimes smallest of things we miss and it makes the complete difference. I am kind of embarrassed i missed it. But always a learning and hence this forum comes to rescue.

Thanks you again