Enter on second consequtive signal


Unable to get desired result after trying the below code. This code ended up removing all Buy signals.

Buy_sig = IIf(Buy,1,0);
Short_sig = IIf(Short,-1,0);
Sig_Count=Cum(Buy OR Short);
Buy = Buy AND  (ValueWhen(Ref(Sig_Count,-2) < Sig_Count,Short_sig) == -1);

We enter long only if there was-

  1. A buy signal earlier, and
  2. a short signal before that buy signal
    (sequence - Short, Buy, Buy - we enter long on second buy)

What should I change in the code above?

I get it that I am just referencing the bar which is 2 bars ago. guess it can't be done without a loop.

How about this:

Buy = BuyTrigger AND Ref( BuyTrigger, -1 ) AND Ref( ShortTrigger, -2 );

That is exact sequence you described. If you want to increase the "window" for short triggering, you can use hold function or BarsSince.

Buy = BuyTrigger AND Ref( BuyTrigger, -1 ) AND BarsSince( ShortTrigger ) >= 2;
1 Like

It was my mistake, Tomasz, that I did not say that there can be any number of bars between any 2 signals. That I should have added in my problem statement. Because signals are not coming up on consecutive bars.

So, let's say:

  1. we had a Short signal
  2. After "n" bars we get a Buy signal
  3. And "x" bars after Buy of step 2, we receive another buy signal
    ("n" and "x" are unknown and vary)
    We go LONG on Step 3 completion.

Thanks for your time. Really appreciate all the guidance. I have started to learn FOR loops for this problem. Always get stuck in such situations.

Thanks for your time.

Buy =  Point3 AND BarsSince( Point2 ) > 1 AND BarsSince( Point3 ) > BarsSince( Point2 );
1 Like

I tried your approach with the below example. But it did not work.

HH = Ref(HHV(H,5),-1);
LL = Ref(LLV(L,5),-1);

Point1 = Cross(LL,C);
Point2 = Cross(C,HH);
Point3 = Cross(C,HH);

Buy =  Point3 AND BarsSince( Point2 ) > 1 AND BarsSince( Point3 ) > BarsSince( Point2 );

//AND BarsSince(Point1) > BarsSince(Point2);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),ParamColor("Up Arrow Color",colorBrightGreen), 0,L, Offset=-12); 
Plot(HH,"HH",colorGreen,styleDashed+ styleNoRescale);


Try this

Point1 = Cross(LL,C);
Point2 = Cross(C,HH);

Buy =  Point2 AND SumSince( Point1, Point2) == 2;


@awilson @Tomasz thanks :pray: You guys rock. And, thanks a lot for your time. Really appreciate it. Hopefully, I will be able to come up with better problems/ questions next time.

1 Like