# Pin bar on XAUUSD, 1-hour bars - specification (RIP Backtest protocol v1.0 plus the 8 Oct 2026 rules)

Specification written and **frozen 2026-11-24 (approved by the owner the same day: "Aprob"; draft 1 unchanged apart from this status line)**. No pin bar, signal or trade of this test has been computed. Once frozen, any change of a rule is documented and treated as a separate test; bugs may be fixed.

## Why this test, and what was looked at before
- **Chosen by the owner** on 2026-11-24 from four untested candidates (Keltner Channel on NQ, the 5-minute opening range breakout on ES, the pin bar on gold 1-hour bars, Williams %R on SPY); the Keltner test was run first, then this one. The design below is the one recommended in the design questions of 2026-11-24 and accepted by the owner the same day ("Da, pe recomandari").
- **The idea.** A pin bar is one candle with a long tail (the "nose") and a small body at the other end of its range: price pushed through a level and was pushed back within the bar. The name comes from Martin Pring's "Pinocchio bar" (the nose that lies about where price is going). The usual way it is taught:
  - the body sits in the top or bottom third of the bar, so the tail is about two thirds of the range or more (DailyForex, 2012; most broker and education pages say the same);
  - the tail should stick out of the surrounding bars, at a level, and a large pin bar counts for more than a small one;
  - the entry is a break of the end opposite the nose; a second common entry is a limit order at about half of the bar (the "50% retrace");
  - the stop goes just beyond the nose; the target is usually two times the risk;
  - pin bars in the direction of the trend are preferred.
  None of the pages looked at gives a test result that can be checked. We fix four readings of these common rules and try no others after the result.
- **Earlier tests on this file (disclosed):** the gold hub (`best-gold-trading-strategy`) lists 20 strategies in 123 versions; none passed every gate. The closest relative is the **engulfing candle on gold, 1-hour bars** (`eg_spec.md`, frozen 2026-10-21): four variants, all Buried 2 of 6, every one barely positive before costs (gross +0.012 to +0.056 R a trade) and negative after; the cost of $0.30 took about 0.14 R a trade because its median risk was only about $3, and the smallest third of its trades lost the most. That is why this test requires a pin bar at least as large as the average bar (rule 2 below). Other candle tests on the site: the inside bar (EUR/USD daily, 3 variants).
- **What is different from the engulfing test:** a one-bar pattern (not two), a minimum size, a nose that must poke out of the last 10 bars, an entry by a stop order beyond the bar (or a limit order at its middle) instead of the next open, and a stop one tick beyond the nose.
- **No pin bar has been computed on the site.** The site mentions pin bars only in passing (the inside bar post).
- Gold rose about elevenfold over the file, which favours long rules; that is why one variant takes only bullish pin bars. The control trades the same direction at other times.

## Instrument and data
- **XAUUSD** on the IC Markets MetaTrader 4 demo server, spot gold CFD in US dollars per ounce; 1-minute bid bars from the first bar of 2004 to **2026-10-07 16:57 New York**, the same cut as every published gold test and the gold hub.
- **The clock of our clock audit** (`gold_audit_clock.loadAuditClock`; the rule of 11 Nov 2026 for every new gold test).
- **1-hour bars** on the New York clock, closing on the hour (`gh_lib.barsOf(M, 60)`: a bar holds the minutes with close stamps in (close - 60, close]), built from the minutes that exist. "The previous bar" is the previous existing bar, across gaps too. Positions are held over breaks, weekends and holidays.
- **Prices** have two decimals; one tick is **$0.01**. All shape and level rules are evaluated in whole cents (integers), so no rounding can decide a pattern.
- **Data checks before the run** (`dtop_datacheck.js --out=./pb_out`, the checks used by the double top test: first and last minute and the cut, increasing stamps, the same minutes and prices as the uncorrected file, forex days per year, Friday-evening and Saturday minutes, the 08:30 release minute on the audit clock, the hole windows carried over), plus a check that every price has at most two decimals. Any failure stops the test.
- **Holes (rule 3):** the suspect and unclear gaps of `data_holes_xau.json`, carried over to the audit clock minute by minute. Trades open across such a gap are counted, and every gate and verdict is recomputed without them; a changed verdict becomes Inconclusive.

## Indicators (on 1-hour bars)
- **ATR(14)**, Wilder's: the true range (bar 0: high - low), averaged over bars 0..13 as a running mean, then at = (13 x at + tr) / 14 (`rsi2_protocol.indicators`, as in the double top test).
- **EMA(200)** of closes: the average of the first 200 closes at bar 199, then e[i] = e[i-1] + (2/201) x (c[i] - e[i-1]) (`rs_lib.ema`).
- Pin bars are looked for **from bar 500** on, so every indicator has its full history.

## The pin bar (bar i, with open O, high H, low L, close C; range G = H - L)
A **bullish pin bar** meets all three rules:
1. **Shape:** the lower tail is at least two thirds of the range: min(O, C) - L >= (2/3) x G, with G > 0 (in cents: 3 x (min(O, C) - L) >= 2 x G). The body and the upper tail are then in the top third. The colour of the bar does not matter.
2. **Size:** G >= ATR(14) of bar i - 1 (the bars before the pin bar). The pin bar is at least as large as the average bar.
3. **The nose sticks out:** L is strictly below the lowest low of the 10 bars before it (bars i - 10 .. i - 1).

A **bearish pin bar** is the mirror: H - max(O, C) >= (2/3) x G; G >= ATR(14) of bar i - 1; H strictly above the highest high of bars i - 10 .. i - 1. A bar cannot be both.

**No look-ahead:** a pin bar exists for the rules from the close of bar i; every rule uses bars up to i only.

## The trade
- **Stop:** one tick beyond the nose: L - $0.01 for a long, H + $0.01 for a short.
- **Entry, break versions (BRK):** a buy stop at H + $0.01 (a sell stop at L - $0.01 after a bearish pin bar), working only during the **next 1-hour bar** (the minutes of bar i + 1). It fills at the first minute whose high reaches the level (low, for a sell): at the level, or at the minute's open if the minute opens at or beyond it. Not filled by the end of bar i + 1: cancelled.
- **Entry, half version (HALF):** a buy limit at the middle of the pin bar, (H + L) / 2 (the same for a sell limit after a bearish pin bar), working during the **next three 1-hour bars** (bars i + 1 .. i + 3). It fills at the first minute whose low reaches the level (high, for a sell): at the level, or at the minute's open if the minute opens at or beyond it. If that fill would be at or beyond the stop, there is no trade and the order is cancelled. Not filled by the end of bar i + 3: cancelled.
- **R** = |fill - stop| in dollars per ounce, from the actual fill.
- **Target:** 2R from the fill: fill + 2R for a long, fill - 2R for a short.
- **Management on 1-minute bars:**
  - **in the fill minute** only the stop counts: if the minute also reached the stop, the trade is stopped there (we cannot know the order of prices inside the minute, so we assume the worse one); the target is not taken in the fill minute;
  - **from the next minute on:** the stop is tested before the target in every minute; a level gapped through fills at the minute's open; otherwise at the level;
  - no time limit; a trade still open at the end of the data closes at the last close.
- **One position at a time.** A pin bar is acted on only if, at its close, no trade is open and no order is still working. A pin bar that closes while a trade is open or an order is working is ignored: not queued and not taken later. A trade that exits in a minute inside or before bar i leaves bar i free. A pin bar that a variant does not trade (a bearish one in the long-only version, one against the trend filter) does not block anything.
- **Four variants:**
  - **PB-BRK:** bullish and bearish pin bars, break entry, target 2R.
  - **PB-TREND:** as PB-BRK, but a bullish pin bar only when its close is above the EMA(200) at bar i, a bearish one only when its close is below it (the pin bar as a pullback in a trend).
  - **PB-HALF:** bullish and bearish pin bars, the half (50%) limit entry, target 2R.
  - **PB-BRK-L:** bullish pin bars only, break entry, target 2R.

## Costs
- **Baseline, used for every gate and verdict:** $0.30 per ounce per round trip, as in every gold test; slippage zero beyond fills at a gapped open (optimistic, stated).
- **Cost sensitivity, reported, not gated:** $0.45 (1.5 x) and $0.75 (2.5 x) per round trip.
- **Financing, reported, not gated:** scenario A, 3% a year of the entry price per calendar day held, longs and shorts alike; scenario B, the IC Markets XAUUSD swap recorded on 2026-10-12, Wednesday counting three (`rs_lib`).
- Results in R per trade and in dollars per ounce.

## Periods, gates, control
- **Periods (by the date of the fill, New York):** development 2004 .. 2012; validation 2013 .. 2019; holdout 2020 .. 2026-10-07 (**already used** by the earlier gold tests).
- **Six gates per variant** (net R):
  1. validation profit factor >= 1.2;
  2. at least 50 trades in each period;
  3. development **t >= 2.5**: candle patterns already have seven versions on the site (the engulfing candle on gold, four; the inside bar on EUR/USD, three), so this family has far more than four. It is a stricter bar for a crowded family, not a full correction for multiple testing;
  4. above the 95th percentile of 500 runs of the matched control;
  5. positive without the 10 best trades;
  6. holdout net positive (flagged as used).

  A variant with fewer than 30 trades is Inconclusive.
- **Matched control (moved entries):**
  - All real fills are moved together by one random circular offset of whole days (at least 5% of the days), to the first existing minute at or after the same time of day on the new day (the same New York calendar day, else that entry is dropped), keeping each trade's direction.
  - The control enters at that minute's open. Its stop and target distances are the real trade's distances measured in ATR(14) units: each real distance is divided by the ATR(14) of the last 1-hour bar closed before the real fill and multiplied by the ATR(14) of the last bar closed before the control entry; R is scaled the same way.
  - The same management from the entry minute on (stop before target in every minute), the same costs, the same one-position rule.
  - 500 runs, seeds 1000 + run. Statistic: the average net R per trade. Gate 4 requires beating its 95th percentile.
- **Also reported:** pin bars per year (bullish and bearish) and how many each rule removes (shape, size, nose); how many orders filled, by gap or by touch, and how many were cancelled; win rate with its Wilson interval; exits by reason; long and short; by year; by R size (descriptive terciles); hours held; the cost per trade in R against the gross; the cost sensitivity and financing; the hole check; and, for the article, how many of the pin bars were also engulfing bars of the engulfing test on the same file.

## Engine and self-test (before the run)
New code:
- `pb_lib.js`, the engine (bars from `gh_lib.barsOf`, ATR from `rsi2_protocol.indicators`, EMA from `rs_lib.ema`, the booking of `rs_lib.book` with the fill price, the moved-entries control after `hs_lib`);
- `pb_ref.js`, an independent re-implementation with its own 1-hour bars, ATR, EMA, pin bars, orders and minute walk;
- `pb_selftest.js`, which must pass before any result is printed. It checks:
  1. **hand-worked cases:** the shape at its edge in cents (exactly two thirds passes, one cent less fails), a doji at the low end, both colours; the size against ATR of bar i - 1; the nose equal to the 10-bar low (fails) and one cent below (passes); the break order filled by touch, filled at a gapped open, and cancelled after one bar; the half order filled on the third bar, cancelled after the third, and refused when it would fill beyond the stop; the stop in the fill minute; the target not taken in the fill minute; stop and target in one later minute (stop); a pin bar ignored while a trade is open and while an order is working; the EMA(200) filter both ways; the long-only variant;
  2. the 1-hour bars, ATR(14) and EMA(200) against naive recomputations on the real data;
  3. the re-implementation matched pin bar by pin bar and trade by trade for all four variants on the real data;
  4. **no look-ahead:** the data cut at four points must give identical pin bars and trades for everything completed before each cut;
  5. the control at offset 0 reproducing the real trades' direction and entry minutes for the break versions.
- `pb_run.js` refuses a second run unless asked to reproduce; a reproduction must give identical outputs.

## Before the article
Results go to the owner first: all four variants and every gate, the verdict and its cause; bullish and bearish; by year; the pin bar counts and fills; target and stop shares; the cost sensitivity and financing; the hole check; the comparison with the engulfing test. One article, published only with the owner's agreement, whatever the result; it links the engulfing post and answers a different question (a one-bar rejection with a size and level filter and a stop-order entry, not a two-bar takeover at the next open).

## Reading the result
"Buried" or "Inconclusive" mean a convincing edge was not demonstrated; neither is proof that none exists. One market, one timeframe, one broker's demo history, four readings of a pattern with many versions.

### Run log
- 2026-11-24: frozen (owner: "Aprob"), draft 1 unchanged apart from this status line. sha256 of this file above the "### Run log" heading: 5f1a75253f9f827f602d3267abc7765aec7caa7e35a5b4fb1b807b9157567361.
- 2026-11-24, before the run: dtop_datacheck.js --out=./pb_out passed every check (the same data as the double top test), and pb_datacheck.js passed (every price has two decimals; low <= open, close <= high in every minute; the last minute at the cut). pb_selftest.js PASSED on its first run: 47 checks, among them 32 hand-worked cases checked on both the engine and the re-implementation (pb_ref.js, a one-pass state machine written by the same author from this spec); on the real data the 1-hour bars, ATR(14) and EMA(200) equal, the 2,008 pin bars identical bar by bar and every trade of the four variants identical; no look-ahead on four cuts of the data; the control at offset 0 entering at the real fill minutes with R unchanged (99.2-99.9% of the fills kept, the rest dropped by the one-position rule because a control trade enters at the minute's open). The trade counts seen by the self-test (1,252 / 526 / 1,415 / 754) are counts only; no result was printed.
- 2026-11-24: pb_run.js -> pb_out/ (one run). 2,008 pin bars (1,136 bullish, 872 bearish; 1.6% of the 129,357 bars examined; the shape alone 11.9%, of which the size rule kept 26% and the nose rule half of those). PB-BRK 1,252 trades, avg -0.063 R (gross +0.011, cost 0.074 R), control percentile 51.0, Buried 1/6; PB-TREND 526 trades, -0.003 R (gross +0.069), percentile 77.8, Buried 2/6 (gates 2 and 6; holdout +4.65 R); PB-HALF 1,415 trades, -0.081 R (gross +0.084, cost 0.165 R: median R $2.33), percentile 95.00 (beat 475 of 500 control runs; gate 4 passes at the line, the site's usual >= 95), Buried 2/6; PB-BRK-L 754 trades, -0.061 R (gross +0.010), percentile 42.4, Buried 1/6. Rule 3 changes no verdict. Win rates 33.7-36.6% against a target of 2R. No rule was changed. node pb_run.js --reproduce: 12 files compared, 0 differ.
- 2026-11-24: published as /posts/pin-bar-trading-strategy-gold/ on 24 Nov 2026 (the owner asked for the article on 2026-11-24: "Da, scrie-l").
- 2026-11-24: added to the gold hub (best-gold-trading-strategy): 4 versions; the hub now counts 127 versions of 21 strategies.
- 2026-11-24: deployed with the owner's explicit agreement ("Deploy"): 73 changed or new files, 72/73 byte-identical on ripbacktest.com and /about/ identical on ripbacktest.pages.dev (Cloudflare Email Obfuscation rewrites the address on the domain); live baseline zip ripbacktest-SITE-COMPLET-2026-11-24-d.zip.
