Use a fixed denominator and an explicit flat-bar rule
For bar t, set U[t] to 1 when close[t] > close[t−1], and to 0 otherwise, provided both closes exist. Sum U[t] through U[t−N+1], divide by N and multiply by 100. The current completed bar is included. This follows the up-bar definition in TradeStation documentation and public TradingView PSY descriptions; implementations can differ in smoothing and initialization.
- Removing flat bars from the denominator defines a different ratio. Counting a flat bar as half an up bar also changes the definition.
- A candle can close below its open but above the previous close after an upward gap. It still contributes 1 here.
- The first close supplies a baseline, not a zero-return observation. With N = 12, the first complete result needs 13 valid closes.
| Close comparison | Contribution | Window treatment |
|---|---|---|
| Higher than previous close | 1 | Counts in numerator and denominator |
| Equal to previous close | 0 | Counts only in denominator |
| Lower than previous close | 0 | Counts only in denominator |
| Either close unavailable | Unavailable | This example leaves the whole window unavailable |