Recipes

Recipes

On this page

Complete scripts you can paste into a new CodeLab file and run. Each one is annotated.

SMA crossover strategyStrategy

The "Hello World" of strategies. Go long when the fast SMA crosses above the slow one; close when it crosses back. Open the Backtest tab to see the trades.

TypeScript
export default indicator('SMA Cross Strategy', { placement: 'overlay' }, () => {
  const fastLen = input.int('Fast', 20, { min: 1, max: 200 });
  const slowLen = input.int('Slow', 50, { min: 2, max: 400 });
  const fast = ta.sma(close, fastLen);
  const slow = ta.sma(close, slowLen);

  plot(fast, { title: 'Fast SMA', color: '#2962ff' });
  plot(slow, { title: 'Slow SMA', color: '#ff9800' });
  plotShape(ta.crossover(fast, slow),  { title: 'Buy',  shape: 'triangleup',   location: 'below', color: '#089981' });
  plotShape(ta.crossunder(fast, slow), { title: 'Sell', shape: 'triangledown', location: 'above', color: '#f23645' });

  // Orders are queued now and fill at the NEXT bar's open (no look-ahead).
  if (ta.crossover(fast, slow).value) strategy.entry('long', 'long');
  if (ta.crossunder(fast, slow).value) strategy.close('death cross');
});

RSI divergence scanIndicator

Flag simple bullish divergence: price makes a new low for the window while RSI stays above its own low.

TypeScript
export default indicator('Bullish RSI Divergence', { placement: 'subpane' }, () => {
  const len = input.int('Lookback', 30, { min: 5, max: 200 });
  const rsi = ta.rsi(close, 14);

  // Price makes a new low for the lookback window...
  const priceLowerLow = low.lt(ta.lowest(low, len).offset(1));
  // ...while RSI stays above its own low for the same window.
  const rsiHigherLow = rsi.gt(ta.lowest(rsi, len).offset(1));

  plot(rsi, { title: 'RSI', color: '#7e57c2' });
  hline(30, { title: 'Oversold', color: '#787b86', style: 'dashed' });
  hline(70, { title: 'Overbought', color: '#787b86', style: 'dashed' });
  plotShape(priceLowerLow.and(rsiHigherLow), {
    title: 'Bullish divergence', shape: 'triangleup', color: '#089981',
    location: 'absolute', price: rsi,   // sit on the RSI line, not on the candles
  });
});

ATR trailing stopStrategy

Ride trends with a volatility-based trailing stop: enter on a close above the EMA, exit when price closes under the ATR trail.

TypeScript
export default indicator('EMA Trend + ATR Trail', { placement: 'overlay' }, () => {
  const emaLen = input.int('EMA length', 50, { min: 1, max: 400 });
  const atrLen = input.int('ATR length', 22, { min: 1, max: 200 });
  const mult   = input.float('ATR multiple', 3, { min: 0.5, max: 10, step: 0.5 });

  const ema = ta.ema(close, emaLen);
  // Chandelier-style trail: the highest close of the window minus a multiple of ATR.
  const trail = ta.highest(close, atrLen).sub(ta.atr(atrLen).mul(mult));

  plot(ema,   { title: 'EMA',   color: '#2962ff', width: 2 });
  plot(trail, { title: 'Trail', color: '#f23645', plotStyle: 'stepline' });

  if (ta.crossover(close, ema).value)    strategy.entry('trend', 'long');
  if (ta.crossunder(close, trail).value) strategy.close('trail hit');
});

Z-score indicatorIndicator

How stretched is price from its mean? Above +2 is stretched up, below -2 is stretched down.

TypeScript
export default indicator('Price Z-Score', { placement: 'subpane' }, () => {
  const len = input.int('Period', 20, { min: 2, max: 500 });

  // How many standard deviations is price from its mean?
  const z = close.sub(ta.sma(close, len)).div(ta.stdev(close, len));

  plot(z, { title: 'Z-score', color: '#22d3ee', width: 2 });
  hline(2,  { title: '+2', color: '#f23645', style: 'dashed' });
  hline(0,  { title: '0',  color: '#787b86' });
  hline(-2, { title: '-2', color: '#089981', style: 'dashed' });
  bgColor(z.gt(2).or(z.lt(-2)), { title: 'Stretched', color: 'rgba(255,235,59,0.08)' });
});