Indicator · Pine Script v6 · On the chart

Taught to Trade - Sample Size Panel

by obekheet

This statistical indicator calculates the confidence interval around a user-defined trade count and observed win rate using the Wilson score interval method.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code112
Settings6inputs
LicenceMPL-2.0
Last update12 days ago16 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

112 lines of Pine Script v6 from scripts/sample-size-panel.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

sample-size-panel.pine
//@version=6

// Taught to Trade - Sample Size Panel
//
// You enter the number of trades you have and the rate you observed. It returns the
// confidence interval around that rate, how wide it is, whether it can be told apart
// from a coin flip, and how many trades it would take to narrow it.
//
// The usual answer is "far more than you have". That is the point.
//
// All maths is scalar arithmetic on inputs plus bar_index. Nothing repaints,
// nothing reads the future, and nothing here is a signal.
indicator("Taught to Trade - Sample Size Panel", shorttitle = "TT SAMPLE", overlay = true)

// ─────────────────────────────── Inputs ───────────────────────────────
grpR = "Your result"
nTrades = input.int(30,   "Number of trades", minval = 1, group = grpR,
     tooltip = "The real count from your trade list, not an estimate.")
winPct  = input.float(60.0, "Observed win rate (%)", minval = 0.0, maxval = 100.0, step = 0.5, group = grpR)
confIn  = input.string("95%", "Confidence level", options = ["90%", "95%", "99%"], group = grpR)

grpC = "Chart context"
holdBars = input.int(20, "Bars held per trade", minval = 1, group = grpC,
     tooltip = "Used only to estimate how many trades this chart's history could have produced.")

grpT = "Table"
posIn = input.string("Top right", "Position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpT)
szIn  = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grpT)

// ─────────────────────────────── Wilson score interval ───────────────────────────────
z = confIn == "90%" ? 1.6449 : confIn == "99%" ? 2.5758 : 1.9600
p = winPct / 100.0
n = math.max(nTrades, 1)

denom  = 1.0 + z * z / n
centre = (p + z * z / (2.0 * n)) / denom
margin = (z / denom) * math.sqrt(p * (1.0 - p) / n + z * z / (4.0 * n * n))

lo = math.max(0.0, centre - margin)
hi = math.min(1.0, centre + margin)
widthPP = (hi - lo) * 100.0

// Can the observed rate be told apart from a coin flip at this confidence?
beatsCoin = lo > 0.5
worseCoin = hi < 0.5
verdict   = beatsCoin ? "Yes, above 50%" : worseCoin ? "Yes, below 50%" : "No"

// Sample size needed for a target half-width, using the normal approximation.
f_need(float halfWidthPP) =>
    e = halfWidthPP / 100.0
    e > 0 ? math.ceil(z * z * p * (1.0 - p) / (e * e)) : na

need10 = f_need(10.0)
need5  = f_need(5.0)
need2  = f_need(2.0)

// Chart context
barsHere     = bar_index + 1
tradesOnChart = math.floor(barsHere / math.max(holdBars, 1))

f_pc(float v01) => str.tostring(v01 * 100.0, "#.#") + "%"

// ─────────────────────────────── Table ───────────────────────────────
tblPos = posIn == "Top left" ? position.top_left : posIn == "Bottom right" ? position.bottom_right : posIn == "Bottom left" ? position.bottom_left : position.top_right
tblSz  = szIn == "Tiny" ? size.tiny : szIn == "Normal" ? size.normal : size.small

bgCol  = color.new(#0E1526, 10)
txtCol = color.new(#E6EAF2, 0)
hdrCol = color.new(#2F6FED, 0)
badCol = color.new(#B3261E, 25)
okCol  = color.new(#2E7D32, 25)
warnCol = color.new(#B26A00, 25)

var table t = table.new(tblPos, 2, 10, border_width = 1, border_color = color.new(#2F6FED, 70))

f_l(int r, string s, color bc) => table.cell(t, 0, r, s, text_color = txtCol, text_size = tblSz, text_halign = text.align_left, bgcolor = bc)
f_v(int r, string s, color bc) => table.cell(t, 1, r, s, text_color = txtCol, text_size = tblSz, text_halign = text.align_right, bgcolor = bc)

if barstate.islast
    table.cell(t, 0, 0, "SAMPLE SIZE", text_color = color.white, text_size = tblSz, text_halign = text.align_left, bgcolor = hdrCol)
    table.cell(t, 1, 0, confIn + " confidence", text_color = color.white, text_size = tblSz, text_halign = text.align_right, bgcolor = hdrCol)

    f_l(1, "Trades", bgCol)
    f_v(1, str.tostring(n), n < 30 ? badCol : n < 100 ? warnCol : okCol)

    f_l(2, "Observed rate", bgCol)
    f_v(2, str.tostring(winPct, "#.#") + "%", bgCol)

    f_l(3, "True rate is between", bgCol)
    f_v(3, f_pc(lo) + "  and  " + f_pc(hi), bgCol)

    f_l(4, "Interval width", bgCol)
    f_v(4, str.tostring(widthPP, "#.#") + " pp", widthPP > 20.0 ? badCol : widthPP > 10.0 ? warnCol : okCol)

    f_l(5, "Tells apart from 50%?", bgCol)
    f_v(5, verdict, beatsCoin or worseCoin ? okCol : badCol)

    f_l(6, "Trades for +/-10pp", bgCol)
    f_v(6, na(need10) ? "-" : str.tostring(need10), bgCol)

    f_l(7, "Trades for +/-5pp", bgCol)
    f_v(7, na(need5) ? "-" : str.tostring(need5), bgCol)

    f_l(8, "Trades for +/-2pp", bgCol)
    f_v(8, na(need2) ? "-" : str.tostring(need2), bgCol)

    f_l(9, "This chart could give", bgCol)
    f_v(9, str.tostring(tradesOnChart) + " at " + str.tostring(holdBars) + " bars", tradesOnChart < nTrades ? warnCol : bgCol)

// ─────────────────────────────── No alerts, deliberately ───────────────────────────────
// This is a calculator. It has no market state to alert on, so it ships with no
// alert conditions rather than a placeholder that can never fire.

Overview

Taught to Trade - Sample Size Panel displays an on-chart table showing the plausible win rate range, total interval width, and whether the performance statistically differs from a 50% coin flip. It also computes how many trades are required to narrow the margin of error and estimates the chart capacity based on average holding time. It provides statistical evaluation without generating trade signals.

Details

Published by

obekheet on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Screeners & dashboards

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pine-scriptpinescripttechnical-analysistrading-indicatorstradingviewtradingview-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Number of tradesint30nTrades
Observed win rate (%)float60.0winPct
Confidence levelstring95%confIn
Bars held per tradeint20holdBars
PositionstringTop rightposIn
Text sizestringSmallszIn
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Symbol Info Helper

by everget
948 stars

Symbol Info Helper is an informational utility script that displays metadata about the currently selected instrument directly on the main price chart. It gathers built-in symbol properties including ticker identifier, description, instrument type, point value,

Indicator v4 Screeners & dashboards
Updated 10 days ago · GPL-3.0 GitHub repository of Symbol Info Helper
575 stars

The indicator plots ICT displacement candles on the price chart and can switch to retracement scanning. Displacement requires a move larger than a set share of the 14-bar average high-low range, lasting at least a chosen number of bars, with optional close, pr

Indicator v6 SMC / ICT / price actionCandlestick patterns
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Displacement Candles Indicator
330 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 2 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

Pricing Table

by namuan
318 stars

This indicator calculates theoretical options contract values and in-the-money probabilities using historical realized volatility across multiple standard deviations. It displays these metrics in a table overlay directly on the price chart. Users can adjust th

Indicator v4 Volatility & bandsScreeners & dashboards
Updated 2 months ago · MIT GitHub repository of Pricing Table

3938 Tracker

by Allen1212
303 stars

3938 Tracker visualizes historical buying and selling activity from ARK Invest funds directly on the main price chart. It plots labels at specific timestamps corresponding to trade dates, indicating buy or sell actions along with the count of participating ETF

Indicator v4 Screeners & dashboards
Updated 2 years ago · None stated GitHub repository of 3938 Tracker

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers