Watchlist for Consecutive Bullish and Bearish Candles


Category:
0
0

Is there a way to create a watchlist column to show the number of consecutive bullish or bearish candles for a stock in the watchlist? Thank you.

RESOLVED
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on June 6, 2023 10:06 am
89 views
0
Private answer

We can barrow some code from a similar solution based on the TTM Trend indicator. That solution is located here:

https://www.hahn-tech.com/ans/ttm-trend-consecutive-bar-count/

However it depends on how one defines a "Bullish" bar and a "Bearish" bar. We have two choices:

  1. Bullish == close > previous close AND Bearish == close < previous close
  2. Bullish == close > open AND Bearish == close < open

You did not specify which of these you wanted to use so I will provide a solution which is based on option 1:

def higherClose = close > close[1];
def lowerClose = close < close[1];
rec countUpTrend = if higherClose and !higherClose[1] then 1 else if higherClose then countUpTrend[1] + 1 else 0;
rec countDownTrend = if lowerClose and !lowerClose[1] then -1 else if lowerClose then countDownTrend[1] - 1 else 0;
plot data = if countUpTrend > 0 then countUpTrend else countDownTrend;
AssignBackgroundColor(if data > 0 then Color.GREEN else if data < 0 then Color.RED else Color.CURRENT);

 

If you wanted to change that to use option 2 then you would modify the first two lines of code as follows:

def higherClose = close > open;
def lowerClose = close < open;

However if you do that the names of those variables should be updated to avoid confusion. But I'll leave that task up to you.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on June 6, 2023 10:39 am
0
Pete, thank you for this solution. It is exactly what I needed. I accidentally submitted this reply as a solution. Please disregard it.
( at June 6, 2023 12:41 pm)
0
Yep, glad I could help!
( at June 6, 2023 1:00 pm)