Hi all, I am trying to set a custom trailing stop loss which bases on previous high price * 1.03. For example, if I short one contract of S&P 500 futures on 15/12/2020 with entry price of $3000 and the high on this day is 3100. Then, the trailing stop loss on 16/12/2020 will be 3100 * 1.03 = 3193. So if S&P500 crosses 3193 on 16/12/2020 within the day, I will exit at that price with no delay. I wrote the following codes but turned out that it seems to have triggered the stop loss on the same day and exit on the next day open. Could someone points me where did I go wrong? Thank you.
_SECTION_BEGIN("Simple RSI Momentum Long Only Strategy");
SetTradeDelays(0,0,0,0);
//Trading Logic
Short = RSI(5) < 40 ;
Cover = 0;
ApplyStop(stopTypeLoss, stopModePercent, Optimize( "max. loss stop level", 3, 2, 30, 1 ), 1);
ApplyStop( stopTypeNBar, stopModeBars,Optimize( "num of days to stop",17,5,30,1 ),1);
SL_above_previous_high = Ref(High,-1) * 1.03;
ApplyStop( stopTypeTrailing, stopModePercent, SL_above_previous_high ,1);
ApplyStop( stopTypeProfit, stopModePercent, Optimize("profit stop",20,1,30,1),1);
Equity(1,-1);
SetStopPrecedence( stopTypeNBar, stopTypeTrailing, stopTypeLoss, stopTypeProfit );
//Position Size
PositionSize = MarginDeposit = 1;
_SECTION_END();
PlotShapes(Cover*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Short*shapeDownArrow,colorRed,0,High);
Plot( Close,"Price",colorBlack,styleBar);
Plot(SL_above_previous_high , "trailing stop line", colorRed );