OPEN-SOURCE SCRIPT
Atualizado

Session Statistical Levels

8 528
## Session Statistical Levels

A universal session range statistics indicator that calculates the historical distribution of how each trading session has behaved and plots those levels forward from the current session's opening price. Works on any instrument, any timeframe, and tracks up to four sessions simultaneously — Asia, London, New York, and a fully configurable custom session.

Everything here is derived live from your chart's own price history. The levels update at the open of each new session and extend rightward as the session develops.

---

## IMPORTANT DISCLAIMER

All statistics are calculated in real time from your chart's own bar history within the chosen lookback window. This means:

- Statistics will differ between users depending on chart history available and the lookback setting chosen.
- The indicator requires a minimum of 2 completed sessions to begin drawing levels, but meaningful statistics require at least 20–30 sessions. The more history available, the more stable the percentile estimates.
- Past session behaviour does not guarantee future outcomes. Any individual session can deviate significantly from historical distributions, particularly around major news events or unusual volatility conditions.
- The indicator is most meaningful on instruments with well-defined, recurring session structure — futures, forex, and index products tend to work best.

---

## Core Concept

At the open of each tracked session, the indicator looks back through all completed sessions within the lookback window and asks: *how large was this session's range, and how far did price travel above and below the opening price?* It builds a statistical distribution from those completed sessions and draws the key percentile levels as horizontal lines extending forward from the current session's open price.

Every level is expressed as an **offset from the session open** — so a level labelled "NY P75" placed 200 points above the NY open means that in 75% of historical NY sessions, the session high was within 200 points of the open. The mirror level 200 points below means the same applies to the downside.

This gives you a live, continuously updated picture of where price is relative to what is historically normal for the current session.

---

## Sessions

Four sessions are tracked, each independently toggled:

**Asia** — 7:00 PM to 2:00 AM ET. Captures the overnight Asian market session.

**London** — 2:00 AM to 8:00 AM ET. Captures the European open and London session.

**New York** — 8:00 AM to 4:00 PM ET. Captures the US cash equity and futures session.

**Custom Session** — User-defined. You specify the start time, end time (both in HHMM format, Eastern Time), and a display label. This can be used to isolate any sub-session you find useful — for example, the opening drive (8:00–10:00 AM), the London close overlap (10:00 AM–12:00 PM), or an overnight Globex window. All the same statistics and levels are calculated for the custom session as for the named sessions.

All session times use the **America/New_York timezone** and do not automatically adjust for daylight saving transitions in other regions.

---

## Levels Explained

All levels are drawn as horizontal lines extending from the session open bar. They are extended bar by bar throughout the session. Each level appears both **above** (positive offset) and **below** (negative offset) the session open, reflecting that the distribution is applied symmetrically — the session could run in either direction.

### Median (P50)

The median session range. Half of all historical sessions in the lookback window had a total range smaller than this, half had larger. This is the central reference point — if price has already moved the median range from the open, the session has covered "normal" territory.

Displayed as a dashed line above and below the open, labelled e.g. `NY Med` and `NY -Med`.

### IQR Band (P25 / P75)

The interquartile range — the middle 50% of historical session ranges. The P75 line above the open means 75% of sessions had their high within this distance of the open; the P25 line means 25% of sessions exceeded this level on the upside.

Together the P25 and P75 lines form a band representing where the session range most commonly falls. Price moving beyond the P75 level is entering the upper quarter of historical sessions by range. An optional fill between these lines visually highlights the band.

Displayed as dotted lines, labelled e.g. `NY P75` / `NY P25`.

### P10 / P90

The outer 80% band. The P90 level above the open represents a large range day — only 10% of historical sessions have extended further. The P10 level is the inner boundary, below which only 10% of sessions failed to reach.

These levels help identify when a session is developing into an unusually large or unusually small range day. An optional fill between the outer and inner bands provides additional visual context.

Labelled e.g. `NY P90` / `NY -P90`, `NY P10` / `NY -P10`.

### P95

An optional extreme outlier level. Only 5% of sessions have extended beyond this distance from the open. Useful for identifying historically rare range expansions — not shown by default as they are rarely reached and can visually clutter the chart.

### Mean

The arithmetic average session range, plotted symmetrically above and below the open. Because session ranges tend to have a right-skewed distribution (occasional very large range days pull the average up), the mean is typically somewhat higher than the median. When the mean and median are noticeably different, it indicates the presence of outlier sessions in the lookback window.

Displayed as a dashed line, labelled e.g. `NY Mean` / `NY -Mean`.

### Mean ± 1 Standard Deviation

Optional. Plots lines one standard deviation above and below the mean range. Note that standard deviation assumes a roughly normal distribution, which session ranges typically do not follow — they tend to be right-skewed with fat tails. For this reason the StDev bands are disabled by default and the percentile-based levels (which make no distributional assumptions) are preferred. The tooltip in the settings explicitly flags this limitation.

Labelled e.g. `NY +1SD` / `NY -1SD`.

---

## Directional MAE / MFE Levels

This is the most distinctive feature of the indicator, and requires some explanation.

The indicator tracks each completed session as either **bullish** (closed above the opening price) or **bearish** (closed below the opening price). For each category it separately records:

- **MFE (Maximum Favourable Excursion)** — how far price moved in the winning direction before the session closed. For bull sessions this is the maximum distance above the open; for bear sessions it is the maximum distance below the open.
- **MAE (Maximum Adverse Excursion)** — how far price moved against the eventual session direction before recovering. For bull sessions this is the maximum distance price dropped below the open before closing higher; for bear sessions it is the maximum distance price rose above the open before closing lower.

From these directional arrays the indicator calculates and plots:

**Bull MFE50** — the median upside excursion on bull sessions. In 50% of sessions that closed above the open, price ran at least this far above the open at some point during the session. Plotted above the open.

**Bull MFE75** — the 75th percentile upside excursion on bull sessions. A higher-probability upside target for sessions that are developing as bullish.

**Bull MAE50** — the median adverse dip on bull sessions. Even in sessions that ultimately closed higher, price dipped at least this far below the open half the time. Plotted below the open. This is a guide to how much heat a long position from the open has historically needed to absorb before the session recovered.

**Bear MFE50** — the median downside excursion on bear sessions. In 50% of sessions that closed below the open, price fell at least this far. Plotted below the open.

**Bear MFE75** — the 75th percentile downside excursion on bear sessions.

**Bear MAE50** — the median adverse rally on bear sessions. Even in sessions that ultimately closed lower, price bounced at least this far above the open half the time. Plotted above the open.

### How to Use MAE / MFE

The MAE levels are particularly useful for position sizing and stop placement. If the Bull MAE50 for the NY session is 40 points, then a stop placed less than 40 points below the NY open on a long trade has historically been triggered in more than half of all ultimately bullish sessions — not because the trade was wrong, but because intraday chop reached that depth before the session recovered. Knowing this helps calibrate stop distance relative to historical session behaviour.

The MFE levels are useful as probabilistic targets. If NY Bull MFE75 is 150 points above the open, that level represents a target that 75% of historical bull sessions have reached or exceeded on the upside.

**Important:** the MAE/MFE levels are only drawn when there are at least 2 sessions of each type (bull or bear) in the lookback window. On instruments or sessions with very few bull or bear sessions in the lookback period, these levels may not appear or may be based on a very small sample.

The bull/bear session count is visible in the stats table (shown as e.g. `15b/12B` — 15 bull sessions, 12 bear sessions).

---

Stats Table
The stats table provides a numerical summary of the range distribution for each active session. It is shown at the bottom of the chart and updates on the last bar.
Session — Session name, plus bull/bear session count (e.g. NY 45b/38B). The bull count (lowercase b) shows how many sessions in the lookback window closed above their opening price; the bear count (uppercase B) shows how many closed below.
N — Total number of completed sessions in the lookback window for this session. This is your effective sample size — if it is low, treat the percentile levels with less confidence.
P25 — 25th percentile of session ranges in points. Three quarters of historical sessions had a range larger than this. A session still inside this level is developing as an unusually narrow range day.
P50 Med — Median (50th percentile) session range in points. Half of historical sessions had a smaller range, half had larger. This is the central reference point for what a typical session looks like.
P75 — 75th percentile of session ranges in points. Only one quarter of historical sessions exceeded this range. Price moving beyond the P75 level on the chart means the session is in the upper quarter of historical range days.
P90 — 90th percentile of session ranges in points. Only 10% of historical sessions exceeded this range. A session reaching the P90 level is a statistically large range day.
Mean/SD — The arithmetic mean session range and its standard deviation, both in points, displayed as mean/SD. Because session ranges tend to be right-skewed, the mean will typically be somewhat higher than the median. A large gap between the two indicates the presence of outlier sessions pulling the average up.


Sessions that are currently toggled off are shown with a grey background. The bottom row shows the current lookback setting as a reminder.

All range values in the table are in the **native points of the instrument** — the same units as the price axis.

---

## Fills

When fills are enabled, semi-transparent shaded regions are drawn between the P25/P75 band and between the P10/P90 band. These provide a quick visual read of where the session range typically falls without needing to read individual level labels. The fill colours are independently configurable. Fills are extended bar by bar as the session progresses alongside the lines.

---

## How to Use This Indicator

**At the session open:** The levels are drawn immediately from the opening bar. Note where price is relative to the P25, P50 and P75 levels — this sets the context for how large a range you might expect.

**During the session:** As price moves away from the open, the levels tell you where you are in the historical distribution. Price passing through the P75 level means the range has entered the upper quarter of historical sessions. Price still inside the P25 level late in the session suggests a narrow, below-average day.

**For target setting:** The MFE levels (bull or bear depending on the direction the session is developing) provide historically grounded targets. The P50 MFE is a conservative target; P75 MFE is more ambitious but still within the range of the majority of directional sessions.

**For stop placement:** The MAE levels provide context for how much adverse movement has historically occurred even in ultimately successful sessions. Stops placed tighter than the MAE50 level have historically been triggered in more than half of all sessions that eventually moved in the intended direction.

**Comparing sessions:** With multiple sessions visible simultaneously, you can compare how the current session's range compares to the Asia or London session that preceded it. An NY session already exceeding the Asia P90 level early in the day is developing into an unusually large range day by historical standards.

**The custom session** is most useful for isolating specific intraday windows you trade. For example, setting it to 09:30–11:00 gives you statistics purely for the opening drive, without the full-day NY range diluting the picture.

---

## Inputs and Settings

### Global Settings

- **Lookback Sessions** *(default: 100, minimum: 10)* — Number of completed sessions used to build the statistical distributions. Applies to all four sessions. Higher values give more stable percentile estimates but are less responsive to recent regime changes. Lower values track recent conditions more closely but are more susceptible to small-sample noise. A minimum of 30–50 sessions is recommended for reliable percentile estimates; the tooltip suggests 50+.
- **Label Size** *(default: Tiny)* — Size of the text labels on all level lines. Options: Tiny, Small, Normal.
- **Show Stats Table** — Toggle the summary table on or off.
- **Table Position** — Place the table in any corner: Top Left, Top Right, Bottom Left, Bottom Right.

### Sessions

- **Asia (7pm – 2am ET)** — Toggle Asia session levels on or off.
- **London (2am – 8am ET)** — Toggle London session levels on or off.
- **NY (8am – 4pm ET)** — Toggle New York session levels on or off.
- **Custom Session** — Toggle the custom session on or off. When enabled, three further inputs appear:
- **Custom Start (HHMM ET)** — Session start time in 24-hour format, Eastern Time. E.g. `0930` for 9:30 AM.
- **Custom End (HHMM ET)** — Session end time. E.g. `1200` for noon.
- **Custom Label** — The name shown on level labels and in the stats table for this session.

### Levels to Show

- **Median (P50)** — Toggle median lines on or off.
- **IQR Band (P25 / P75)** — Toggle the interquartile range lines and fill on or off.
- **P10 / P90** — Toggle the outer percentile lines and fill on or off.
- **P95 (extreme sessions)** — Toggle the P95 extreme level on or off. Off by default.
- **Mean** — Toggle mean lines on or off.
- **Mean +/- 1 StDev** — Toggle standard deviation bands on or off. The tooltip notes these are less reliable for skewed data and percentile levels are preferred.
- **MAE / MFE (bull+bear sep.)** — Toggle the directional max adverse/favourable excursion levels on or off. The tooltip explains these are excursions from the session open.
- **Separate Bull/Bear levels** — When MAE/MFE is enabled, this toggle determines whether bull and bear levels are drawn separately (on) or combined (off). Keeping this on is recommended as combining directional excursion data from bull and bear sessions produces less meaningful levels.

### Style

All colours are individually configurable:

- **Median color** *(default: near black)*
- **IQR color** *(default: dark grey)*
- **P10/P90 color** *(default: medium grey)*
- **P95 color** *(default: muted red)*
- **Mean color** *(default: dark blue)*
- **StDev color** *(default: medium blue)*
- **MAE color** *(default: muted red)* — used for adverse excursion levels
- **MFE color** *(default: muted green)* — used for favourable excursion levels
- **IQR fill** *(default: very light grey, 92% transparent)*
- **P10/P90 fill** *(default: very light grey, 96% transparent)*
- **Show fills** — Master toggle for all band fills.

---

## Limitations

**Lookback window applies across all sessions equally.** The same lookback count is used for Asia, London, NY, and custom. If you set a lookback of 100, each session independently accumulates up to 100 completed sessions. There is no cross-session weighting — a day with an unusual Asia session does not automatically influence the NY statistics for that day.

**Total number of completed sessions actually captured in the lookback window for this session. This may be significantly lower than your chosen lookback setting — on higher timeframes, TradingView's bar history limit means fewer historical bars are available, which directly reduces how many completed sessions the indicator can see. If you have set a lookback of 100 but the table shows N=18, your chart does not have enough history loaded for that timeframe. Switching to a lower timeframe or scrolling back further to load more history will increase the session count. If N is low, treat the percentile levels with less confidence.

**Bull/bear split reduces effective sample size.** When MAE/MFE is enabled, the directional arrays only contain sessions of each respective type. With a lookback of 100 sessions and a roughly 50/50 bull/bear split, each directional array will have approximately 50 sessions. With more extreme splits or smaller lookbacks, the directional levels may be based on fewer sessions than the overall range statistics. Always check the bull/bear count in the stats table.

**Session times are fixed to ET.** The indicator does not adapt to other timezone daylight saving changes. Users in regions that observe different DST schedules may notice sessions shifting by one hour at certain times of year.

**Custom session must fit within a single calendar day boundary in ET.** Overnight sessions that cross midnight require careful setup — the standard Asia session (which crosses midnight ET) works correctly because it uses a predefined time string, but custom sessions that cross midnight may not behave as expected depending on how TradingView resolves the time range.

**Standard deviation limitation.** As noted in the settings tooltip, session ranges are typically right-skewed — occasional very large range days create a long upper tail. Standard deviation assumes a symmetric normal distribution and will underestimate tail risk. The percentile-based levels (P90, P75 etc.) make no distributional assumptions and are more reliable for characterising the actual shape of the distribution.

**Timeframe sensitivity.** The indicator tracks session opens and closes bar by bar. On high timeframes (1H or above), session boundary detection may miss the exact opening bar or fail to capture intrabar session transitions accurately. Recommended timeframes are 1-minute through 30-minute charts. On lower timeframes, more bars are available and session boundaries are captured precisely.

**Past statistics do not predict future outcomes.** A P75 level means that 75% of historical sessions stayed within that range — it does not mean price will stop there, reverse there, or that the current session will follow the historical distribution. Use these levels as probabilistic context, not as targets or stops in isolation.

---

## Technical Notes

- Session ranges and directional excursions are stored in separate per-session arrays, trimmed to the lookback limit using a FIFO approach (oldest session dropped when the limit is reached)
- Lines and fills are extended bar by bar throughout each session using stored object references
- Compatible with all instruments and all timeframes; most accurate on timeframes of 30 minutes or below
Notas de Lançamento
Session Statistics [Combined]
A significant development of the original Session Statistical Levels indicator. Everything from the original is retained — the same percentile range bands, MAE/MFE levels, fills, and stats table — but the data model has been expanded, the session structure refined, and a new layer of intra-session sequencing analysis added. This description focuses on what has changed and what is new. Users familiar with the original indicator will find the core usage unchanged; the additions are either new toggles or run transparently in the background.

NY session split into Morning (8am–12pm ET) and Afternoon (12pm–5pm ET), each tracked independently — five sessions total
Each session has its own assigned colour applied to all its levels
Line style and width now configurable independently for range bands and MFE/MAE bands
Label offset control reduces label overlap at session boundaries
Both tables offer nine position options instead of four
P95 and Mean ± StDev options from the original are not present in this version

Overview Table — one row per session: N (completed sessions) | P25 / P50 / P75 / P90 range in points | Mean | Bull/Bear count.
Live Level Table — shows MFE/MAE excursion statistics for the active session (or last closed session). Header shows session name, open price, and current distance from open in points. Eight rows cover MFE P25–P90 (green) and MAE P25–P90 (red) with four columns:

Pts — the level in points from the session open
Reached — how many historical sessions hit this level
→ Next — of sessions that reached this level, what % continued to the next level
↓ Retrace — of sessions that reached this level, what % subsequently pulled back below the next lower level

The → Next and ↓ Retrace columns together answer the key question once price hits a level: historically, does it tend to continue or fade? For example, if MFE P75 shows → Next 60% and ↓ Retrace 25%, sessions that reached P75 continued to P90 more often than they reversed. Conversely a high Retrace % at a given level suggests that level has historically acted as a reliable fading point.

Aviso legal

As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.