Two or more consecutive down bars within 5 bars


Category:
0
0

Hello,

I am trying to write a scan condition where consecutive 2 or more down bars occurred within the last 5 bars in 30 minutes time frame. Can anyone please help with the code?

Thank you very much in advance!

RESOLVED
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on September 20, 2021 3:17 pm
124 views
0
Private answer

You mentioned "up" bars and "down" bars but you did not include a definition. There are two definitions available:

  1. Up Bar: Close of current bar is greater than close of previous bar
  2. Up Bar: Close of current bar is greater than open of current bar

Since you did not specify one or the other I have selected option 1 for this solution. Please note the candles on Thinkorswim are colored based on option 2. Which means on Thinkorswim, a bar that closes higher than the previous can still be colored red if it closes below the open.

For this solution I have included user inputs to allow full flexibilty for the end user (except that you cannot select between option 1 or 2 as listed above). You can select the number of consecutive bars, the number of total bars to include in the patter and the direction of those consecutive bars, either up or down.

input consecutiveBars = 2;
input totalBars = 5;
input direction = {up, default down};
def consecutiveUpDown = if direction == direction.up then Sum(close > close[1], consecutiveBars) >= consecutiveBars else
Sum(close < close[1], consecutiveBars) >= consecutiveBars;
plot scan = Sum(consecutiveUpDown, totalBars) > 0;

Marked as spam
Posted by (Questions: 37, Answers: 4079)
Answered on September 20, 2021 5:11 pm
0
Thank you much! It works like a charm!
( at September 30, 2021 9:18 am)