Count consecutive bars above or below prior day high or low


Category:
0
0

I want to create a watchlist column with counter for price crossing previous day high/low on 15m timeframe. Green background color for cross above and red for cross below showing number of bars since cross.

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on July 13, 2025 2:39 pm
31 views
0
Private answer

For the solution below I borrowed code from the previous solution listed here:

https://www.hahn-tech.com/ans/pre-market-hl-previous-days-hlc/

This solution requires the watchlist column is set to EXCLUDE extended session hours and is set to any intraday time frame of 2 min or higher.

def newDay = GetDay() <> GetDay()[1];
rec dailyHigh = if newDay then high else if high > dailyHigh[1] then high else dailyHigh[1];
rec dailyLow = if newDay then low else if low > 0 and low < dailyLow[1] then low else dailyLow[1];
rec previousHigh = if newDay then dailyHigh[1] else previousHigh[1];
rec previousLow = if newDay then dailyLow[1] else previousLow[1];
rec countAboveHigh = if close[1] < previousHigh and close > previousHigh then 1 else if close > previousHigh then countAboveHigh[1] + 1 else countAboveHigh[1];
rec countBelowLow = if close[1] > previousLow and close < previousLow then 1 else if close < previousLow then countBelowLow[1] + 1 else countBelowLow[1];
plot data = if close > previousHigh then countAboveHigh else if close < previousLow then countBelowLow else 0;
data.AssignValueColor(if data <> 0 then Color.BLACK else Color.CURRENT);
AssignBackGroundColor(if close > previousHigh then Color.GREEN else if close < previousLow then Color.RED else Color.CURRENT);

Marked as spam
Posted by (Questions: 37, Answers: 4138)
Answered on July 13, 2025 2:42 pm