TradingView indicator calculations

Psychological Line indicator: count up closes within a fixed window

The Psychological Line indicator (PSY or PSL) is the percentage of the last N chart bars whose close exceeds the preceding close. PSY = 100 × up closes / N. Under the strict comparison used here, an unchanged close contributes zero but remains in the denominator. A reading of 75 means three quarters of the window closed higher; it measures neither the size of those gains nor investor survey sentiment.

See the decision guide

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 comparisonContributionWindow treatment
Higher than previous close1Counts in numerator and denominator
Equal to previous close0Counts only in denominator
Lower than previous close0Counts only in denominator
Either close unavailableUnavailableThis example leaves the whole window unavailable

Recalculate a six-bar window, including a flat close

These prices are hypothetical arithmetic inputs, not market observations. Set N = 6 and begin with a baseline close of 100. At bar 6, the six comparisons contain four up closes, one unchanged close and one down close.

  • At bar 6, excluding the flat bar would produce 4 / 5 × 100 = 80. That is not the fixed-window result of 66.67.
  • At bar 7, bar 1 leaves the window and the new flat bar enters. PSY falls by 16.67 percentage points even though the latest price is unchanged.
  • For N = 6, one count is 100 / 6 = 16.67 percentage points. A longer window has smaller single-count steps; it also retains older observations longer.
BarCloseUp contributionPSY with N = 6
0100Baseline onlyUnavailable
11011Warm-up
21021Warm-up
31020 (flat)Warm-up
41010 (down)Warm-up
51031Warm-up
610414 / 6 × 100 = 66.67
71040 (flat)3 / 6 × 100 = 50.00

Separate threshold states, crossing events and winning streaks

TradeStation exposes 25 and 75 as reference levels, but a reference is not a reversal probability or a complete trading rule. For an unsmoothed N-bar line, only multiples of 100 / N are possible. Specify the comparison operator before creating an alert.

  • Using U for up and D for down, U U U D U U U D U U U D has nine ups out of twelve and PSY = 75. Its longest up streak is three and its current up streak is zero.
  • U U U U U U U U U D D D also has PSY = 75, but its longest up streak is nine. The percentage ignores order within the window.
  • Nine hypothetical gains of 0.1 and three losses of 1 produce PSY = 75 while the net price change is −2.1. Counting up closes does not measure net return.
  • A high reading can persist in an advancing market. Selling solely because a level is high has no demonstrated edge from this calculation alone.
Rule with N = 12Required up closesMeaning
PSY ≥ 75At least 9Current level is at or above 75
PSY > 75At least 10Current level is strictly above 75
Cross above 75Now at least 10; previous value at most 75A transition, not every bar above the level
PSY ≤ 25At most 3Current level is at or below 25
PSY < 25At most 2Current level is strictly below 25

Match the trading calendar and timeframe

The length counts observed chart bars, not calendar days. Exchange sessions determine when those bars exist. Match the symbol, data feed, session setting, timeframe and candle type before comparing two PSY values.

  • For a holiday-free Monday-to-Friday equity series ending Friday, twelve daily bars run from Thursday two weeks earlier through Friday. The thirteen closes needed for the calculation begin on the preceding Wednesday.
  • Switching an intraday equity chart between regular and extended sessions changes which bars enter the count. A daily bar and its intraday subdivisions do not receive equivalent weighting.
  • A 12-bar hourly reading cannot be reconstructed by averaging twelve 12-bar 5-minute readings. Their rolling comparisons overlap and use different closing observations.
  • Use ordinary price candles for this audit. Synthetic chart closes, such as Heikin Ashi closes, change the input series and therefore the question being measured.
Chart and N = 12What is countedComparison limit
Daily equity chartTwelve available daily barsWeekends and exchange holidays without bars are not flat observations
Daily 24/7 marketTwelve daily bars, including weekends when data existsThe elapsed calendar window differs from a weekday-only market
Hourly chartTwelve available hourly barsOvernight closures or session changes can extend the elapsed window
5-minute chartTwelve available 5-minute barsOne hour only when bars are contiguous and complete

Audit the count in Pine Script v6

This example uses the chart timeframe, a fixed denominator and no smoothing. It plots completed bars only, leaves incomplete or invalid windows blank, and exposes the up count in the Data Window. The alert is a strict cross above 75 on a closed chart bar.

  • Paste into Pine Editor, add it to a standard candle chart, and inspect a completed timestamp. Count the previous N comparisons from N + 1 closes and match the Data Window values.
  • For N = 1, each valid result is 100 for an up close and 0 for a flat or down close. An all-flat full window is 0, not 50, under this strict rule.
  • The current realtime plot is blank until the bar closes. A temporary intrabar move above the preceding close does not become a confirmed plotted observation.
  • Create the alert from the confirmed-cross condition after verifying the values. No alert is emitted merely because the first valid observation is already above 75; a crossing needs a valid previous value.
Original implementation of the stated counting rules. Syntax checked with Pineify; chart-value parity and trading performance require separate testing.pine
//@version=6
indicator("Psychological Line count audit", overlay=false, precision=2)
length = input.int(12, "Window bars", minval=1, maxval=5000)
validPair = not na(close) and not na(close[1])
up = validPair and close > close[1] ? 1.0 : 0.0
validCount = math.sum(validPair ? 1.0 : 0.0, length)
upCount = math.sum(up, length)
fullWindow = validCount == length
psy = fullWindow ? 100.0 * upCount / length : na
confirmedPsy = barstate.isconfirmed ? psy : na
plot(confirmedPsy, "PSY (%)", color=color.blue, linewidth=2, style=plot.style_linebr)
plot(barstate.isconfirmed and fullWindow ? upCount : na, "Up closes", display=display.data_window)
hline(75, "Upper reference", color=color.gray)
hline(50, "Midpoint", color=color.gray, linestyle=hline.style_dotted)
hline(25, "Lower reference", color=color.gray)
crossAbove75 = ta.crossover(psy, 75.0)
alertcondition(barstate.isconfirmed and crossAbove75, "Confirmed cross above 75", "PSY crossed strictly above 75 on a closed chart bar.")

Specify the same rules when asking Pineify for changes

The Pine Script AI Coding Agent can help draft or modify the script from this public specification. Include the close-to-close comparison, fixed N denominator, flat-bar policy, warm-up requirement, chart session and close-confirmation rule. Request syntax checking explicitly, then compare completed-bar values in TradingView before using an alert.

  • Keep the six-bar fixture and the nine-versus-ten count distinction in the request so revisions can be checked against expected values.
  • If adding smoothing, retain the raw PSY plot. Smoothed values no longer have the same count-to-threshold mapping.
  • A community script may use a raw count, different tie handling or hidden logic. This specification does not reproduce proprietary source or prove parity with every script called Psychological Line.
Where Pineify fits

Pine Script AI Coding Agent

Draft or revise a PSY indicator from explicit counting and confirmation rules, then request syntax checking and review the chart values.

Draft a PSY indicator

Frequently asked questions

Educational information, not investment advice. Numerical examples are hypothetical. PSY thresholds and syntax-valid code do not establish a trading edge or guarantee returns.

Sources and verification

Published by Pineify. Pineify is an independent product and is not affiliated with TradingView. Sources support the technical explanations; examples do not establish trading performance.