Custom trailing stop loss based on yesterday high

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 );

As far as I know, AmiBroker's built-in trailing stop always trails behind the High for Long trades and behind the Low for Short trades. You are trying to use the High as your reference price for Short trades, which I don't think is supported.

However, you don't need to use ApplyStop at all for your exit, because you can just treat it as a normal Cover signal. Just do something like this:

SL_above_previous_high = Ref(High,-1) * 1.03;
Cover = H > SL_above_previous_high;
CoverPrice = Max(O, SL_above_previous_high);

3rd argument of ApplyStop() expects amount but not price level.

//Trading Logic
Short = RSI(5) < 40 ;
Cover = 0;

TickSize = 0.01;
amount = Max(TickSize, Ref(High,-1)*1.03-L);

ApplyStop( stopTypeTrailing, stopModePoint, amount, 1);

Or

//Trading Logic
Short = RSI(5) < 40 ;
Cover = 0;

TickSize = 0.01;
prev = Ref(High,-1)*1.03;
amount = Max(TickSize, prev-L) / L * 100;

ApplyStop( stopTypeTrailing, stopModePercent, amount, 1);

....

2 Likes

This looks like a good idea, simple and fast. I found it executes at today's high instead of (yesterday high*1.03). I tried twisting the codes but could not find any clue why this happened.

I am not sure why it requires to minus L because after entering the short position, I would like the trailing stop loss to be a factor of 1.3 of yesterday high, so it's unlike of traditional trailing stop loss and this trailing stop loss will fluctuate up and down.

AmiBroker's default behavior is to limit your trade execution prices to the actual range of the day. For example, if your Sell signal is True and your SellPrice is $110, but the high for that bar is only $100, then AmiBroker will close the trade at $100, i.e. the high of the day.

In your example, it sounds like maybe you are generating cover signals for bars when the High is not greater than the target exit price. A quick Exploration showing Cover, CoverPrice, SL Price and High Price should help you figure that out.

Thanks, I see the reason now. I have made a bit of twist and it works fine.

Actually turned out I cross checked your codes' result with my excel result, it is right.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.