The rules at a glance
- Market: XAUUSD on the 1-hour chart
- Pin bar: a candle whose lower wick is at least two-thirds of its range, so the body sits in the top third, with a range of at least one ATR (14)
- Location: the pin's low is the lowest low of the last 20 candles
- Trend filter: it closes above the 200 EMA (bearish pins, the mirror image, close below it)
- Entry: a buy stop just above the pin's high, plus 5% of an ATR; cancelled if it hasn't filled within 2 candles, or if a candle closes below the pin's low first
- Stop loss: just below the pin's low, by the same buffer
- Target: twice the risk (2R)
What a pin bar shows
A bullish pin bar has a long lower wick and a small body near the top of the candle. During that hour, sellers pushed gold well down, buyers pushed it all the way back, and the candle closed near where it started. The long wick is the rejected price. A bearish pin is the mirror image: a long upper wick and the body at the bottom. Martin Pring is usually credited with the original name, the "Pinocchio bar": the candle "lies" about where the price was going, and the long wick is its nose.
The shape overlaps with the hammer and the shooting star in the candlestick patterns guide. These rules don't care about the body's colour; a pin can close slightly up or slightly down.
Why location matters more than the candle
On a 1-hour gold chart there's a candle with a long wick most days. What makes one worth trading is where it appears. The price action guide makes the same point: a signal at a level, in the direction of the trend, means more than one in the middle of nowhere. These rules turn that into two checks a script can make.
| Where the pin forms | Under these rules |
|---|---|
| At a new 20-candle low, above the 200 EMA (a pullback in an uptrend) | Trade it |
| At a new 20-candle low, below the 200 EMA (a falling market) | Skip it: the trend is down |
| In the middle of the recent range | Skip it: no level being tested |
| Small candle, under one ATR | Skip it: too small to mean much |
The rules, step by step
1. Find the pin bar
On a 1-hour XAUUSD chart, a bullish pin bar has a lower wick of at least two-thirds of the candle's range, measured from the low to the lower end of the body. Its range must be at least one 14-period ATR (measured on the candle before, so the pin doesn't inflate its own test).
2. Check where it formed
Its low must be the lowest low of the last 20 candles, so the pin rejects a fresh low. With the trend filter on, it must close above the 200 EMA.
3. Enter on a buy stop above the pin
Place a buy stop 5% of an ATR above the pin's high. You only get in if the price trades above the pin, which shows buyers following through. If the order hasn't filled within two candles, or a candle closes below the pin's low first, cancel it. A new pin bar before then replaces the order.
4. Stop below the wick, target 2R
The stop goes 5% of an ATR below the pin's low: if the price trades back below the rejected low, the idea was wrong. The target is twice the distance from the entry to the stop. Bearish pins are the mirror image, below the 200 EMA, with a sell stop under the pin's low.
Worked example: XAUUSD, 1-hour chart
Gold is in an uptrend, well above its 200 EMA, and pulls back for about a day. Then a 1-hour candle opens at 4,268.00, drops to 4,240.40, below the 20-candle low of 4,262.60, and closes at 4,269.80. Its range is $31.70, more than the ATR of 10.18, and the lower wick is 87% of it. That's a pin bar in the right place. With the pin included the ATR is 11.71, so the 5% buffer is $0.59: the buy stop goes at 4,272.69 and the stop loss at 4,239.81.
| XAUUSD long | |
|---|---|
| Entry (buy stop above the pin's high) | 4,272.69 |
| Stop loss (below the pin's low) | 4,239.81, $32.88 away |
| Target (2R) | 4,338.45, $65.76 away |
| Account and risk | $10,000, risking 1% = $100 |
| Lot size | $100 ÷ ($32.88 × 100 ounces) = 0.030, so 0.03 lots |
| Loss if the stop is hit | $98.64 |
| Profit at the target | $197.28 |
The next candle trades above the pin's high and fills the buy stop. Gold then climbs back into its trend and reaches the target 16 candles later. The stop here is over $30, because a meaningful pin bar on the 1-hour gold chart is a big candle. That's why the lot size is only 0.03. Plenty of pins fail: the buy stop fills, the price stalls and comes back through the low. The stop is there for those.
Why a buy stop and not a market order
Buying at the pin's close gets a better price but takes every pin, including the ones where the price keeps falling on the next candle. The buy stop waits for the price to trade above the pin, which costs a few dollars of entry price and filters out some of those. Neither is right by default. The script uses the stop order; to test the other way, compare it with the engulfing candle strategy, which enters at the close.
Risk management
- Expect wide stops. On the 1-hour chart a pin bar's range is often $20 to $40. At 1% of $10,000 that's 0.02 to 0.05 lots. The gold lot size calculator works it out.
- Watch the news calendar. A pin bar made by a data spike can be a one-off reaction rather than a real rejection. The gold news guide has the times.
- Weekend gaps. A pending order left over the weekend can fill at a gap far from the planned price. Cancel it on Friday.
Common mistakes
- Trading every long wick. Without the location and trend checks, a pin bar is just a candle.
- Putting the stop inside the wick. It's tempting because it makes the lot size bigger, but the wick is exactly where the market just tested.
- Leaving the order open for days. After two candles the pin's information is old.
- Counting pins on the 5-minute chart. There are dozens a day. The 1-hour chart shows ones that took an hour of trading to form.
Backtest it yourself on TradingView
This page doesn't quote a win rate or a profit figure, and you should be wary of anyone who does without showing the test. Results change with the market, the dates, the data feed and the costs you put in. The honest way is to run the test yourself, and the free script below does the work in a couple of minutes.
- Open the chart. On TradingView, open XAUUSD on the 1-hour chart.
- Add the script. Open the Pine Editor from the Pine icon in the panel on the right of the chart (older layouts have it as a tab under the chart), delete the code that's already there, paste this strategy's code and click "Add to chart".
- Put in your costs. Open the strategy's settings and go to Properties. Enter your broker's commission and a little slippage. A test without costs flatters every strategy, and short-term ones most of all.
- Read the Strategy Tester. The panel under the chart shows the net profit, the maximum drawdown, the profit factor, the number of trades and a list of every trade. Click a few trades and check on the chart that each one follows the rules. Trade sizes are shown in units rather than lots: 100,000 units is one lot of a forex pair, and 100 ounces is one lot of gold.
- Change one setting at a time. Compare the wick size (60% to 75% of the range), the location rule (10 to 50 candles), the trend filter on and off, how long the order stays (1 to 3 candles) and the 2R target against 1.5R. Then try the 4-hour chart, where pins are rarer and the stops wider.
As a rough guide, don't trust a result with fewer than 100 trades, a profit factor under 1 once costs are in, or a drawdown you couldn't sit through with real money. Settings tuned to look good on one stretch of history often fail on the next, so check the same settings on dates you didn't tune them on, then demo trade the strategy for a few weeks. How much history you can test depends on your TradingView plan: the free plan loads 5,000 candles, which is a few months of 15-minute candles but years of daily ones.
The script sizes each trade from the stop loss and your risk percentage, starts with $10,000 and assumes 1:100 leverage (a 1% margin setting), so the tester doesn't flag margin calls on normal forex trade sizes. Change these under Properties to match your account. By default TradingView fills orders at the open of the candle after the signal, so the fill can differ a little from the signal candle's close.
Download the free Pine Script and PDF
The script is written in Pine Script version 6 for this guide and follows the rules above. It can't read the economic calendar, so skipping news days is up to you. It's free to use, change and share. The PDF is a one-page cheat sheet with the pin bar rules, the filters, a worked example and a checklist, to keep next to your chart.
Show the code (Pine Script v6, 89 lines)
//@version=6
// XAUUSD Price Action (pin bar) strategy by PipLedger (https://pipledgerfx.com/xauusd-price-action-strategy)
// Looks for a pin bar that makes a new 20-candle low (or high) in the direction of the 200 EMA, then places a stop order just beyond the pin bar.
// Free to use and change. For education and backtesting only; not financial advice.
strategy("PipLedger: XAUUSD Pin Bar", shorttitle="Gold Pin Bar", overlay=true, initial_capital=10000, pyramiding=0, default_qty_type=strategy.fixed, default_qty_value=1, margin_long=1, margin_short=1, currency=currency.USD)
// ---------- Inputs
wickPct = input.float(66.0, "Long wick at least (% of the candle's range)", minval=50.0, maxval=90.0, step=1.0)
minRange = input.float(1.0, "Pin bar range at least (x ATR 14)", minval=0.0, step=0.1)
swingBars = input.int(20, "The pin must make a new low (or high) of the last (candles)", minval=5)
useTrend = input.bool(true, "Only trade in the direction of the 200 EMA")
bufAtr = input.float(0.05, "Entry and stop buffer (x ATR 14)", minval=0.0, step=0.01)
validBars = input.int(2, "Candles the entry order stays open", minval=1)
rr = input.float(2.0, "Target (R multiple)", minval=0.5, step=0.25)
riskPct = input.float(1.0, "Risk per trade (% of equity)", minval=0.1, maxval=5.0, step=0.1)
// ---------- Helpers
fx = strategy.convert_to_account(1.0) // one unit of the chart's quote currency in account currency (1 on USD-quoted charts)
calcQty(float entry, float stop) =>
float dist = math.abs(entry - stop)
dist > 0 and not na(fx) ? strategy.equity * riskPct / 100 / (dist * syminfo.pointvalue * fx) : 0.0
atr = ta.atr(14)
ema200 = ta.ema(close, 200)
rng = high - low
bigBar = rng > 0 and rng >= minRange * atr[1] // ATR before the pin, so the pin's own size doesn't move the bar
lowN = ta.lowest(low, swingBars)[1] // worked out on every candle (Pine v6 skips the right side of "and" when it can)
highN = ta.highest(high, swingBars)[1]
// bullish pin: long lower wick (the body sits in the top third of the candle) and a new 20-candle low
bullPin = bigBar and math.min(open, close) - low >= rng * wickPct / 100 and low <= lowN and (not useTrend or close > ema200)
bearPin = bigBar and high - math.max(open, close) >= rng * wickPct / 100 and high >= highN and (not useTrend or close < ema200)
var int pend = 0 // 1 = a buy stop waits, -1 = a sell stop waits
var int pBar = na
var float pHi = na
var float pLo = na
flat = strategy.position_size == 0
newLong = flat and bullPin
newShort = flat and bearPin
// a trade has opened, or the order has waited long enough, or a candle has closed beyond the pin's wick (and no new pin replaces it): cancel it
failed = (pend == 1 and close < pLo) or (pend == -1 and close > pHi)
if pend != 0 and (not flat or ((bar_index - pBar >= validBars or failed) and not newLong and not newShort))
strategy.cancel("Long")
strategy.cancel("Short")
pend := 0
// ---------- Orders: a stop order beyond the pin's nose side, stop loss beyond the wick
if newLong
strategy.cancel("Short")
float buf = atr * bufAtr
float entry = high + buf
float sl = low - buf
float q = calcQty(entry, sl)
pend := 0
if q > 0
// a newer pin moves the waiting order (calling strategy.entry again with the same id updates it)
strategy.entry("Long", strategy.long, qty=q, stop=entry)
strategy.exit("Long exit", "Long", stop=sl, limit=entry + (entry - sl) * rr)
pend := 1
pBar := bar_index
pHi := high
pLo := low
else
strategy.cancel("Long")
else if newShort
strategy.cancel("Long")
float buf = atr * bufAtr
float entry = low - buf
float sl = high + buf
float q = calcQty(entry, sl)
pend := 0
if q > 0
strategy.entry("Short", strategy.short, qty=q, stop=entry)
strategy.exit("Short exit", "Short", stop=sl, limit=entry - (sl - entry) * rr)
pend := -1
pBar := bar_index
pHi := high
pLo := low
else
strategy.cancel("Short")
// ---------- Chart
plot(pend != 0 ? pHi : na, "Pin high", color=color.new(color.teal, 0), style=plot.style_linebr)
plot(pend != 0 ? pLo : na, "Pin low", color=color.new(color.red, 0), style=plot.style_linebr)
plot(useTrend ? ema200 : na, "200 EMA", color=color.new(color.gray, 0))
plotshape(bullPin, "Bullish pin bar", shape.triangleup, location.belowbar, color.teal, size=size.small)
plotshape(bearPin, "Bearish pin bar", shape.triangledown, location.abovebar, color.red, size=size.small)Trading this from India
The rules work on any gold chart, and TradingView's XAUUSD chart is a fine place to learn and backtest them. To trade gold legally from India, though, use MCX gold futures, including the smaller Gold Mini and Gold Petal contracts, through a SEBI-registered broker. Trading XAUUSD as a CFD with an offshore forex platform isn't allowed for residents under FEMA. MCX gold trades from 9 a.m. to 11:30 p.m. IST while the US is on daylight saving time and to 11:55 p.m. in the US winter, which covers the London session and the New York morning. The contract is priced in rupees, and the script works in ATR rather than pips, so it needs no pip setting. The full picture is in is forex trading legal in India and XAUUSD market timings in India.
Related strategies: break and retest, inside bar, engulfing candle, previous day high and low, liquidity sweep, support and resistance bounce, XAUUSD intraday pivots. All 34 strategies are compared on one page in forex trading strategies.