# Cookbook

> Complete, runnable recipes to copy and adapt: Heikin Ashi, a squeeze, a trailing stop, divergence, a ribbon, volume spikes and a breakout strategy.

Each recipe is a complete script. Run it here, then **Open in editor** to try it on the live feed and change it.

## Heikin Ashi, computed yourself

The feed does not serve Heikin Ashi candles, but they are four lines of Pine. This draws them in a pane.

```pine
//@version=6
indicator("Heikin Ashi")
haClose = (open + high + low + close) / 4
var float haOpen = na
haOpen := na(haOpen[1]) ? (open + close) / 2 : (haOpen[1] + haClose[1]) / 2
haHigh = math.max(high, math.max(haOpen, haClose))
haLow = math.min(low, math.min(haOpen, haClose))
plotcandle(haOpen, haHigh, haLow, haClose, "HA", color = haClose >= haOpen ? color.teal : color.red)
```

## Squeeze: Bollinger inside Keltner

When the Bollinger Bands contract inside the Keltner Channel, volatility is low and a move often follows.

```pine
//@version=6
indicator("Squeeze")
[bbMid, bbUp, bbLo] = ta.bb(close, 20, 2)
[kcMid, kcUp, kcLo] = ta.kc(close, 20, 1.5)
squeezed = bbUp < kcUp and bbLo > kcLo
mom = ta.linreg(close - ta.sma(close, 20), 20, 0)
plot(mom, "Momentum", style = plot.style_columns, color = mom >= 0 ? color.new(color.teal, 30) : color.new(color.red, 30))
plotshape(squeezed, "Squeeze on", style = shape.circle, location = location.bottom, color = color.orange, size = size.tiny)
```

## ATR trailing stop

A stop that follows price up and never moves down (for longs), flipping when price crosses it.

```pine
//@version=6
indicator("ATR trailing stop", overlay = true)
mult = input.float(3.0, "ATR multiple", minval = 0.5, step = 0.5)
a = ta.atr(14) * mult
var float stop = na
var int dir = 1
if dir == 1
    stop := math.max(nz(stop, close - a), close - a)
    if close < stop
        dir := -1
        stop := close + a
else
    stop := math.min(nz(stop, close + a), close + a)
    if close > stop
        dir := 1
        stop := close - a
plot(stop, "Stop", color = dir == 1 ? color.teal : color.red, style = plot.style_linebr, linewidth = 2)
```

## RSI divergence

Bearish divergence: price makes a higher pivot high while RSI makes a lower one. Pivots are confirmed `5` bars later, so markers are shifted back with `offset`.

```pine
//@version=6
indicator("RSI divergence")
r = ta.rsi(close, 14)
lb = 5
ph = ta.pivothigh(r, lb, lb)
isPh = not na(ph)
prevR = ta.valuewhen(isPh, r[lb], 1)
prevH = ta.valuewhen(isPh, high[lb], 1)
bear = isPh and r[lb] < prevR and high[lb] > prevH
plot(r, "RSI", color = color.purple)
hline(70, color = color.gray)
hline(30, color = color.gray)
plotshape(bear, "Bearish divergence", style = shape.triangledown, location = location.top, color = color.red, offset = -lb, size = size.small)
```

## Moving average ribbon

```pine
//@version=6
indicator("EMA ribbon", overlay = true)
plot(ta.ema(close, 10), "10", color = color.new(#8b7cff, 0))
plot(ta.ema(close, 20), "20", color = color.new(#a48cf0, 0))
plot(ta.ema(close, 30), "30", color = color.new(#c59bff, 0))
plot(ta.ema(close, 40), "40", color = color.new(#e3a58f, 0))
plot(ta.ema(close, 50), "50", color = color.new(#ffb547, 0))
```

## Volume spikes

Volume is a tick count here, which makes spikes a good activity signal.

```pine
//@version=6
indicator("Volume spikes")
avg = ta.sma(volume, 20)
spike = volume > avg * 1.8
plot(volume, "Ticks", style = plot.style_columns, color = spike ? color.orange : color.new(color.gray, 60))
plot(avg, "Average", color = color.new(#8b7cff, 0))
```

## Donchian breakout strategy

```pine
//@version=6
strategy("Donchian breakout", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)
len = input.int(20, "Channel", minval = 5)
upper = ta.highest(high, len)[1]
lower = ta.lowest(low, len)[1]
plot(upper, "Upper", color = color.teal)
plot(lower, "Lower", color = color.red)
if close > upper
    strategy.entry("Long", strategy.long)
if close < lower
    strategy.entry("Short", strategy.short)
```

Using `[1]` compares with the channel as it stood *before* this bar, so a bar cannot break out of a channel that already includes it.
