How to avoid exiting current position, opposite signal is triggered

Hi All,
I am facing a problem please guide me to solve it.
I have both Buy and Short signals in my system.

I have a trailing stop loss mechanism where I update the Sell and Cover values. I did not use Applystop.

The problem is, when I am already in a long trade, a new short signal is coming which is exiting the current long trade and placing a new short signal. But my expectation is, to stay in the long trade as long as its stop loss gets hit through trailing.

Buy = BuySignal AND TimeNum() < 150000;
BuyPrice = ValueWhen(Buy,C,0) * 1.001;
Sell = L < HighestSince(Buy, H - 3*ATR(10)) OR TimeNum() == 151500;
Short = ShortSignal AND TimeNum() < 150000;
ShortPrice = ValueWhen(Short,C,0) * 0.999;
Cover = H > LowestSince(Short, L + 3*ATR(10)) OR TimeNum() == 151500;

inLongTrade = Flip(Buy,Sell);
SetOption("EveryBarNullCheck",True);
stopLineLong = IIf(inLongTrade,HighestSince(Buy, H - 3*ATR(10)),Null);
Plot(stopLineLong,"Stop Loss Long",colorRed,styleLine|styleThick,0,0,0,0);

inShortTrade = Flip(Short,Cover);
SetOption("EveryBarNullCheck",True);
stopLineShort = IIf(inShortTrade,LowestSince(Short, L + 3*ATR(10)),Null);
Plot(stopLineShort,"Stop Loss short",colorGreen,styleLine|styleThick,0,0,0,0);

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);```

Use

SetOption("ReverseSignalForcesExit", False);

Why 2 of these?

SetOption("EveryBarNullCheck",True);

It was copied from a youtube code, Where it was explained that it is used to check if the bar is not null.,

You only need this once in your code.

Yes, correct. I removed it. And the solution you gave works fine. Thank you very much.,

1 Like