OPEN-SOURCE SCRIPT

O'Neil Market Timing

86
Bill O'Neil Market Timing Indicator - User Guide

Overview

This Pine Script indicator implements William O'Neil's market timing methodology, which assigns one of four distinct states to a market index (such as SPY or QQQ) to help traders identify optimal market conditions for investing. The indicator is designed to work exclusively on Daily timeframe charts.

The Four Market States

The indicator tracks the market through four distinct states, with specific transition rules between them:

1. Confirmed Uptrend (Green)
- Meaning: The market is in a healthy uptrend with institutional support
- Action: Favorable conditions for building positions in leading stocks
- Can transition to: State 2 (Uptrend Under Pressure)

2. Uptrend Under Pressure (Yellow)
- Meaning: The uptrend is showing signs of weakness with increasing distribution
- Action: Be cautious, tighten stops, reduce position sizes
- Can transition to: State 1 (Confirmed Uptrend) or State 3 (Downtrend)

3. Downtrend (Red)
- Meaning: The market is in a confirmed downtrend
- Action: Stay mostly in cash, avoid new purchases
- Can transition to: State 4 (Rally Attempt)

4. Rally Attempt (Pink/Fuchsia)
- Meaning: The market is attempting to bottom and reverse
- Action: Watch for Follow-Through Day to confirm new uptrend
- Can transition to: State 1 (Confirmed Uptrend) or State 3 (Downtrend)

Key Concepts

Distribution Day
A distribution day occurs when:
1. The index closes down by more than the critical percentage (default 0.2%)
2. Volume is higher than the previous day's volume

Distribution days indicate institutional selling and are marked with red triangles on the indicator.

Follow-Through Day
A follow-through day occurs during a Rally Attempt when:
1. The index closes up by more than the critical percentage (default 1.6%)
2. Volume is higher than the previous day's volume

A Follow-Through Day confirms a new uptrend and triggers the transition from Rally Attempt to Confirmed Uptrend.

State Transition Logic

Valid Transitions
The system only allows specific transitions:
- 1 → 2: When distribution days reach the "pressure number" (default 5) within the lookback period (default 25 bars)
- 2 → 1: When distribution days drop below the pressure number
- 2 → 3: When distribution days reach "downtrend number" (default 7) AND price drops by "downtrend criterion" (default 6%) from the lookback high
- 3 → 4: When the market doesn't make a new low for 3 consecutive days
- 4 → 3: When a new low is made, undercutting the downtrend low
- 4 → 1: When a Follow-Through Day occurs during the Rally Attempt

Input Parameters

Distribution Day Parameters
- Distribution Day % Threshold (default 0.2%, range 0.1-2.0%)
- Minimum percentage decline required to qualify as a distribution day. While 0.2% seems to be the canonical number I see in literature about this, I use a much higher threshold (at least 0.5%)

Follow-Through Day Parameters
- Follow-Through Day % Threshold (default 1.6%, range 1.0-2.0%)
- Minimum percentage gain required to qualify as a follow-through day

### State Transition Parameters
- Pressure Number (default 5, range 3-6)
- Number of distribution days needed to transition from Confirmed Uptrend to Uptrend Under Pressure

- Lookback Period (default 25 bars, range 20-30)
- Number of days to count distribution days

- Downtrend Number (default 7, range 4-10)
- Number of distribution days needed (with price drop) to transition to Downtrend

- Downtrend % Drop from High (default 6%, range 5-10%)
- Percentage drop from lookback high required for downtrend confirmation

Visual Settings
- Color customization for each state
- Table position selection (Top Left, Top Right, Bottom Left, Bottom Right)

## How to Use This Indicator

### Installation
1. Open TradingView and navigate to SPY or QQQ (or another major index)
2. **Important**: Switch to the Daily (1D) timeframe
3. Click on "Indicators" at the top of the chart
4. Click "Pine Editor" at the bottom of the screen
5. Copy and paste the Pine Script code
6. Click "Add to Chart"

### Interpretation

**When the indicator shows:**
- **Green (State 1)**: Market is healthy - consider adding quality positions
- **Yellow (State 2)**: Exercise caution - tighten stops, be selective
- **Red (State 3)**: Defensive mode - preserve capital, avoid new buys
- **Pink (State 4)**: Watch closely - prepare for potential Follow-Through Day

### The Information Table

The table displays:
- **Current State**: The current market condition
- **Distribution Days**: Number of distribution days in the lookback period
- **Lookback Period**: Number of bars being analyzed
- **Rally Attempt Day**: (Only in State 4) Days into the current rally attempt

### Visual Elements

1. **State Line**: A stepped line showing the current state (1-4)
2. **Red Triangles**: Mark each distribution day
3. **Horizontal Reference Lines**: Dotted lines marking each state level
4. **Color-Coded Display**: The state line changes color based on the current market condition

## Trading Strategy Guidelines

### In Confirmed Uptrend (State 1)
- Build positions in stocks breaking out of proper bases
- Use normal position sizing
- Focus on stocks showing institutional accumulation
- Hold winners as long as they act properly

### In Uptrend Under Pressure (State 2)
- Take partial profits in extended positions
- Tighten stop losses
- Be more selective with new entries
- Reduce overall exposure

### In Downtrend (State 3)
- Move to cash or maintain very light exposure
- Avoid new purchases
- Focus on preservation of capital
- Use the time for research and watchlist building

### In Rally Attempt (State 4)
- Stay mostly in cash but prepare
- Build a watchlist of strong stocks
- On Day 4+ of the rally attempt, watch for Follow-Through Day
- If FTD occurs, begin cautiously adding positions

## Best Practices

1. **Use with Major Indices**: This indicator works best with SPY, QQQ, or other broad market indices
2. **Daily Timeframe Only**: The indicator is designed for daily bars - do not use on intraday timeframes
3. **Combine with Stock Analysis**: Use the market state as a filter for individual stock decisions
4. **Respect the Signals**: When the market enters Downtrend, reduce exposure regardless of individual stock setups
5. **Monitor Distribution Days**: Pay attention when distribution days accumulate - it's a warning sign
6. **Wait for Follow-Through**: Don't jump back in too early during Rally Attempt - wait for confirmation

## Alert Conditions

The indicator includes built-in alert conditions for:
- State changes (entering any of the four states)
- Distribution Day detection
- Follow-Through Day detection during Rally Attempt

To set up alerts:
1. Click the "Alert" button while the indicator is on your chart
2. Select "O'Neil Market Timing"
3. Choose your desired alert condition
4. Configure notification preferences

## Customization Tips

### For More Sensitive Detection
- Lower the "Pressure Number" to 3-4
- Lower the "Distribution Day % Threshold" to 0.15%
- Reduce the "Downtrend Number" to 5-6

### For More Conservative Detection
- Raise the "Pressure Number" to 6
- Raise the "Distribution Day % Threshold" to 0.3-0.5%
- Increase the "Downtrend Number" to 8-9

### For Different Market Conditions
- **Bull Market**: Consider slightly higher thresholds
- **Bear Market**: Consider slightly lower thresholds
- **Volatile Market**: May need to increase percentage thresholds

## Limitations and Considerations

1. **Not a Crystal Ball**: The indicator identifies conditions but doesn't predict the future
2. **False Signals**: Follow-Through Days can fail - use proper risk management
3. **Whipsaws Possible**: In choppy markets, the indicator may switch states frequently
4. **Confirmation Lag**: By design, there's a lag as the system waits for confirmation
5. **Works Best with Price Action**: Combine with your analysis of individual stocks

## Historical Context

This methodology is based on William J. O'Neil's decades of market research, documented in books like "How to Make Money in Stocks" and through Investor's Business Daily. O'Neil's research showed that:
- Most major market tops are preceded by accumulation of distribution days
- Most successful rallies begin with a Follow-Through Day on Day 4-7 of a rally attempt
- Identifying market state helps prevent buying during unfavorable conditions

## Troubleshooting

**Problem**: Indicator shows "Initializing"
- **Solution**: Let the chart load at least 5 bars to establish the initial state

**Problem**: No distribution day markers appear
- **Solution**: Verify you're on daily timeframe and check if volume data is available

**Problem**: Table not visible
- **Solution**: Check the table position setting and ensure it's not off-screen

**Problem**: State seems to change too frequently
- **Solution**: Increase the lookback period or adjust threshold parameters

## Support and Further Learning

For deeper understanding of this methodology:
- Read "How to Make Money in Stocks" by William J. O'Neil
- Study Investor's Business Daily's "Market Pulse"
- Review historical market tops and bottoms to see the pattern
- Practice identifying distribution days and follow-through days manually

## Version History

**Version 1.0** (November 2025)
- Initial implementation
- Four-state system with proper transitions
- Distribution day detection and marking
- Follow-through day detection
- Customizable parameters
- Information table display
- Alert conditions

---

## Quick Reference Card

| State | Number | Color | Action |
|-------|--------|-------|--------|
| Confirmed Uptrend | 1 | Green | Buy quality setups |
| Uptrend Under Pressure | 2 | Yellow | Tighten stops, be selective |
| Downtrend | 3 | Red | Cash position, no new buys |
| Rally Attempt | 4 | Pink | Watch for Follow-Through Day |

**Distribution Day**: Down > 0.2% on higher volume (red triangle)
**Follow-Through Day**: Up > 1.6% on higher volume during Rally Attempt (triggers State 4→1)

---

*Remember: This indicator is a tool to help identify market conditions. It should be used as part of a comprehensive trading strategy that includes proper risk management, position sizing, and individual stock analysis.*

Also, I created this with the help of an AI coding framework, and I didn't exhaustively test it. I don't actually use this for my own trading, so it's quite possible that it's materially wrong, and that following this will lead to poor investment decisions.. This is "copy left" software, so feel free to alter this to your own tastes, and claim authorship.

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

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.