Need help to understand why SL is not working in few scenarios

Hi,
While working on SL point based exit from a Buy position. The code works in most of the scenarios but fail to work on few. Wanted to check if I am missing anything in the code or the reason of this failure.
Attaching the code snippet below.

	    Point = 90;  
    SL_Pt = BuyPrice - Point;  
   
	MACD_cross = Cross(Ref(Signal(),-1),Ref(MACD(),-1)); 
  
    Sell = ( Short OR Sell_SAR OR Cross( TimeNum(), End_Of_Day_time ) OR Cross( SL_Pt, L ) OR MACD_cross)  
           AND Day() == Buyday AND TimeNum() > Buy_Time;  
  
    SellPrice = ValueWhen( Sell, IIf( Sell_SAR, Sell_SAR_Price, IIf( Short, ShortPrice, IIf( Cross( SL_Pt, L ), SL_Pt, IIf(MACD_cross, O, C ) ) ) ));  

Filter = 1;  
AddColumn( SL_Pt, "SL Price" );  
AddColumn( Buy, "Buy" );  
AddColumn( Cross( SL_Pt, L ), "SL Hit" );  
AddColumn( Sell, "Sell" ); 

In the above code I am trying to exit if stock has fallen 90 points from BuyPrice then I will exit.
This is not working in this below case, not sure why.

image
Please see the above image, the stock price has fallen more than 90 points but still its exiting on other exit criteria but not on the hard SL point.

Explorer is showing the sell condition met, but Sell is not true.
image

Any help to fix this would be greatly appreciated.

Stop code is incorrect.

For proper stops either use ApplyStop or looping.
Point stop example

And more at site:amibroker.com/kb applystop - Google Search


Besides this

MACD_cross = Cross(Ref(Signal(),-1),Ref(MACD(),-1)); 

can be simplified to

MACD_cross = Ref(Cross(Signal(), MACD()), -1);

In General: reduce function calls.


This

 AND Day() == Buyday AND TimeNum() > Buy_Time

is not required (and will not work properly anyway as you possibly use ValueWhen(Buy,..)).
Besides you exit at end of day already.
And for sessions and avoiding overnight positions rather follow below KB article.


If you want to exit at reverse signal then there is SetOption there.

SetOption("ReverseSignalForcesExit", true);

:no_entry:
No, no there.
(ValueWhen() may be used after the fact(s) (e.g. for plotting stop lines, see upper stop code example again)).

1 Like

Thanks @fxshrat for your response. If I ignore all sell conditions, and want to continue with price base stop loss, then this condition is not working. though in explorer "Cross( SL_Pt, L )" is giving True signal but in the same candle "Sell" is False, not sure why. Is there any logic behind that which I am not aware.
I understand by using ApplyStop function backtest will work fine and its working fine. But for actual trading I need to use loop and looks like that is the only solution to this.
Please suggest me if you think otherwise.
However with this solution, my question to the explorer is still a mystery to me.

There is no difference between adding Cross(SL_PT, L) to AddColumn and adding Sell of Sell = Cross(Sl_PT, L) to Addcolumn . One just stores true/false array condition before Addcolumn output. That's all.

Buy = Cross(C, MA(C,20));
BuyPrice = Close;

Point = 2;  

SL_Pt = BuyPrice - Point;  

Sell = Cross(SL_Pt, L);   // DON'T DO THAT 

Filter = Cross(SL_Pt, L) > 0 OR Sell > 0;  

AddSummaryRows(1, 1);
AddColumn( SL_Pt, "SL Price" ); 
AddColumn( Buy, "Buy" );  
AddColumn( Cross( SL_Pt, L ), "SL Hit" ); 
AddColumn( Sell, "Sell" ); 

If you get difference then you have additional Sell code there.


But once again.. this

SL_Pt = BuyPrice - Point;  
Sell = Cross( SL_Pt, L );  

is simply incorrect. BuyPrice does not hold time (of entry) information. It just stores price array.
So it is the same as if you do e.g. this

SL_Pt = Open - Point;  
Sell = Cross( SL_Pt, L );  

The way you do simply is stop no go. So don't do it.


And as for stops in real-time trading see here (again).

1 Like