Backtest idea with ATR stop and 1.4 * Risk Target


I have trade idea that I want to backtest.

  1. Buy Signal can be anything, let say golden cross MACD, entry on close
  2. stop loss need to be the last 5 days low from that bar minus the last 14 days ATR
  3. the target sell need to be 1.4 * the risk ( entry price - stop loss). It does not have to be 1.4, it can be any number.

I am not sure how it gonna work, and I want to use backtest to check it.

Here are what I did so far, I am not sure this implementation is correct or not.

BuySignal 		= Cross(MACD(), Signal());
Low5 			= LLV(L, 5); atr14 = ATR(14);
SLATR 		 	= Low5 - atr14;

BuyPrc			= ValueWhen(BuySignal, Close);
Stop 			= ValueWhen(BuySignal, SLATR);

Risk 			= BuyPrc - Stop;
TargetSell		= BuyPrc + (1.4 * Risk);

Buy 		= BuySignal;
Sell 		= IIf(H > TargetSell OR Low < Stop, True, False);

BuyPrice 	= Close;

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorBlue, 0, L, -12);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, H, -12);

ApplyStop(stopTypeLoss, stopModePoint, Stop);

The problem that I saw is, since the buy signal can occur multiple time, the TargetSell is changed with the new one, especially when the sell signal is not triggered yet. the same issue happen for the stop loss.

How can we make the stop and target follow the one when the buy signal is triggered?

Thank you

1 Like

Just use ONLY ApplyStop (not ValueWhen) for BOTH stop loss AND profit target
and use Sell = 0.

1 Like

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