CoinScriptExamples
Examples
Complete CoinScript examples to copy, from the built-in templates through revenue vs. price, liquidations and other symbols to Bollinger Bands, volume spikes and a strategy for the backtest.
Every example on this page is a complete script. Copy it into the editor, click Run, and Add to chart puts it on the chart. The first five are the built-in templates, which you can also load through Templates in the MantaAi panel.
Delta EMA
Smooths the volume delta with an EMA and shows it as a histogram. Bars above zero mean aggressive buyers dominated in these candles, bars below it aggressive sellers.
// CoinScript · Delta EMA
const len = input('Länge', 21)
const d = ema(delta, len)
plot(d, { title: 'Delta EMA', color: '#00FFDA', style: 'hist' })
hline(0)- Draws in its own pane, positive bars turquoise, negative bars red.
- You set Länge in the pane's settings under Inputs.
- The delta comes from taker volume, see Language. For symbols without order flow, the pane stays empty.
CVD divergence
Marks candles where price and CVD diverge, and draws the CVD in its own pane.
// CoinScript · CVD divergence
// Price makes a new high, CVD does not (and vice versa)
const len = input('Fenster', 20)
const pHi = highest(high, len), cHi = highest(cvd, len)
const pLo = lowest(low, len), cLo = lowest(cvd, len)
const bear = close.map((_, i) => i > len && high[i] >= pHi[i] && cvd[i] < cHi[i - 1])
const bull = close.map((_, i) => i > len && low[i] <= pLo[i] && cvd[i] > cLo[i - 1])
signal(bear, { title: 'Bearish', color: '#FF1919', pos: 'above' })
signal(bull, { title: 'Bullish', color: '#00FFDA', pos: 'below' })
plot(cvd, { title: 'CVD', color: '#FFFCF0' })How to read it:
- Bearish (red triangle above the candle): The candle's high is the highest of the last 20 candles, but the CVD is below its previous high. The price is rising without buyers adding to it.
- Bullish (turquoise triangle below the candle): the reverse, at the low.
- In the backtest, Bullish becomes a long entry and Bearish a short entry.
Funding extreme
Normalizes the funding rate as a z-score, meaning the distance from the average of the last candles in standard deviations. Values above 2 or below −2 count as extreme: the funding rate is then unusually high or unusually low.
// CoinScript · Funding extreme
const z = zscore(funding, input('Fenster', 48))
plot(z, { title: 'Funding Z', color: '#FFEB3B' })
hline(2, { color: '#FF1919' })
hline(-2, { color: '#00FFDA' })- Works on crypto perpetuals only.
fundingmoves in steps and goes back about 1,000 candles; before that the line stays empty.- Fenster counts candles. 48 candles are two days on a 1-hour chart and eight on a 4-hour chart.
EMA band
Two EMAs directly on the price chart, crossovers as yellow markers.
// CoinScript · EMA band on the price
const fast = ema(close, input('Schnell', 21))
const slow = ema(close, input('Langsam', 55))
plot(fast, { title: 'EMA 21', color: '#00FFDA', overlay: true })
plot(slow, { title: 'EMA 55', color: '#FF1919', overlay: true })
signal(cross(fast, slow), { title: 'Kreuzung', color: '#FFEB3B', pos: 'below' })cross() detects both directions, and all markers sit below the candle. For the backtest, these are therefore long entries only. You can find a version with separate directions further down under “EMA crossover as a strategy”.
Revenue vs. price
For stocks: the company's revenue and earnings as lines in their own pane with their own scale, below the price. A yellow triangle marks each report where revenue is higher than in the last report, but the price has fallen since then.
// CoinScript · Revenue vs. price
// Stocks: revenue and earnings in billions in their own pane. Marks every report
// with more revenue than the last one, before which the price has fallen since.
const zwoelf = input('12 Monate (1) oder Quartal (0)', 1)
const umsatz = zwoelf ? revenueTtm : revenue
const gewinn = zwoelf ? netIncomeTtm : netIncome
plot(umsatz.map(v => v / 1e9), { title: 'Umsatz (Mrd.)', color: '#00FFDA' })
plot(gewinn.map(v => v / 1e9), { title: 'Gewinn (Mrd.)', color: '#FFFCF0' })
hline(0)
const treffer = close.map(() => false)
let letzter = -1
for (let i = 1; i < bars; i++) {
// A new report: the value changes on this day.
if (Number.isNaN(umsatz[i]) || umsatz[i] === umsatz[i - 1]) continue
if (letzter > 0 && umsatz[i] > umsatz[letzter] && close[i - 1] < close[letzter - 1]) treffer[i] = true
letzter = i
}
signal(treffer, { title: 'Umsatz hoch, Kurs runter', color: '#FFEB3B', pos: 'below' })
alertcondition(treffer, 'Umsatz hoch, Kurs runter')How to read it:
- The lines are staircases: each figure stands from its report day until the next report comes. Both share the pane's scale, in billions of the reporting currency.
- 12 Monate (1) oder Quartal (0) under Inputs: With 1, the script compares the revenue of the last twelve months. This smooths seasonal quarters and goes back further. With 0, it compares each quarter with the previous one.
- The triangle sits on the candle of the new report. It compares the close before this report with the one before the last report, so the reaction to the figures does not count.
- On crypto, funds and indices the pane stays empty, and the console says so.
- With Umsatz hoch, Kurs runter in the alert dialog, a script alert notifies you at the next report where this applies. Because the script reads company figures, mantacharts only checks this alert while the terminal is open.
Liquidations as a histogram
Liquidated longs pointing up, shorts pointing down, in $M, and a signal when a candle liquidates an unusual number of longs.
// CoinScript · Liquidations
const longs = liqLong.map(v => v / 1e6)
const shorts = liqShort.map(v => -v / 1e6)
plot(longs, { title: 'Longs (Mio. $)', style: 'hist', color: '#FF1919' })
plot(shorts, { title: 'Shorts (Mio. $)', style: 'hist', colorDn: '#00FFDA' })
hline(0)
const z = zscore(liqLong, input('Fenster', 48))
const schwelle = input('Schwelle', 3)
signal(z.map(v => v > schwelle), { title: 'Long-Kaskade', color: '#FF1919', pos: 'below' })- Only for crypto symbols with order flow. A candle without a liquidation is
0. zscoremeasures how far the candle is above the window's average. From 3 standard deviations, a triangle appears.
Relative strength against Bitcoin
How the chart's symbol is doing compared with Bitcoin: above 100, it has performed better since the start of the window.
// CoinScript · Relative strength against Bitcoin
const btc = symbol('BTC')
const n = input('Fenster', 90)
const rs = close.map((c, i) => (i >= n ? (c / close[i - n]) / (btc.close[i] / btc.close[i - n]) * 100 : NaN))
plot(rs, { title: 'Relative Stärke', color: '#B28CFF' })
hline(100)symbol('BTC')returns Bitcoin's candles in the chart's timeframe, aligned by time. On a stock chart, each candle takes the last Bitcoin candle at the same time.- Instead of
'BTC', any symbol from the symbol search works, such as'SPX'or'XAU'.
Bitcoin against the 10-year
The yield of the 10-year US government bond in its own pane, and a signal when it rises by more than half a percentage point within 20 candles.
// CoinScript · US 10-year yield
const r = feed('us10y')
plot(r, { title: 'US10Y %', color: '#FFEB3B' })
const punkte = input('Prozentpunkte', 0.5)
const sprung = change(r, input('Kerzen', 20)).map(v => v > punkte)
signal(sprung, { title: 'Rendite springt', color: '#FF1919', pos: 'above' })The yield is a daily value. On a 1-hour chart, every candle of a day carries the same value, and change then counts candles, not days.
RSI with zones
The RSI in its own pane with lines at 70 and 30. A signal appears when the RSI leaves a zone again.
// CoinScript · RSI with zones
const len = input('Länge', 14)
const r = rsi(close, len)
plot(r, { title: 'RSI', color: '#FFFCF0' })
hline(70, { color: '#FF1919' })
hline(30, { color: '#00FFDA' })
const ausTief = r.map((v, i) => i > 0 && r[i - 1] < 30 && v >= 30)
const ausHoch = r.map((v, i) => i > 0 && r[i - 1] > 70 && v <= 70)
signal(ausTief, { title: 'Aus überverkauft', color: '#00FFDA', pos: 'below' })
signal(ausHoch, { title: 'Aus überkauft', color: '#FF1919', pos: 'above' })The triangles sit on the candles in the price chart, and the pane shows the RSI.
Bollinger Bands with alert conditions
Middle line and two bands on the price chart, plus two conditions you can set a script alert on.
// CoinScript · Bollinger Bands
const len = input('Länge', 20)
const k = input('Faktor', 2)
const mid = sma(close, len)
const sd = stdev(close, len)
const upper = mid.map((m, i) => m + k * sd[i])
const lower = mid.map((m, i) => m - k * sd[i])
plot(mid, { title: 'Mitte', color: '#FFFCF0', overlay: true })
plot(upper, { title: 'Oben', color: '#FF1919', overlay: true })
plot(lower, { title: 'Unten', color: '#00FFDA', overlay: true })
alertcondition(close.map((c, i) => c > upper[i]), { title: 'Über dem oberen Band' })
alertcondition(close.map((c, i) => c < lower[i]), { title: 'Unter dem unteren Band' })alertcondition() draws nothing. After saving, “Über dem oberen Band” and “Unter dem unteren Band” appear in the alert dialog under Condition when you choose Script · {name} as the source.
Open interest: change in percent
How much open interest has risen or fallen compared with a fixed number of candles earlier, as a histogram.
// CoinScript · OI change in percent
const n = input('Kerzen', 24)
const pct = oi.map((v, i) => (i >= n ? (v / oi[i - n] - 1) * 100 : NaN))
plot(pct, { title: 'OI Veränderung %', color: '#00FFDA', colorDn: '#FF1919', style: 'hist' })
hline(0)- For crypto perpetuals only.
oiis in coin, and the percentages do not depend on that. - If the price rises with rising open interest, new positions are being added. If it rises with falling open interest, this points more to shorts closing.
Volume spikes
Shows each candle's relative volume compared with the average before it, and marks spikes. With the condition, you can get an alert on a spike.
// CoinScript · Volume spikes
const len = input('Fenster', 20)
const faktor = input('Faktor', 3)
const avg = sma(volume, len)
const rv = volume.map((v, i) => v / avg[i - 1])
plot(rv, { title: 'Relatives Volumen', color: '#FFEB3B', style: 'hist' })
hline(faktor, { color: '#FF1919' })
const spitze = rv.map(v => v >= faktor)
signal(spitze, { title: 'Volumen-Spitze', color: '#FFEB3B', pos: 'above' })
alertcondition(spitze, { title: 'Volumen-Spitze' })Relative volume compares each candle with the average of the 20 candles before it. 3 means three times as much volume as usual. In the running candle, the value keeps growing until it closes. For an alert on closed candles only, choose the On candle close trigger.
Taker buy share
The share of taker volume that falls on buys, smoothed. Above 50%, aggressive buyers dominate.
// CoinScript · Taker buy share
const anteil = buyVol.map((b, i) => b / (b + sellVol[i]) * 100)
plot(ema(anteil, input('Glättung', 10)), { title: 'Kaufanteil %', color: '#00FFDA' })
hline(50)EMA crossover as a strategy
A simple trend-following strategy for the backtest: long when the fast EMA crosses the slow one upward, short when it crosses downward.
// CoinScript · EMA crossover as a strategy
const fast = ema(close, input('Schnell', 9))
const slow = ema(close, input('Langsam', 21))
const up = fast.map((f, i) => i > 0 && fast[i - 1] <= slow[i - 1] && f > slow[i])
const down = fast.map((f, i) => i > 0 && fast[i - 1] >= slow[i - 1] && f < slow[i])
plot(fast, { title: 'EMA schnell', color: '#00FFDA', overlay: true })
plot(slow, { title: 'EMA langsam', color: '#FF1919', overlay: true })
signal(up, { title: 'Long', color: '#00FFDA', pos: 'below' })
signal(down, { title: 'Short', color: '#FF1919', pos: 'above' })How to test it:
- Switch to the Backtest tab. It calculates immediately, by default with Long + short and the Opposite signal exit: each opposite signal closes the position and opens the opposite direction.
- Under Optimization, you see Schnell and Langsam with a suggested range. Click Start optimization (Pro and up).
- Apply writes the chosen combination into the code.