OPEN-SOURCE SCRIPT

Trend Bars with Counter Table

103
# TradingView Trend Bar Indicator Explained

## Indicator Overview

This is a TradingView indicator designed to identify and count **Trend Bars**. It not only visually marks strong bullish and bearish bars on the chart but also displays a data table in the upper right corner that tracks the distribution of trend bars across different periods, helping traders quickly assess market bias.

## Core Concept: What is a Trend Bar?

The indicator defines two types of trend bars:

### Bull Trend Bar
- **Condition**: Close > Open (bullish candle)
- **Strength Requirement**: Body size ≥ 75% of total candle range

```
Body Length = |Close - Open|
Total Candle Range = High - Low
Criteria: Body Length ≥ 0.75 × Total Candle Range
```

This means both upper and lower wicks are very short, representing a very strong bullish candle.

### Bear Trend Bar
- **Condition**: Close < Open (bearish candle)
- **Strength Requirement**: Body size ≥ 75% of total candle range

Similarly, this represents a strong bearish candle with minimal wicks and a full body.

## Visual Markers

The indicator marks qualifying candles with:
- **Green upward arrow**: Bull trend bar, appears below the candle
- **Red downward arrow**: Bear trend bar, appears above the candle

## Statistical Function

The indicator uses a **rolling array** (storing up to 1000 trend bars) to track historical data, then counts trend bar distribution across 5 different periods:

| Period | Statistical Range |
|--------|------------------|
| Group 1 | Last 7 trend bars |
| Group 2 | Last 15 trend bars |
| Group 3 | Last 21 trend bars |
| Group 4 | Last 29 trend bars |
| Group 5 | Last 35 trend bars |

**Note**: This counts "the last N trend bars," not "the last N candles." Only candles meeting the trend bar criteria are included.

## Data Table Interpretation

The table in the upper right corner contains 5 columns:

1. **Last N**: The set statistical range (7, 15, 21, 29, 35)
2. **Total**: Actual number of trend bars counted (may be less than target initially)
3. **Bull**: Number of bull trend bars (displayed in green)
4. **Bear**: Number of bear trend bars (displayed in red)
5. **Bias**: Market bias
- "bull" (green): More bull trend bars
- "bear" (red): More bear trend bars

## Practical Applications

### 1. Assess Short-term Momentum
Check the distribution of the last 7 trend bars. If bull trend bars dominate (e.g., 5:2), it indicates strong short-term buying pressure.

### 2. Identify Trend Strength
If multiple periods show the same Bias direction, the trend is very clear. For example, all 5 periods showing "bull" is a strong upward signal.

### 3. Spot Trend Reversals
When short-term bias (7 bars) opposes long-term bias (35 bars), it may signal a trend change in progress.

### 4. Combine with Other Indicators
Use this indicator alongside moving averages, support/resistance levels, and other tools to improve trading decision accuracy.

## Technical Highlights

- **Dynamic Array Management**: Uses `array.unshift()` to add new data at the array's beginning, ensuring the latest trend bars are always first
- **Efficient Statistics**: Quickly calculates bull/bear distribution through loop iteration over specified array ranges
- **Adaptive Display**: Shows actual available count when historical data is insufficient
- **Real-time Updates**: Only updates the table on the last bar to avoid unnecessary calculations

## Conclusion

The core value of this indicator lies in **quantifying price action**. By identifying strong candles with full bodies and clear direction, then tracking their distribution, traders can quickly grasp the balance of market forces and make more informed trading decisions. Whether for intraday trading or swing trading, this tool provides valuable reference information.

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ ข้อกำหนดการใช้งาน