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.
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.
Any help to fix this would be greatly appreciated.
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, no there.
(ValueWhen() may be used after the fact(s) (e.g. for plotting stop lines, see upper stop code example again)).
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.